#include "shoes.h"
#include <cstdio>
#include <cassert>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<bool,bool> pbb;
typedef vector<pbb> vbb;
typedef pair<int,int> pii;
typedef vector<pii> vii;
const ll INF=1000000001;
bool samesize(vi v){
for (int x:v){
if (abs(x)!=abs(v[0])) return false;
}
return true;
}
bool caso_raro(vi v){
ll n=v.size()/2;
for(int i=0;i<n;i++){
if (v[i]>0 or v[n+i]<0 or v[i]!=-v[n+i]) return false;
}
return true;
}
ll solve1(vi v){
//cerr<<1<<'\n';
int n=v.size()/2;
ll sol=0;
ll balance=0;
for (int x:v){
if (x<0){
if (balance>0){
sol+=balance;
}
balance--;
}
else{
if (balance<-1) sol+=-balance-1;
balance++;
}
}
return sol;
}
ll solve2(vi v){
int n=v.size()/2;
ll sol=0;
for (int i=0;i<n;i++){
vbb porver(n+1,pbb{true,true});
vii dondevisto(n+1);
for (int j=2*i;j<2*n;j++){
int x=v[j];
if (x<0){
x=-x;
if (porver[x].first){
porver[x].first=false;
dondevisto[x].first=j;
}
}
else{
if (porver[x].second){
porver[x].second=false;
dondevisto[x].second=j;
}
}
if (porver[x]==pbb{false,false}){
for (int h=dondevisto[x].first;h>2*i;h--){
sol++;
swap(v[h],v[h-1]);
}
if (dondevisto[x].second<dondevisto[x].first) dondevisto[x].second++;
for (int h=dondevisto[x].second;h>2*i+1;h--){
sol++;
swap(v[h],v[h-1]);
}
break;
}
}
}
return sol;
}
ll solve3(vi v){
ll n=v.size()/2;
return n*(n-1)/2;
}
long long debug(std::vector<int> v) {
cerr << "begin\n";
int t=v[0];
int n=v[1];
while (t--){
vi v(2*n,1);
int counter=n;
while (counter){
int x=rand()%(2*n);
if (v[x]==1){
v[x]=-1;
counter--;
}
}
if (solve1(v)!=solve2(v)){
for (int x:v) cerr << x << ' ';
cerr<<'\n';
cerr<< solve1(v) << ' '<<solve2(v)<<'\n';
}
}
cerr << "end\n";
return 1;
}
long long count_swaps(vi v){
if (samesize(v)) return solve1(v);
else if (caso_raro(v)) return solve3(v);
else return solve2(v);
}
Compilation message
shoes.cpp: In function 'll solve1(vi)':
shoes.cpp:35:6: warning: unused variable 'n' [-Wunused-variable]
35 | int n=v.size()/2;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
248 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
3 ms |
460 KB |
Output is correct |
21 |
Correct |
3 ms |
460 KB |
Output is correct |
22 |
Correct |
27 ms |
2620 KB |
Output is correct |
23 |
Correct |
36 ms |
2628 KB |
Output is correct |
24 |
Correct |
26 ms |
2628 KB |
Output is correct |
25 |
Correct |
27 ms |
2636 KB |
Output is correct |
26 |
Correct |
26 ms |
2620 KB |
Output is correct |
27 |
Correct |
25 ms |
2624 KB |
Output is correct |
28 |
Correct |
23 ms |
2620 KB |
Output is correct |
29 |
Correct |
23 ms |
2620 KB |
Output is correct |
30 |
Correct |
23 ms |
2636 KB |
Output is correct |
31 |
Correct |
27 ms |
2624 KB |
Output is correct |
32 |
Correct |
27 ms |
2612 KB |
Output is correct |
33 |
Correct |
31 ms |
2612 KB |
Output is correct |
34 |
Correct |
24 ms |
2624 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
33 ms |
2608 KB |
Output is correct |
38 |
Correct |
24 ms |
2620 KB |
Output is correct |
39 |
Correct |
23 ms |
2616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
23 ms |
2620 KB |
Output is correct |
6 |
Correct |
23 ms |
2636 KB |
Output is correct |
7 |
Correct |
30 ms |
2620 KB |
Output is correct |
8 |
Correct |
27 ms |
2604 KB |
Output is correct |
9 |
Correct |
23 ms |
2616 KB |
Output is correct |
10 |
Correct |
24 ms |
2628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
248 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
5 ms |
204 KB |
Output is correct |
42 |
Correct |
3 ms |
204 KB |
Output is correct |
43 |
Correct |
3 ms |
204 KB |
Output is correct |
44 |
Correct |
7 ms |
204 KB |
Output is correct |
45 |
Correct |
3 ms |
204 KB |
Output is correct |
46 |
Correct |
3 ms |
204 KB |
Output is correct |
47 |
Correct |
5 ms |
316 KB |
Output is correct |
48 |
Correct |
6 ms |
328 KB |
Output is correct |
49 |
Correct |
5 ms |
204 KB |
Output is correct |
50 |
Correct |
4 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
6 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
248 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
0 ms |
204 KB |
Output is correct |
45 |
Correct |
0 ms |
204 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
1 ms |
204 KB |
Output is correct |
53 |
Correct |
1 ms |
204 KB |
Output is correct |
54 |
Correct |
3 ms |
460 KB |
Output is correct |
55 |
Correct |
3 ms |
460 KB |
Output is correct |
56 |
Correct |
27 ms |
2620 KB |
Output is correct |
57 |
Correct |
36 ms |
2628 KB |
Output is correct |
58 |
Correct |
26 ms |
2628 KB |
Output is correct |
59 |
Correct |
27 ms |
2636 KB |
Output is correct |
60 |
Correct |
26 ms |
2620 KB |
Output is correct |
61 |
Correct |
25 ms |
2624 KB |
Output is correct |
62 |
Correct |
23 ms |
2620 KB |
Output is correct |
63 |
Correct |
23 ms |
2620 KB |
Output is correct |
64 |
Correct |
23 ms |
2636 KB |
Output is correct |
65 |
Correct |
27 ms |
2624 KB |
Output is correct |
66 |
Correct |
27 ms |
2612 KB |
Output is correct |
67 |
Correct |
31 ms |
2612 KB |
Output is correct |
68 |
Correct |
24 ms |
2624 KB |
Output is correct |
69 |
Correct |
0 ms |
204 KB |
Output is correct |
70 |
Correct |
0 ms |
204 KB |
Output is correct |
71 |
Correct |
33 ms |
2608 KB |
Output is correct |
72 |
Correct |
24 ms |
2620 KB |
Output is correct |
73 |
Correct |
23 ms |
2616 KB |
Output is correct |
74 |
Correct |
0 ms |
204 KB |
Output is correct |
75 |
Correct |
0 ms |
204 KB |
Output is correct |
76 |
Correct |
0 ms |
204 KB |
Output is correct |
77 |
Correct |
0 ms |
204 KB |
Output is correct |
78 |
Correct |
23 ms |
2620 KB |
Output is correct |
79 |
Correct |
23 ms |
2636 KB |
Output is correct |
80 |
Correct |
30 ms |
2620 KB |
Output is correct |
81 |
Correct |
27 ms |
2604 KB |
Output is correct |
82 |
Correct |
23 ms |
2616 KB |
Output is correct |
83 |
Correct |
24 ms |
2628 KB |
Output is correct |
84 |
Correct |
0 ms |
204 KB |
Output is correct |
85 |
Correct |
0 ms |
204 KB |
Output is correct |
86 |
Correct |
1 ms |
204 KB |
Output is correct |
87 |
Correct |
1 ms |
204 KB |
Output is correct |
88 |
Correct |
5 ms |
204 KB |
Output is correct |
89 |
Correct |
3 ms |
204 KB |
Output is correct |
90 |
Correct |
3 ms |
204 KB |
Output is correct |
91 |
Correct |
7 ms |
204 KB |
Output is correct |
92 |
Correct |
3 ms |
204 KB |
Output is correct |
93 |
Correct |
3 ms |
204 KB |
Output is correct |
94 |
Correct |
5 ms |
316 KB |
Output is correct |
95 |
Correct |
6 ms |
328 KB |
Output is correct |
96 |
Correct |
5 ms |
204 KB |
Output is correct |
97 |
Correct |
4 ms |
204 KB |
Output is correct |
98 |
Correct |
1 ms |
204 KB |
Output is correct |
99 |
Correct |
6 ms |
204 KB |
Output is correct |
100 |
Correct |
5 ms |
204 KB |
Output is correct |
101 |
Correct |
396 ms |
608 KB |
Output is correct |
102 |
Correct |
300 ms |
596 KB |
Output is correct |
103 |
Execution timed out |
1081 ms |
3508 KB |
Time limit exceeded |
104 |
Halted |
0 ms |
0 KB |
- |