-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcf492a.cpp
More file actions
129 lines (119 loc) · 3.09 KB
/
Copy pathcf492a.cpp
File metadata and controls
129 lines (119 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Debug(x) cout << "> " << #x << " : " << x << "\n";
#define DebugArr(a,n) For(i,0,n) cout << i << " : " << a[i] << "\n";
#define For(i,a,b) for(int i=(a);i<(b);i++)
#define ForD(i,a,b) for(int i=(a);i>(b);i--)
#define tests int t; cin>>t; while(t--)
#define Imax INT_MAX
#define Imin INT_MIN
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define sz(a) int((a).size())
#define pb push_back
#define it iterator
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define mod 1000000007
/////////////////////////////////////////////////////////////////////
int d[2000] = {0};
int a[2000];
int pos[2000][2];
int wrong[2000];
int tw;
void dasgn(int i){
int x = a[i];
int othr;
if(pos[x][0] == i){
othr = pos[x][1];
}else othr = pos[x][0];
if(abs(othr-i) == 1){
d[othr] = 0;
d[i] = 0;
if(wrong[i] == 1) tw--;
wrong[i] = 0;
}else{
if(wrong[i] == 0) tw++;
wrong[i] = 1;
if(othr > i){
d[othr] = -1;
d[i] = 1;
}else{
//othr ...........i
d[othr] = 1;
d[i] = -1;
}
}
// Debug(x)
// Debug(d[i])
// Debug(d[othr])
}
void pupd(int x, int opos, int npos){
if(pos[x][0] == opos){
pos[x][0] = npos;
}else pos[x][1] = npos;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int n;cin >> n;
tw = n;
// int a[2*n];
For(i,0,2*n) cin >> a[i];
// int pos[n][2];
For(i,0,n+1){
pos[i][0] = -1;
pos[i][1] = -1;
}
For(i,0,2*n){
if(pos[a[i]][0] == -1){
pos[a[i]][0] = i;
}else pos[a[i]][1] = i;
}
For(i,0,2*n){
dasgn(i);
}
// Debug(d[0])
// Debug(d[1])
int chk = 0;int y,z;
long long int ans = 0;
while(chk < 10000 && tw != 0){
chk++;
For(i,0,2*n-1){
y = a[i];
if(d[i] == 0) continue;
if(d[i] == 1 && (d[i+1] == -1 || d[i+1] == 0)){
z = a[i+1];
// Debug(y) Debug(z)
// Debug(d[i]) Debug(d[i+1])
pupd(y,i,i+1);
pupd(z,i+1,i);
swap(a[i],a[i+1]);
ans++;
dasgn(i);
dasgn(i+1);
}
for(int i = 2*n-1;i>=1;i--){
y = a[i];
if(d[i] == 0) continue;
if(d[i] == -1 && (d[i-1] == 1 || d[i-1] == 0)){
z = a[i-1];
// Debug(y) Debug(z)
// Debug(d[i]) Debug(d[i-1])
pupd(y,i,i-1);
pupd(z,i-1,i);
swap(a[i],a[i-1]);
ans++;
dasgn(i);
dasgn(i-1);
// Debug(y) Debug(z)
}
}
cout << ans;
return 0;
}