//!yrt tsuj uoy srettam gnihton no emoc
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define pii pair <int, int>
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define F first
#define S second
const int maxn = 5e5 + 5;
struct edge{
int c, l, r, u, v;
} ed[maxn];
int t[5];
vector <int> vec[maxn];
bool mark[maxn];
int cnt;
int find_mx(){
pii v[] = {{t[0], 0}, {t[1], 1}, {t[2], 2}};
sort(v, v + 3);
return v[2].S;
}
void upd(int le, int x){
ed[cnt].c = 2 * (ed[x].c + ed[le].c) % 3;
ed[cnt].l = ed[le].l;
ed[cnt].r = ed[x].r;
ed[cnt].u = ed[le].u;
ed[cnt].v = ed[x].v;
ed[ed[cnt].l].r = cnt;
ed[ed[cnt].r].l = cnt;
cout << ed[cnt].u + 1 << " " << ed[cnt].v + 1 << " " << ed[cnt].c + 1 << endl;
mark[x] = mark[le] = true;
vec[ed[cnt].c].pb(cnt);
vec[ed[ed[cnt].l].c].pb(ed[cnt].l);
vec[ed[ed[cnt].r].c].pb(ed[cnt].r);
t[ed[cnt].c] ++;
t[ed[le].c] --;
t[ed[x].c] --;
cnt ++;
return;
}
bool check(int x){
if(mark[x]){
return false;
}
int le = ed[x].l;
int ri = ed[x].r;
if(ed[x].c != ed[le].c && !mark[le]){
upd(le, x);
return true;
}
if(ed[x].c != ed[ri].c && !mark[ri]){
upd(x, ri);
return true;
}
return false;
}
void solve(int n){
if(n == 3){
return;
}
int mx = find_mx();
bool f = true;
while(f){
int x = vec[mx].back();
vec[mx].pop_back();
if(check(x)){
f = false;
}
}
solve(n - 1);
}
int main(){
fast_io;
int n;
cin >> n;
string s; cin >> s;
for(int i = 0; i < n; i ++){
ed[i].c = (s[i] - '0') - 1;
ed[i].u = i;
ed[i].v = (i + 1) % n;
ed[i].l = (i - 1 + n) % n;
ed[i].r = (i + 1) % n;
t[ed[i].c] ++;
vec[ed[i].c].pb(i);
}
for(int i = 0; i < 3; i ++){
if((t[i] & 1) != (n & 1) || t[i] == n){
cout << "NE";
return 0;
}
}
cnt = n;
cout << "DA" << endl;
solve(n);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Correct |
8 ms |
11980 KB |
Output is correct |
4 |
Correct |
8 ms |
11980 KB |
Output is correct |
5 |
Correct |
8 ms |
11980 KB |
Output is correct |
6 |
Correct |
8 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
12004 KB |
Output is correct |
8 |
Correct |
8 ms |
11980 KB |
Output is correct |
9 |
Correct |
10 ms |
12064 KB |
Output is correct |
10 |
Correct |
8 ms |
11980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Correct |
8 ms |
11980 KB |
Output is correct |
4 |
Correct |
8 ms |
11980 KB |
Output is correct |
5 |
Correct |
8 ms |
11980 KB |
Output is correct |
6 |
Correct |
8 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
12004 KB |
Output is correct |
8 |
Correct |
8 ms |
11980 KB |
Output is correct |
9 |
Correct |
10 ms |
12064 KB |
Output is correct |
10 |
Correct |
8 ms |
11980 KB |
Output is correct |
11 |
Correct |
8 ms |
12072 KB |
Output is correct |
12 |
Correct |
8 ms |
11980 KB |
Output is correct |
13 |
Correct |
13 ms |
12108 KB |
Output is correct |
14 |
Correct |
9 ms |
11952 KB |
Output is correct |
15 |
Correct |
8 ms |
11980 KB |
Output is correct |
16 |
Correct |
10 ms |
12044 KB |
Output is correct |
17 |
Correct |
9 ms |
12056 KB |
Output is correct |
18 |
Correct |
12 ms |
12108 KB |
Output is correct |
19 |
Correct |
8 ms |
11980 KB |
Output is correct |
20 |
Correct |
10 ms |
12108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
3 |
Correct |
8 ms |
11980 KB |
Output is correct |
4 |
Correct |
8 ms |
11980 KB |
Output is correct |
5 |
Correct |
8 ms |
11980 KB |
Output is correct |
6 |
Correct |
8 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
12004 KB |
Output is correct |
8 |
Correct |
8 ms |
11980 KB |
Output is correct |
9 |
Correct |
10 ms |
12064 KB |
Output is correct |
10 |
Correct |
8 ms |
11980 KB |
Output is correct |
11 |
Correct |
8 ms |
12072 KB |
Output is correct |
12 |
Correct |
8 ms |
11980 KB |
Output is correct |
13 |
Correct |
13 ms |
12108 KB |
Output is correct |
14 |
Correct |
9 ms |
11952 KB |
Output is correct |
15 |
Correct |
8 ms |
11980 KB |
Output is correct |
16 |
Correct |
10 ms |
12044 KB |
Output is correct |
17 |
Correct |
9 ms |
12056 KB |
Output is correct |
18 |
Correct |
12 ms |
12108 KB |
Output is correct |
19 |
Correct |
8 ms |
11980 KB |
Output is correct |
20 |
Correct |
10 ms |
12108 KB |
Output is correct |
21 |
Correct |
414 ms |
26236 KB |
Output is correct |
22 |
Correct |
408 ms |
25824 KB |
Output is correct |
23 |
Correct |
9 ms |
11980 KB |
Output is correct |
24 |
Correct |
8 ms |
12016 KB |
Output is correct |
25 |
Correct |
396 ms |
25016 KB |
Output is correct |
26 |
Correct |
8 ms |
12072 KB |
Output is correct |
27 |
Correct |
209 ms |
18384 KB |
Output is correct |
28 |
Correct |
14 ms |
17492 KB |
Output is correct |
29 |
Correct |
8 ms |
11980 KB |
Output is correct |
30 |
Correct |
13 ms |
17200 KB |
Output is correct |