#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
typedef long long ll;
using namespace std;
const int maxn = 1 << 18;
int maxi[maxn * 2], alive[maxn * 2];
void merge(int i)
{
maxi[i] = max(maxi[i << 1], maxi[i << 1 | 1]);
alive[i] = alive[i << 1] + alive[i << 1 | 1];
}
void remove_element(int i)
{
i += maxn;
maxi[i] = -1, alive[i] = 0;
for (i = (i >> 1); i > 0; i >>= 1) merge(i);
}
int last_big(int x)
{
int i = 1;
for (; i < maxn; )
{
if (maxi[i << 1 | 1] < x) i = i << 1;
else i = i << 1 | 1;
}
return i - maxn;
}
int sum(int l, int r)
{
int s = 0;
for (l += maxn, r += maxn + 1; l < r; l >>= 1, r >>= 1)
{
if (l & 1) s += alive[l++];
if (r & 1) s += alive[--r];
}
return s;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 0; i < maxn * 2; i++) maxi[i] = -1;
int n;
cin >> n;
vector<int> d(n);
for (int i = 0; i < n; i++) cin >> d[i], maxi[i + maxn] = --d[i], alive[i + maxn] = 1;
for (int i = maxn - 1; i > 0; i--) merge(i);
vector<int> v = d;
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) if (v[i] < i)
{
cout << "-1\n";
return 0;
}
ll ans = 0;
for (int i = n - 1; i >= 0; i--)
{
int j = last_big(i);
//cout << j << endl;
ans += sum(j, n - 1) - 1;
remove_element(j);
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3460 KB |
Output is correct |
2 |
Correct |
5 ms |
3404 KB |
Output is correct |
3 |
Correct |
5 ms |
3404 KB |
Output is correct |
4 |
Correct |
5 ms |
3404 KB |
Output is correct |
5 |
Correct |
5 ms |
3404 KB |
Output is correct |
6 |
Correct |
5 ms |
3448 KB |
Output is correct |
7 |
Correct |
5 ms |
3404 KB |
Output is correct |
8 |
Correct |
5 ms |
3404 KB |
Output is correct |
9 |
Correct |
5 ms |
3404 KB |
Output is correct |
10 |
Correct |
5 ms |
3404 KB |
Output is correct |
11 |
Correct |
5 ms |
3404 KB |
Output is correct |
12 |
Correct |
5 ms |
3404 KB |
Output is correct |
13 |
Correct |
5 ms |
3404 KB |
Output is correct |
14 |
Correct |
5 ms |
3404 KB |
Output is correct |
15 |
Correct |
5 ms |
3404 KB |
Output is correct |
16 |
Correct |
5 ms |
3404 KB |
Output is correct |
17 |
Correct |
5 ms |
3404 KB |
Output is correct |
18 |
Correct |
5 ms |
3404 KB |
Output is correct |
19 |
Correct |
5 ms |
3472 KB |
Output is correct |
20 |
Correct |
5 ms |
3532 KB |
Output is correct |
21 |
Correct |
5 ms |
3404 KB |
Output is correct |
22 |
Correct |
5 ms |
3404 KB |
Output is correct |
23 |
Correct |
5 ms |
3412 KB |
Output is correct |
24 |
Correct |
5 ms |
3404 KB |
Output is correct |
25 |
Correct |
7 ms |
3404 KB |
Output is correct |
26 |
Correct |
5 ms |
3404 KB |
Output is correct |
27 |
Correct |
5 ms |
3412 KB |
Output is correct |
28 |
Correct |
5 ms |
3404 KB |
Output is correct |
29 |
Correct |
6 ms |
3404 KB |
Output is correct |
30 |
Correct |
5 ms |
3404 KB |
Output is correct |
31 |
Correct |
4 ms |
3404 KB |
Output is correct |
32 |
Correct |
5 ms |
3448 KB |
Output is correct |
33 |
Correct |
5 ms |
3404 KB |
Output is correct |
34 |
Correct |
5 ms |
3404 KB |
Output is correct |
35 |
Correct |
5 ms |
3404 KB |
Output is correct |
36 |
Correct |
5 ms |
3404 KB |
Output is correct |
37 |
Correct |
5 ms |
3404 KB |
Output is correct |
38 |
Correct |
5 ms |
3412 KB |
Output is correct |
39 |
Correct |
5 ms |
3412 KB |
Output is correct |
40 |
Correct |
6 ms |
3404 KB |
Output is correct |
41 |
Correct |
4 ms |
3404 KB |
Output is correct |
42 |
Correct |
6 ms |
3404 KB |
Output is correct |
43 |
Correct |
3 ms |
3404 KB |
Output is correct |
44 |
Correct |
3 ms |
3276 KB |
Output is correct |
45 |
Correct |
3 ms |
3276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3460 KB |
Output is correct |
2 |
Correct |
5 ms |
3404 KB |
Output is correct |
3 |
Correct |
5 ms |
3404 KB |
Output is correct |
4 |
Correct |
5 ms |
3404 KB |
Output is correct |
5 |
Correct |
5 ms |
3404 KB |
Output is correct |
6 |
Correct |
5 ms |
3448 KB |
Output is correct |
7 |
Correct |
5 ms |
3404 KB |
Output is correct |
8 |
Correct |
5 ms |
3404 KB |
Output is correct |
9 |
Correct |
5 ms |
3404 KB |
Output is correct |
10 |
Correct |
5 ms |
3404 KB |
Output is correct |
11 |
Correct |
5 ms |
3404 KB |
Output is correct |
12 |
Correct |
5 ms |
3404 KB |
Output is correct |
13 |
Correct |
5 ms |
3404 KB |
Output is correct |
14 |
Correct |
5 ms |
3404 KB |
Output is correct |
15 |
Correct |
5 ms |
3404 KB |
Output is correct |
16 |
Correct |
5 ms |
3404 KB |
Output is correct |
17 |
Correct |
5 ms |
3404 KB |
Output is correct |
18 |
Correct |
5 ms |
3404 KB |
Output is correct |
19 |
Correct |
5 ms |
3472 KB |
Output is correct |
20 |
Correct |
5 ms |
3532 KB |
Output is correct |
21 |
Correct |
5 ms |
3404 KB |
Output is correct |
22 |
Correct |
5 ms |
3404 KB |
Output is correct |
23 |
Correct |
5 ms |
3412 KB |
Output is correct |
24 |
Correct |
5 ms |
3404 KB |
Output is correct |
25 |
Correct |
7 ms |
3404 KB |
Output is correct |
26 |
Correct |
5 ms |
3404 KB |
Output is correct |
27 |
Correct |
5 ms |
3412 KB |
Output is correct |
28 |
Correct |
5 ms |
3404 KB |
Output is correct |
29 |
Correct |
6 ms |
3404 KB |
Output is correct |
30 |
Correct |
5 ms |
3404 KB |
Output is correct |
31 |
Correct |
4 ms |
3404 KB |
Output is correct |
32 |
Correct |
5 ms |
3448 KB |
Output is correct |
33 |
Correct |
5 ms |
3404 KB |
Output is correct |
34 |
Correct |
5 ms |
3404 KB |
Output is correct |
35 |
Correct |
5 ms |
3404 KB |
Output is correct |
36 |
Correct |
5 ms |
3404 KB |
Output is correct |
37 |
Correct |
5 ms |
3404 KB |
Output is correct |
38 |
Correct |
5 ms |
3412 KB |
Output is correct |
39 |
Correct |
5 ms |
3412 KB |
Output is correct |
40 |
Correct |
6 ms |
3404 KB |
Output is correct |
41 |
Correct |
4 ms |
3404 KB |
Output is correct |
42 |
Correct |
6 ms |
3404 KB |
Output is correct |
43 |
Correct |
3 ms |
3404 KB |
Output is correct |
44 |
Correct |
3 ms |
3276 KB |
Output is correct |
45 |
Correct |
3 ms |
3276 KB |
Output is correct |
46 |
Correct |
105 ms |
6980 KB |
Output is correct |
47 |
Correct |
108 ms |
7076 KB |
Output is correct |
48 |
Correct |
93 ms |
7036 KB |
Output is correct |
49 |
Correct |
94 ms |
7108 KB |
Output is correct |
50 |
Correct |
105 ms |
7092 KB |
Output is correct |
51 |
Correct |
100 ms |
7012 KB |
Output is correct |
52 |
Correct |
133 ms |
7000 KB |
Output is correct |
53 |
Correct |
131 ms |
7108 KB |
Output is correct |
54 |
Correct |
107 ms |
6940 KB |
Output is correct |
55 |
Correct |
88 ms |
6896 KB |
Output is correct |
56 |
Correct |
105 ms |
6932 KB |
Output is correct |
57 |
Correct |
110 ms |
7000 KB |
Output is correct |
58 |
Correct |
129 ms |
7004 KB |
Output is correct |
59 |
Correct |
130 ms |
6988 KB |
Output is correct |
60 |
Correct |
133 ms |
6964 KB |
Output is correct |
61 |
Correct |
103 ms |
7012 KB |
Output is correct |
62 |
Correct |
88 ms |
6968 KB |
Output is correct |
63 |
Correct |
107 ms |
6928 KB |
Output is correct |
64 |
Correct |
127 ms |
6988 KB |
Output is correct |
65 |
Correct |
145 ms |
6980 KB |
Output is correct |
66 |
Correct |
115 ms |
7104 KB |
Output is correct |
67 |
Correct |
104 ms |
7112 KB |
Output is correct |
68 |
Correct |
131 ms |
6988 KB |
Output is correct |
69 |
Correct |
123 ms |
6984 KB |
Output is correct |
70 |
Correct |
111 ms |
7028 KB |
Output is correct |
71 |
Correct |
111 ms |
6984 KB |
Output is correct |
72 |
Correct |
105 ms |
7036 KB |
Output is correct |
73 |
Correct |
114 ms |
6968 KB |
Output is correct |
74 |
Correct |
89 ms |
6988 KB |
Output is correct |
75 |
Correct |
113 ms |
7024 KB |
Output is correct |
76 |
Correct |
42 ms |
6984 KB |
Output is correct |
77 |
Correct |
85 ms |
7108 KB |
Output is correct |
78 |
Correct |
88 ms |
6964 KB |
Output is correct |
79 |
Correct |
89 ms |
6980 KB |
Output is correct |
80 |
Correct |
43 ms |
6924 KB |
Output is correct |
81 |
Correct |
89 ms |
7112 KB |
Output is correct |
82 |
Correct |
91 ms |
6980 KB |
Output is correct |
83 |
Correct |
87 ms |
7008 KB |
Output is correct |
84 |
Correct |
115 ms |
6976 KB |
Output is correct |
85 |
Correct |
112 ms |
7024 KB |
Output is correct |
86 |
Correct |
121 ms |
7068 KB |
Output is correct |
87 |
Correct |
49 ms |
7060 KB |
Output is correct |
88 |
Correct |
119 ms |
7048 KB |
Output is correct |