#include <cstdio>
#include <vector>
#include <set>
#include "library.h"
#include <cstring>
#include <cassert>
#include <algorithm>
#ifdef atom
#include "grader.cpp"
#endif
using namespace std;
struct cc
{
vector<int> mem;
};
vector< cc > comps;
vector<int> input;
int n;
void clear()
{
input.assign(n, 0);
}
void insert(cc x)
{
for(auto k : x.mem) input[k-1] = 1;
}
int ask()
{
return Query(input);
}
int dx(int from, int to, int work)
{
int bf = to-from+1;
clear();
for(int i = from; i<= to; i++) insert(comps[i]);
input[work-1] = 1;
int res = ask();
return res-bf;
}
void join(int index, int work)
{
cc &here = comps[index];
clear();
input[here.mem[0]-1] = 1; input[work-1] = 1;
int res = ask();
if(res == 1)
{
//printf("this case\n");
//work here
here.mem.insert(here.mem.begin(), work);
}
else
{
//here work
here.mem.push_back(work);
}
}
void join2(int id1, int id2, int work)
{
cc cc1 = comps[id1], cc2 = comps[id2];
clear();
input[cc1.mem[0]-1] = 1; input[work-1] = 1;
int res = ask();
clear();
input[cc2.mem[0]-1] = 1; input[work-1] = 1;
int res2 = ask();
cc nouv; nouv.mem = cc1.mem;
if(res == 1) reverse(nouv.mem.begin(), nouv.mem.end());
nouv.mem.push_back(work);
if(res2 == 1)
{
//1cc work cc2
nouv.mem.insert(nouv.mem.end(), cc2.mem.begin(), cc2.mem.end());
}
else
{
//1cc work 2cc
nouv.mem.insert(nouv.mem.end(), cc2.mem.rbegin(), cc2.mem.rend());
}
if(id1> id2) swap(id1, id2);
comps.erase(comps.begin()+id2);
comps.erase(comps.begin()+id1);
comps.push_back(nouv);
}
int lookfor(int from, int to, int work)
{
if(from == to) return from;
int mid = (from+to)/2;
int res = dx(from, mid, work);
if(res == 0) return lookfor(from, mid, work);
return lookfor(mid+1, to, work);
}
#define ii pair<int, int>
ii lookfor2(int from, int to, int work)
{
if(from+1 == to) return ii(from, to);
int mid = (from+to)/2;
int res = dx(from, mid, work);
if(res == -1) return lookfor2(from, mid, work);
if(res == 0) return ii(lookfor(from, mid, work), lookfor(mid+1, to, work));
return lookfor2(mid+1, to, work);
}
void Solve(int N)
{
n = N;
input.resize(N);
cc one; one.mem.push_back(1);
comps.push_back(one);
for(int i = 2; i<= N; i++)
{
int res = dx(0, comps.size()-1, i);
//printf("%d: %d\n", i, res);
if(res == 1)
{
cc nouvelle; nouvelle.mem.push_back(i);
comps.push_back(nouvelle);
//printf("lone component %d\n", comps.size()-1);
}
else if(res == 0)
{
int k = lookfor(0, comps.size()-1, i);
//printf("attach with %d\n", k);
join(k, i);
}
else
{
ii tt = lookfor2(0, comps.size()-1, i);
// printf("joined %d %d\n", tt.first, tt.second);
join2(tt.first, tt.second, i);
}
//printf("now %d comps\n", comps.size());
// for(int i = 0; i< (int) comps.size(); i++)
// {
// printf("comp %d: ", i);
// for(auto x : comps[i].mem) printf("%d ", x);
// printf("\n");
// }
}
//for(int i = 1; i<= n; i++) printf("%d %d\n", lhs[i], rhs[i]);
//for(auto x : res) printf("%d ", x);
Answer(comps[0].mem);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
376 KB |
Output is correct |
2 |
Correct |
20 ms |
464 KB |
Output is correct |
3 |
Correct |
16 ms |
464 KB |
Output is correct |
4 |
Correct |
20 ms |
464 KB |
Output is correct |
5 |
Correct |
22 ms |
472 KB |
Output is correct |
6 |
Correct |
22 ms |
648 KB |
Output is correct |
7 |
Correct |
22 ms |
648 KB |
Output is correct |
8 |
Correct |
21 ms |
648 KB |
Output is correct |
9 |
Correct |
20 ms |
648 KB |
Output is correct |
10 |
Correct |
13 ms |
648 KB |
Output is correct |
11 |
Correct |
2 ms |
648 KB |
Output is correct |
12 |
Correct |
2 ms |
648 KB |
Output is correct |
13 |
Correct |
2 ms |
648 KB |
Output is correct |
14 |
Correct |
2 ms |
648 KB |
Output is correct |
15 |
Correct |
2 ms |
648 KB |
Output is correct |
16 |
Correct |
3 ms |
648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
376 KB |
Output is correct |
2 |
Correct |
20 ms |
464 KB |
Output is correct |
3 |
Correct |
16 ms |
464 KB |
Output is correct |
4 |
Correct |
20 ms |
464 KB |
Output is correct |
5 |
Correct |
22 ms |
472 KB |
Output is correct |
6 |
Correct |
22 ms |
648 KB |
Output is correct |
7 |
Correct |
22 ms |
648 KB |
Output is correct |
8 |
Correct |
21 ms |
648 KB |
Output is correct |
9 |
Correct |
20 ms |
648 KB |
Output is correct |
10 |
Correct |
13 ms |
648 KB |
Output is correct |
11 |
Correct |
2 ms |
648 KB |
Output is correct |
12 |
Correct |
2 ms |
648 KB |
Output is correct |
13 |
Correct |
2 ms |
648 KB |
Output is correct |
14 |
Correct |
2 ms |
648 KB |
Output is correct |
15 |
Correct |
2 ms |
648 KB |
Output is correct |
16 |
Correct |
3 ms |
648 KB |
Output is correct |
17 |
Correct |
227 ms |
648 KB |
Output is correct |
18 |
Correct |
232 ms |
648 KB |
Output is correct |
19 |
Correct |
216 ms |
648 KB |
Output is correct |
20 |
Correct |
206 ms |
716 KB |
Output is correct |
21 |
Correct |
206 ms |
716 KB |
Output is correct |
22 |
Correct |
238 ms |
716 KB |
Output is correct |
23 |
Correct |
197 ms |
724 KB |
Output is correct |
24 |
Correct |
70 ms |
724 KB |
Output is correct |
25 |
Correct |
226 ms |
724 KB |
Output is correct |
26 |
Correct |
203 ms |
724 KB |
Output is correct |
27 |
Correct |
80 ms |
724 KB |
Output is correct |
28 |
Correct |
53 ms |
724 KB |
Output is correct |
29 |
Correct |
53 ms |
724 KB |
Output is correct |
30 |
Correct |
51 ms |
724 KB |
Output is correct |