Submission #513646

# Submission time Handle Problem Language Result Execution time Memory
513646 2022-01-17T11:14:03 Z Mazaalai Money (IZhO17_money) C++17
100 / 100
278 ms 26880 KB
#include <bits/stdc++.h>
#define pb push_back
#define LINE "-----------------\n"
#define ALL(x) x.begin(),x.end()
using namespace std;
int n, m;
vector <int> nums, sorted;
const int N = 1e6 + 5;
const int M = 1e6 + 1;
int cnt[N], nx[N], pv[N];
void print(vector<int>x) {
	for (auto el : x) cout << el << " "; cout << '\n';
}
void showList() {
	cout << "LIST: ";
	int x = nx[0];
	while(x != M) {
		for (int j = 0; j < cnt[x]; j++) cout << x << ' ';
		x = nx[x];
	}
	cout << '\n';
	cout << "TSIL: ";
	x = pv[M];
	while(x != 0) {
		for (int j = 0; j < cnt[x]; j++) cout << x << ' ';
		x = pv[x];
	}
	cout << '\n';
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	// freopen("in.txt", "r", stdin);
	// freopen("out.txt", "w", stdout);
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x; cin >> x;
		cnt[x]++;
		nums.pb(x);
		sorted.pb(x);
	}
	sort(ALL(sorted));
	nx[0] = sorted[0];
	pv[sorted[0]] = 0;
	for (int i = 0; i < n - 1; i++) {
		if (sorted[i] != sorted[i+1]) {
			nx[sorted[i]] = sorted[i+1];
			pv[sorted[i+1]] = sorted[i];
		}
	}
	nx[sorted.back()] = M;
	pv[M] = sorted.back();
		// showList();
	int ans = 0;
	for (int i = n - 1; i >= 0; i--) {
		int j, curCnt = 1;
		bool border = 1;
		for (j = i-1; j >= 0; j--) {
			if (nums[j] == nums[j+1]) {
				curCnt++;
				continue;
			} else {
				if (nx[nums[j]] != nums[j+1]) break;
				if (!border && curCnt != cnt[nums[j+1]]) break;
				cnt[nums[j+1]] -= curCnt;
				if (cnt[nums[j+1]] == 0) {
					int a = pv[nums[j+1]], b = nx[nums[j+1]];
					// cout << "REMOVE: " << nums[j+1] << " " << a << ',' << b << '\n';
					nx[a] = b;
					pv[b] = a;
				}
				border = 0;
				curCnt = 1;
			}
			// showList();
		}
		// cout << LINE;
		// for (int k = j+1; k <= i; k++) cout << nums[k] << " \n"[k==i];
		cnt[nums[j+1]] -= curCnt;
		if (cnt[nums[j+1]] == 0) {
			int a = pv[nums[j+1]], b = nx[nums[j+1]];
			// cout << "REMOVE: " << nums[j+1] << " " << a << ',' << b << '\n';
			nx[a] = b;
			pv[b] = a;
		}
		// showList();
		ans++;
		i = j+1;
	}
	cout << ans << '\n';
}







Compilation message

money.cpp: In function 'void print(std::vector<int>)':
money.cpp:12:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   12 |  for (auto el : x) cout << el << " "; cout << '\n';
      |  ^~~
money.cpp:12:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   12 |  for (auto el : x) cout << el << " "; cout << '\n';
      |                                       ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 1 ms 316 KB Output is correct
37 Correct 1 ms 1996 KB Output is correct
38 Correct 2 ms 1988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 1 ms 316 KB Output is correct
37 Correct 1 ms 1996 KB Output is correct
38 Correct 2 ms 1988 KB Output is correct
39 Correct 60 ms 7528 KB Output is correct
40 Correct 91 ms 12108 KB Output is correct
41 Correct 47 ms 6176 KB Output is correct
42 Correct 40 ms 5688 KB Output is correct
43 Correct 30 ms 4180 KB Output is correct
44 Correct 117 ms 15148 KB Output is correct
45 Correct 113 ms 15024 KB Output is correct
46 Correct 124 ms 15364 KB Output is correct
47 Correct 115 ms 15124 KB Output is correct
48 Correct 115 ms 15244 KB Output is correct
49 Correct 185 ms 26784 KB Output is correct
50 Correct 176 ms 26772 KB Output is correct
51 Correct 201 ms 26804 KB Output is correct
52 Correct 197 ms 26876 KB Output is correct
53 Correct 208 ms 26880 KB Output is correct
54 Correct 182 ms 26644 KB Output is correct
55 Correct 270 ms 26844 KB Output is correct
56 Correct 273 ms 26816 KB Output is correct
57 Correct 250 ms 26808 KB Output is correct
58 Correct 278 ms 26776 KB Output is correct
59 Correct 278 ms 26880 KB Output is correct
60 Correct 237 ms 26804 KB Output is correct
61 Correct 255 ms 26772 KB Output is correct
62 Correct 264 ms 26816 KB Output is correct