#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 1e2 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
ll p[N], q[N], ind[N];
vector<ll> G[N], top;
ll L, R;
int mk[N], n;
void DFS(int u){
mk[u] = 1;
for(auto adj : G[u]) if(!mk[adj] && L <= p[adj] && p[adj] <= R) DFS(adj);
top.pb(u);
}
bool Val(int x){
memset(mk, 0, sizeof mk);
top.clear();
for(int i = L; i <= R; i++){
if(!mk[ind[i]]){
DFS(ind[i]);
}
}
reverse(all(top));
memcpy(q, p, sizeof p);
//cerr << "$$ " << top.size() << '\n';
//assert(top.size() == x);
for(int i = L; i <= R; i++)
q[top[i-L]] = i;
cout << "query";
for(int i = 1; i <= n; i++) cout << " " << q[i];
cout << endl;
int res;
//res = (q[2] < q[1]) && (q[1] < q[4]) && (q[4] < q[3]);
cin >> res;
//debug(res);
return res;
}
int g[N][N];
int mx[N], mn[N];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> p[i];
ind[p[i]] = i;
}
for(int i = 2; i <= n; i++){
for(int j = i - 1; j > 0; j--){
L = j;
R = i;
G[ind[i]].pb(ind[j]);
int rs = Val(i);
G[ind[i]].pop_back();
if(!rs){
//cerr << "!! " << ind[j] << " " << ind[i] << '\n';
G[ind[j]].pb(ind[i]);
}
}
}
for(int i = 1; i <= n; i++){
for(auto x : G[i]) g[i][x] = 1;
}
vector<int> v(n);
vector<int> Mn, Mx;
iota(all(v), 1);
do {
int fl = 1;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if((v[i] > v[j]) && (g[i+1][j+1])) fl = 0;
}
}
if(fl) Mx = v;
if(fl && Mn.size() == 0) Mn = v;
} while(next_permutation(all(v)));
top.clear();
memset(mk,0,sizeof mk);
cout << "end" << endl;
for(auto al : Mn) cout << al << " ";
cout << '\n';
for(auto al : Mx) cout << al << " ";
cout << endl;
return 0;
for(int i = 1; i <= n; i++) if(!mk[i]) DFS(i);
reverse(all(top));
for(int i = 0 ; i < n; i++){
for(int j = n - 2; j >= 0; j--){
if(top[j] > top[j + 1]){
if(!g[top[j]][top[j+1]]) swap(top[j], top[j + 1]);
}
}
}
for(int i = 0; i < n; i++) mn[top[i]] = i + 1;
for(int i = 0 ; i < n; i++){
for(int j = n - 2; j >= 0; j--){
if(top[j] < top[j + 1]){
if(!g[top[j]][top[j+1]]) swap(top[j], top[j + 1]);
}
}
}
for(int i = 0; i < n; i++) mx[top[i]] = i + 1;
for(int i = 1; i <= n; i++) cout << mn[i] << " \n"[i==n];
for(int i = 1; i <= n; i++) cout << mx[i] << " \n"[i==n];
cout << flush;
return 0;
}
/*
4
3 2 1 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
432 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3043 ms |
384 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3097 ms |
400 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3092 ms |
384 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |