Submission #260717

# Submission time Handle Problem Language Result Execution time Memory
260717 2020-08-10T19:09:29 Z ChrisT Exercise Deadlines (CCO20_day1problem2) C++17
25 / 25
253 ms 18292 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pll = pair<ll,ll>;
using ld = long double;
#define all(x) (x).begin(),(x).end()
const int MN = 2e5 + 5, MOD = 1e9 + 7, BASE = 131;
int bit[MN],n; set<int> in;
void inc (int i) {
	for (;i<MN;i+=i&-i)
		++bit[i];
}
void dec (int i) {
	for (;i<MN;i+=i&-i)
		--bit[i];
}
int query (int i) {
	int ret = 0;
	for (;i;i^=i&-i)
		ret += bit[i];
	return ret;
}
int bsearch (int ind) {
	int l = 1, r = n, mid, ans = -1;
	while (l <= r) {
		mid = (l+r)/2;
		if (query(mid) <= ind) l = (ans = mid) + 1;
		else r = mid - 1;
	}
	return ans;
}
int main () {
	scanf ("%d",&n);
	vector<vector<int>> where(n+1);
	vector<int> d(n+1);
	ll ret = 0;
	for (int i = 1; i <= n; i++) scanf ("%d",&d[i]), where[d[i]].push_back(i), inc(i);
	for (int i = n; i >= 1; i--) {
		for (int go : where[i]) in.insert(go);
		int where = bsearch(i);
		auto it = in.upper_bound(where);
		if (it == in.begin()) return !printf ("-1\n");
		--it;
		int pos = *it;
		in.erase(it);
		ret += i - query(pos);
		dec(pos);
	}
	printf ("%lld\n",ret);
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   35 |  scanf ("%d",&n);
      |  ~~~~~~^~~~~~~~~
Main.cpp:39:37: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   39 |  for (int i = 1; i <= n; i++) scanf ("%d",&d[i]), where[d[i]].push_back(i), inc(i);
      |                               ~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 644 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 5 ms 640 KB Output is correct
6 Correct 5 ms 640 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 5 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 3 ms 640 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
15 Correct 4 ms 640 KB Output is correct
16 Correct 5 ms 640 KB Output is correct
17 Correct 4 ms 640 KB Output is correct
18 Correct 3 ms 640 KB Output is correct
19 Correct 4 ms 640 KB Output is correct
20 Correct 5 ms 640 KB Output is correct
21 Correct 4 ms 640 KB Output is correct
22 Correct 4 ms 640 KB Output is correct
23 Correct 5 ms 640 KB Output is correct
24 Correct 4 ms 640 KB Output is correct
25 Correct 4 ms 640 KB Output is correct
26 Correct 4 ms 704 KB Output is correct
27 Correct 5 ms 640 KB Output is correct
28 Correct 6 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 4 ms 640 KB Output is correct
31 Correct 4 ms 640 KB Output is correct
32 Correct 4 ms 768 KB Output is correct
33 Correct 4 ms 640 KB Output is correct
34 Correct 5 ms 640 KB Output is correct
35 Correct 4 ms 640 KB Output is correct
36 Correct 4 ms 768 KB Output is correct
37 Correct 5 ms 640 KB Output is correct
38 Correct 4 ms 640 KB Output is correct
39 Correct 4 ms 640 KB Output is correct
40 Correct 5 ms 640 KB Output is correct
41 Correct 2 ms 616 KB Output is correct
42 Correct 4 ms 640 KB Output is correct
43 Correct 3 ms 640 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 644 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 5 ms 640 KB Output is correct
6 Correct 5 ms 640 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 5 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 3 ms 640 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
15 Correct 4 ms 640 KB Output is correct
16 Correct 5 ms 640 KB Output is correct
17 Correct 4 ms 640 KB Output is correct
18 Correct 3 ms 640 KB Output is correct
19 Correct 4 ms 640 KB Output is correct
20 Correct 5 ms 640 KB Output is correct
21 Correct 4 ms 640 KB Output is correct
22 Correct 4 ms 640 KB Output is correct
23 Correct 5 ms 640 KB Output is correct
24 Correct 4 ms 640 KB Output is correct
25 Correct 4 ms 640 KB Output is correct
26 Correct 4 ms 704 KB Output is correct
27 Correct 5 ms 640 KB Output is correct
28 Correct 6 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 4 ms 640 KB Output is correct
31 Correct 4 ms 640 KB Output is correct
32 Correct 4 ms 768 KB Output is correct
33 Correct 4 ms 640 KB Output is correct
34 Correct 5 ms 640 KB Output is correct
35 Correct 4 ms 640 KB Output is correct
36 Correct 4 ms 768 KB Output is correct
37 Correct 5 ms 640 KB Output is correct
38 Correct 4 ms 640 KB Output is correct
39 Correct 4 ms 640 KB Output is correct
40 Correct 5 ms 640 KB Output is correct
41 Correct 2 ms 616 KB Output is correct
42 Correct 4 ms 640 KB Output is correct
43 Correct 3 ms 640 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 384 KB Output is correct
46 Correct 245 ms 14584 KB Output is correct
47 Correct 243 ms 14584 KB Output is correct
48 Correct 232 ms 14832 KB Output is correct
49 Correct 213 ms 14588 KB Output is correct
50 Correct 215 ms 14584 KB Output is correct
51 Correct 214 ms 14844 KB Output is correct
52 Correct 175 ms 14072 KB Output is correct
53 Correct 183 ms 14176 KB Output is correct
54 Correct 163 ms 14200 KB Output is correct
55 Correct 135 ms 14072 KB Output is correct
56 Correct 166 ms 14072 KB Output is correct
57 Correct 172 ms 14072 KB Output is correct
58 Correct 202 ms 11896 KB Output is correct
59 Correct 177 ms 11896 KB Output is correct
60 Correct 180 ms 11896 KB Output is correct
61 Correct 158 ms 11900 KB Output is correct
62 Correct 148 ms 11896 KB Output is correct
63 Correct 153 ms 11896 KB Output is correct
64 Correct 178 ms 11896 KB Output is correct
65 Correct 185 ms 11892 KB Output is correct
66 Correct 171 ms 11840 KB Output is correct
67 Correct 169 ms 11768 KB Output is correct
68 Correct 168 ms 11768 KB Output is correct
69 Correct 181 ms 11768 KB Output is correct
70 Correct 215 ms 12920 KB Output is correct
71 Correct 194 ms 12664 KB Output is correct
72 Correct 204 ms 12664 KB Output is correct
73 Correct 202 ms 12900 KB Output is correct
74 Correct 160 ms 12776 KB Output is correct
75 Correct 185 ms 12664 KB Output is correct
76 Correct 120 ms 12280 KB Output is correct
77 Correct 180 ms 18200 KB Output is correct
78 Correct 141 ms 14072 KB Output is correct
79 Correct 145 ms 12000 KB Output is correct
80 Correct 153 ms 13048 KB Output is correct
81 Correct 190 ms 18292 KB Output is correct
82 Correct 129 ms 14072 KB Output is correct
83 Correct 143 ms 12024 KB Output is correct
84 Correct 214 ms 13816 KB Output is correct
85 Correct 217 ms 13844 KB Output is correct
86 Correct 253 ms 13816 KB Output is correct
87 Correct 84 ms 11452 KB Output is correct
88 Correct 236 ms 13888 KB Output is correct