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
| #include <bits/stdc++.h> #define re register
using namespace std;
mt19937 rnd(random_device{}()); const int N = 2010; int n,x,arr[N];
inline int read(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; }
inline void dfs(vector<int> v,int l,int r){ if (l == r) return arr[v.back()] = l,void(); int len = v.size(); int id = abs((int)rnd()) % len; vector<int> a,b; while (1){ printf("? %d\n",v[id]); fflush(stdout); char op[10]; scanf("%s",op); if (op[0] == '<') x--; else if (op[0] == '>') x++; else break; } arr[v[id]] = x; for (auto p:v){ if (p == v[id]) continue; printf("? %d\n",p); fflush(stdout); char op[10]; scanf("%s",op); if (op[0] == '<') a.push_back(p); else b.push_back(p); printf("? %d\n",v[id]); fflush(stdout); scanf("%s",op); } if (!a.empty()) dfs(a,l,x - 1); if (!b.empty()) dfs(b,x + 1,r); }
inline void solve(){ vector<int> a,b; int num = 1; n = read(); while (1){ puts("? 1"); fflush(stdout); char op[10]; scanf("%s",op); if (op[0] == '=') break; } for (re int i = 2;i <= n;i++){ printf("? %d\n",i); fflush(stdout); char op[10]; scanf("%s",op); if (op[0] == '<'){ num++; a.push_back(i); } else b.push_back(i); puts("? 1"); fflush(stdout); scanf("%s",op); } arr[1] = x = num; if (!a.empty()) dfs(a,1,num - 1); if (!b.empty()) dfs(b,num + 1,n); printf("! "); for (re int i = 1;i <= n;i++) printf("%d ",arr[i]); puts(""); fflush(stdout); }
int main(){ int T; T = read(); while (T--) solve(); return 0; }
|