#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,i,e,f,g,n,m,k,l,ans,BITree[500005],A[500005];
vector <long long> v[500005];
priority_queue <long long> pq;
long long getSum(long long idx) {
idx=idx+1;
long long sum=0;
while(idx>0) {
sum+=BITree[idx];
idx-=idx&(-idx);
}
return sum;
}
void updateBIT(long long idx,long long val) {
idx=idx+1;
while(idx<=200005) {
BITree[idx]+=val;
idx+=idx&(-idx);
}
}
int main() {
cin>>n;
for(long long i=1;i<=n;i++) {
cin>>A[i];
v[A[i]].push_back(i);
}
sort(A+1,A+1+n);
for(long long i=1;i<=n;i++) {
if(A[i]<i) { cout<<-1; return 0; }
}
for(long long i=n;i>=1;i--) {
for(long long j=0;j<v[i].size();j++)
pq.push(v[i][j]);
k=pq.top();
//cout<<k<<" ";
pq.pop();
a=getSum(k-1);
//cout<<a<<" ";
ans+=i-(k-a);
//cout<<ans<<endl;
updateBIT(k,1);
}
cout<<ans;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:33:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(long long j=0;j<v[i].size();j++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12288 KB |
Output is correct |
2 |
Correct |
12 ms |
12288 KB |
Output is correct |
3 |
Correct |
15 ms |
12288 KB |
Output is correct |
4 |
Correct |
14 ms |
12384 KB |
Output is correct |
5 |
Correct |
13 ms |
12288 KB |
Output is correct |
6 |
Correct |
12 ms |
12288 KB |
Output is correct |
7 |
Correct |
12 ms |
12416 KB |
Output is correct |
8 |
Correct |
12 ms |
12416 KB |
Output is correct |
9 |
Correct |
13 ms |
12288 KB |
Output is correct |
10 |
Correct |
11 ms |
12416 KB |
Output is correct |
11 |
Correct |
12 ms |
12416 KB |
Output is correct |
12 |
Correct |
11 ms |
12416 KB |
Output is correct |
13 |
Correct |
12 ms |
12288 KB |
Output is correct |
14 |
Correct |
12 ms |
12288 KB |
Output is correct |
15 |
Correct |
12 ms |
12288 KB |
Output is correct |
16 |
Correct |
12 ms |
12288 KB |
Output is correct |
17 |
Correct |
11 ms |
12288 KB |
Output is correct |
18 |
Correct |
11 ms |
12288 KB |
Output is correct |
19 |
Correct |
12 ms |
12288 KB |
Output is correct |
20 |
Correct |
12 ms |
12288 KB |
Output is correct |
21 |
Correct |
12 ms |
12320 KB |
Output is correct |
22 |
Correct |
13 ms |
12288 KB |
Output is correct |
23 |
Correct |
14 ms |
12288 KB |
Output is correct |
24 |
Correct |
12 ms |
12288 KB |
Output is correct |
25 |
Correct |
14 ms |
12288 KB |
Output is correct |
26 |
Correct |
12 ms |
12288 KB |
Output is correct |
27 |
Correct |
13 ms |
12288 KB |
Output is correct |
28 |
Correct |
12 ms |
12288 KB |
Output is correct |
29 |
Correct |
12 ms |
12288 KB |
Output is correct |
30 |
Correct |
11 ms |
12288 KB |
Output is correct |
31 |
Correct |
11 ms |
12160 KB |
Output is correct |
32 |
Correct |
13 ms |
12408 KB |
Output is correct |
33 |
Correct |
12 ms |
12416 KB |
Output is correct |
34 |
Correct |
12 ms |
12288 KB |
Output is correct |
35 |
Correct |
11 ms |
12160 KB |
Output is correct |
36 |
Correct |
12 ms |
12416 KB |
Output is correct |
37 |
Correct |
11 ms |
12288 KB |
Output is correct |
38 |
Correct |
11 ms |
12288 KB |
Output is correct |
39 |
Correct |
13 ms |
12288 KB |
Output is correct |
40 |
Correct |
12 ms |
12288 KB |
Output is correct |
41 |
Correct |
11 ms |
12288 KB |
Output is correct |
42 |
Correct |
13 ms |
12288 KB |
Output is correct |
43 |
Correct |
11 ms |
12288 KB |
Output is correct |
44 |
Correct |
8 ms |
12160 KB |
Output is correct |
45 |
Correct |
8 ms |
12032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12288 KB |
Output is correct |
2 |
Correct |
12 ms |
12288 KB |
Output is correct |
3 |
Correct |
15 ms |
12288 KB |
Output is correct |
4 |
Correct |
14 ms |
12384 KB |
Output is correct |
5 |
Correct |
13 ms |
12288 KB |
Output is correct |
6 |
Correct |
12 ms |
12288 KB |
Output is correct |
7 |
Correct |
12 ms |
12416 KB |
Output is correct |
8 |
Correct |
12 ms |
12416 KB |
Output is correct |
9 |
Correct |
13 ms |
12288 KB |
Output is correct |
10 |
Correct |
11 ms |
12416 KB |
Output is correct |
11 |
Correct |
12 ms |
12416 KB |
Output is correct |
12 |
Correct |
11 ms |
12416 KB |
Output is correct |
13 |
Correct |
12 ms |
12288 KB |
Output is correct |
14 |
Correct |
12 ms |
12288 KB |
Output is correct |
15 |
Correct |
12 ms |
12288 KB |
Output is correct |
16 |
Correct |
12 ms |
12288 KB |
Output is correct |
17 |
Correct |
11 ms |
12288 KB |
Output is correct |
18 |
Correct |
11 ms |
12288 KB |
Output is correct |
19 |
Correct |
12 ms |
12288 KB |
Output is correct |
20 |
Correct |
12 ms |
12288 KB |
Output is correct |
21 |
Correct |
12 ms |
12320 KB |
Output is correct |
22 |
Correct |
13 ms |
12288 KB |
Output is correct |
23 |
Correct |
14 ms |
12288 KB |
Output is correct |
24 |
Correct |
12 ms |
12288 KB |
Output is correct |
25 |
Correct |
14 ms |
12288 KB |
Output is correct |
26 |
Correct |
12 ms |
12288 KB |
Output is correct |
27 |
Correct |
13 ms |
12288 KB |
Output is correct |
28 |
Correct |
12 ms |
12288 KB |
Output is correct |
29 |
Correct |
12 ms |
12288 KB |
Output is correct |
30 |
Correct |
11 ms |
12288 KB |
Output is correct |
31 |
Correct |
11 ms |
12160 KB |
Output is correct |
32 |
Correct |
13 ms |
12408 KB |
Output is correct |
33 |
Correct |
12 ms |
12416 KB |
Output is correct |
34 |
Correct |
12 ms |
12288 KB |
Output is correct |
35 |
Correct |
11 ms |
12160 KB |
Output is correct |
36 |
Correct |
12 ms |
12416 KB |
Output is correct |
37 |
Correct |
11 ms |
12288 KB |
Output is correct |
38 |
Correct |
11 ms |
12288 KB |
Output is correct |
39 |
Correct |
13 ms |
12288 KB |
Output is correct |
40 |
Correct |
12 ms |
12288 KB |
Output is correct |
41 |
Correct |
11 ms |
12288 KB |
Output is correct |
42 |
Correct |
13 ms |
12288 KB |
Output is correct |
43 |
Correct |
11 ms |
12288 KB |
Output is correct |
44 |
Correct |
8 ms |
12160 KB |
Output is correct |
45 |
Correct |
8 ms |
12032 KB |
Output is correct |
46 |
Correct |
237 ms |
21100 KB |
Output is correct |
47 |
Correct |
236 ms |
20968 KB |
Output is correct |
48 |
Correct |
216 ms |
21104 KB |
Output is correct |
49 |
Correct |
212 ms |
21152 KB |
Output is correct |
50 |
Correct |
229 ms |
21004 KB |
Output is correct |
51 |
Correct |
216 ms |
20988 KB |
Output is correct |
52 |
Correct |
213 ms |
22648 KB |
Output is correct |
53 |
Correct |
221 ms |
22776 KB |
Output is correct |
54 |
Correct |
194 ms |
22776 KB |
Output is correct |
55 |
Correct |
163 ms |
22776 KB |
Output is correct |
56 |
Correct |
188 ms |
22648 KB |
Output is correct |
57 |
Correct |
198 ms |
22776 KB |
Output is correct |
58 |
Correct |
217 ms |
20856 KB |
Output is correct |
59 |
Correct |
211 ms |
20728 KB |
Output is correct |
60 |
Correct |
207 ms |
20728 KB |
Output is correct |
61 |
Correct |
193 ms |
20728 KB |
Output is correct |
62 |
Correct |
166 ms |
20856 KB |
Output is correct |
63 |
Correct |
190 ms |
20856 KB |
Output is correct |
64 |
Correct |
223 ms |
20728 KB |
Output is correct |
65 |
Correct |
222 ms |
20728 KB |
Output is correct |
66 |
Correct |
211 ms |
20728 KB |
Output is correct |
67 |
Correct |
193 ms |
20732 KB |
Output is correct |
68 |
Correct |
204 ms |
20728 KB |
Output is correct |
69 |
Correct |
218 ms |
20728 KB |
Output is correct |
70 |
Correct |
219 ms |
20888 KB |
Output is correct |
71 |
Correct |
222 ms |
20980 KB |
Output is correct |
72 |
Correct |
219 ms |
20872 KB |
Output is correct |
73 |
Correct |
213 ms |
20852 KB |
Output is correct |
74 |
Correct |
183 ms |
20852 KB |
Output is correct |
75 |
Correct |
219 ms |
20980 KB |
Output is correct |
76 |
Correct |
185 ms |
18808 KB |
Output is correct |
77 |
Correct |
177 ms |
20016 KB |
Output is correct |
78 |
Correct |
159 ms |
22648 KB |
Output is correct |
79 |
Correct |
164 ms |
20732 KB |
Output is correct |
80 |
Correct |
186 ms |
18680 KB |
Output is correct |
81 |
Correct |
178 ms |
19980 KB |
Output is correct |
82 |
Correct |
163 ms |
22776 KB |
Output is correct |
83 |
Correct |
162 ms |
20796 KB |
Output is correct |
84 |
Correct |
240 ms |
20976 KB |
Output is correct |
85 |
Correct |
237 ms |
20976 KB |
Output is correct |
86 |
Correct |
231 ms |
20976 KB |
Output is correct |
87 |
Correct |
193 ms |
18936 KB |
Output is correct |
88 |
Correct |
239 ms |
20972 KB |
Output is correct |