Submission #67784688


Source Code Expand

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

using namespace std;

inline int nxt() {
	int x;
	cin >> x;
	return x;
}

void reduce(vector<int>& cur) {
	while ((int)cur.size() >= 2) {
		int x = cur.back();
		if (*(cur.end() - 2) == x) {
			cur.pop_back();
			cur.back() += 1;
		} else {
			break;
		}
	}
}

void solve() {
	int n = nxt();
	long long ans = 0;
	vector<int> cur;
	for (int i = 0; i < n; ++i) {
		int x = nxt();
		while (!cur.empty() && cur.back() < x) {
			int y = cur.back();
			cur.pop_back();
			int mn = x;
			if (!cur.empty() && cur.back() < mn) {
				mn = cur.back();
			}
			ans += mn - y;
			cur.push_back(mn);
			reduce(cur);
		}
		cur.push_back(x);
		reduce(cur);
	}
	if ((int)cur.size() > 1) {
		ans += cur[0] - cur.back() - (int)cur.size() + 2;
	}
	cout << ans << "\n";
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int t = nxt();
	while (t--) {
		solve();
	}

	return 0;
}

Submission Info

Submission Time
Task A - Merge and Increment
User Golovanov399
Language C++ 20 (gcc 12.2)
Score 700
Code Size 1005 Byte
Status AC
Exec Time 23 ms
Memory 4200 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 1
AC × 64
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 01_small_08.txt, 01_small_09.txt, 01_small_10.txt, 01_small_11.txt, 01_small_12.txt, 01_small_13.txt, 01_small_14.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 03_sorted_00.txt, 03_sorted_01.txt, 03_sorted_02.txt, 03_sorted_03.txt, 03_sorted_04.txt, 03_sorted_05.txt, 04_almost_sorted_00.txt, 04_almost_sorted_01.txt, 04_almost_sorted_02.txt, 04_almost_sorted_03.txt, 04_almost_sorted_04.txt, 04_almost_sorted_05.txt, 05_same_00.txt, 05_same_01.txt, 05_same_02.txt, 06_corner_00.txt, 06_corner_01.txt, 06_corner_02.txt, 07_hack_1_00.txt, 07_hack_1_01.txt, 07_hack_1_02.txt, 07_hack_1_03.txt, 07_hack_1_04.txt, 08_hack_2_00.txt, 08_hack_2_01.txt, 08_hack_2_02.txt, 08_hack_2_03.txt, 08_hack_2_04.txt, 09_hack_3_00.txt, 09_hack_3_01.txt, 09_hack_3_02.txt, 09_hack_3_03.txt, 09_hack_3_04.txt, 09_hack_3_05.txt, 09_hack_3_06.txt, 09_hack_3_07.txt, 09_hack_3_08.txt, 09_hack_3_09.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3432 KiB
01_small_00.txt AC 11 ms 3416 KiB
01_small_01.txt AC 11 ms 3480 KiB
01_small_02.txt AC 9 ms 3420 KiB
01_small_03.txt AC 23 ms 3444 KiB
01_small_04.txt AC 20 ms 3476 KiB
01_small_05.txt AC 19 ms 3428 KiB
01_small_06.txt AC 18 ms 3472 KiB
01_small_07.txt AC 16 ms 3400 KiB
01_small_08.txt AC 14 ms 3324 KiB
01_small_09.txt AC 17 ms 3472 KiB
01_small_10.txt AC 14 ms 3404 KiB
01_small_11.txt AC 13 ms 3372 KiB
01_small_12.txt AC 12 ms 3492 KiB
01_small_13.txt AC 11 ms 3440 KiB
01_small_14.txt AC 10 ms 3540 KiB
02_random_00.txt AC 11 ms 3480 KiB
02_random_01.txt AC 13 ms 3420 KiB
02_random_02.txt AC 11 ms 3416 KiB
02_random_03.txt AC 13 ms 3524 KiB
02_random_04.txt AC 13 ms 3384 KiB
02_random_05.txt AC 13 ms 3412 KiB
02_random_06.txt AC 8 ms 3468 KiB
02_random_07.txt AC 13 ms 3444 KiB
02_random_08.txt AC 8 ms 3332 KiB
02_random_09.txt AC 13 ms 3464 KiB
03_sorted_00.txt AC 11 ms 3476 KiB
03_sorted_01.txt AC 11 ms 4068 KiB
03_sorted_02.txt AC 11 ms 4048 KiB
03_sorted_03.txt AC 11 ms 3464 KiB
03_sorted_04.txt AC 11 ms 4120 KiB
03_sorted_05.txt AC 11 ms 4152 KiB
04_almost_sorted_00.txt AC 11 ms 3436 KiB
04_almost_sorted_01.txt AC 11 ms 4156 KiB
04_almost_sorted_02.txt AC 11 ms 4200 KiB
04_almost_sorted_03.txt AC 11 ms 3488 KiB
04_almost_sorted_04.txt AC 11 ms 4096 KiB
04_almost_sorted_05.txt AC 11 ms 4064 KiB
05_same_00.txt AC 7 ms 3424 KiB
05_same_01.txt AC 12 ms 3472 KiB
05_same_02.txt AC 11 ms 3444 KiB
06_corner_00.txt AC 10 ms 3420 KiB
06_corner_01.txt AC 11 ms 4040 KiB
06_corner_02.txt AC 11 ms 3480 KiB
07_hack_1_00.txt AC 12 ms 3468 KiB
07_hack_1_01.txt AC 11 ms 3468 KiB
07_hack_1_02.txt AC 13 ms 3476 KiB
07_hack_1_03.txt AC 11 ms 3464 KiB
07_hack_1_04.txt AC 11 ms 3468 KiB
08_hack_2_00.txt AC 13 ms 3484 KiB
08_hack_2_01.txt AC 13 ms 3608 KiB
08_hack_2_02.txt AC 13 ms 3472 KiB
08_hack_2_03.txt AC 13 ms 3468 KiB
08_hack_2_04.txt AC 13 ms 3328 KiB
09_hack_3_00.txt AC 7 ms 3480 KiB
09_hack_3_01.txt AC 7 ms 3472 KiB
09_hack_3_02.txt AC 9 ms 3416 KiB
09_hack_3_03.txt AC 9 ms 3472 KiB
09_hack_3_04.txt AC 9 ms 3480 KiB
09_hack_3_05.txt AC 9 ms 3472 KiB
09_hack_3_06.txt AC 10 ms 3604 KiB
09_hack_3_07.txt AC 10 ms 3408 KiB
09_hack_3_08.txt AC 10 ms 3604 KiB
09_hack_3_09.txt AC 10 ms 3608 KiB