#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
typedef long double ld;
const int maxn = 105;
const int mod = 1e9+7;
int n;
vector<int> fg[maxn], rfg[maxn];
int g[maxn][maxn];
int vis[maxn], pos[maxn], qry[maxn], p[maxn], deg[maxn], rdeg[maxn], fdeg[maxn], ans[maxn]; ;
vector<int> vec;
set<int> st;
void dfs(int x, int l, int r){
vis[x]=1;
for(int i = l; i < x; i++){
if(vis[i] || !g[i][x])continue;
dfs(i, l, r);
}
}
bool ask(){
cout << "query ";
for(int i = 1; i <= n; i++)cout << qry[i] << " ";
cout << endl;
bool x;
cin >> x;
cout << endl;
return !x;
}
void fe(int l, int r){
for(int i = 1; i <= n; i++)vis[i]=fdeg[i]=0;
vec.clear();
dfs(r, l, r);
if(vis[l])return;
deque<int> dq;
vector<int> sus;
for(int i = l; i <= r; i++){
for(int j = l; j < i; j++)if(g[j][i])fdeg[i]++;
if(!fdeg[i]){
if(vis[i])dq.push_front(i);
else dq.push_back(i);
}
}
while(!dq.empty()){
auto x = dq.front();
dq.pop_front();
sus.push_back(x);
for(int i = x+1; i <= r; i++)if(g[x][i]){
fdeg[i]--;
if(!fdeg[i]){
if(vis[i])dq.push_front(i);
else dq.push_back(i);
}
}
}
for(int i = 1; i <= n; i++)qry[i]=p[i];
for(int i = l; i <= r; i++){
qry[pos[sus[i-l]]]=i;
}
if(ask()){
g[l][r]=1;
g[r][l]=1;
fg[pos[r]].push_back(pos[l]);
deg[pos[l]]++;
rfg[pos[l]].push_back(pos[r]);
rdeg[pos[r]]++;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> p[i];
pos[p[i]]=i;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j+i <= n; j++){
fe(j, i+j);
}
}
cout << "end" << endl;
for(int i = 1; i <= n; i++)if(!deg[i])st.insert(i);
for(int i = n; i > 0; i--){
auto x = *prev(st.end());
st.erase(x);
ans[x]=i;
for(auto v: fg[x]){
deg[v]--;
if(!deg[v])st.insert(v);
}
}
for(int i = 1; i <= n; i++)cout << ans[i] << " ";
cout << endl;
for(int i = 1; i <= n; i++){
if(!rdeg[i])st.insert(i);
}
for(int i = 1; i <= n; i++){
auto x = *prev(st.end());
st.erase(x);
ans[x]=i;
for(auto v: rfg[x]){
rdeg[v]--;
if(!rdeg[v])st.insert(v);
}
}
for(int i = 1; i <= n; i++)cout << ans[i] << " ";
cout << endl;
}
/*
1 3 5 4 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
336 KB |
Output is correct |
2 |
Correct |
33 ms |
312 KB |
Output is correct |
3 |
Correct |
40 ms |
308 KB |
Output is correct |
4 |
Correct |
50 ms |
208 KB |
Output is correct |
5 |
Correct |
10 ms |
312 KB |
Output is correct |
6 |
Correct |
38 ms |
304 KB |
Output is correct |
7 |
Correct |
7 ms |
332 KB |
Output is correct |
8 |
Correct |
5 ms |
340 KB |
Output is correct |
9 |
Correct |
23 ms |
324 KB |
Output is correct |
10 |
Correct |
10 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
6 ms |
208 KB |
Output is correct |
4 |
Correct |
5 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
336 KB |
Output is correct |
8 |
Correct |
6 ms |
208 KB |
Output is correct |
9 |
Correct |
6 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
336 KB |
Output is correct |
11 |
Correct |
5 ms |
208 KB |
Output is correct |
12 |
Correct |
5 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
120 ms |
436 KB |
Output is correct |
2 |
Correct |
96 ms |
316 KB |
Output is correct |
3 |
Correct |
92 ms |
316 KB |
Output is correct |
4 |
Correct |
10 ms |
336 KB |
Output is correct |
5 |
Correct |
15 ms |
336 KB |
Output is correct |
6 |
Correct |
13 ms |
368 KB |
Output is correct |
7 |
Correct |
20 ms |
336 KB |
Output is correct |
8 |
Correct |
38 ms |
344 KB |
Output is correct |
9 |
Correct |
25 ms |
364 KB |
Output is correct |
10 |
Correct |
108 ms |
336 KB |
Output is correct |
11 |
Correct |
83 ms |
344 KB |
Output is correct |
12 |
Correct |
87 ms |
332 KB |
Output is correct |
13 |
Correct |
70 ms |
476 KB |
Output is correct |
14 |
Correct |
101 ms |
344 KB |
Output is correct |
15 |
Correct |
102 ms |
448 KB |
Output is correct |
16 |
Correct |
91 ms |
360 KB |
Output is correct |