結果
問題 |
No.2577 Simple Permutation Guess
|
ユーザー |
|
提出日時 | 2023-12-06 01:59:20 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 170 ms / 2,000 ms |
コード長 | 2,089 bytes |
コンパイル時間 | 976 ms |
コンパイル使用メモリ | 99,144 KB |
実行使用メモリ | 25,452 KB |
平均クエリ数 | 248.84 |
最終ジャッジ日時 | 2024-09-27 00:57:20 |
合計ジャッジ時間 | 14,112 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 111 |
ソースコード
#include <iostream> #include <algorithm> #include <vector> using namespace std; using BI = vector<int>; constexpr int LGN = 9; BI add(const BI &a, const BI &b) { BI c(a.size()); int q = 0; for (int i = 1; i < (int)a.size(); i++) { c[i] = a[i] + b[i] + q; q = c[i] >= i; c[i] = c[i] >= i ? c[i] - i : c[i]; } return c; } BI div2(const BI &a) { BI b(a.size()); int q = 0; for (int i = (int)a.size() - 1; i >= 1; i--) { q = q * i + a[i]; b[i] = q / 2; q %= 2; } return b; } bool gr(const BI &a, const BI &b) { for (int i = (int)a.size() - 1; i >= 1; i--) { if (a[i] != b[i]) { return a[i] > b[i]; } } return false; } void add(int bit[], int n, int x, int a) { for (; x <= n; x += x & -x) { bit[x] += a; } } int qry(int bit[], int n, int v) { int x = 0, cur = 0; for (int i = LGN - 1; i >= 0; i--) { if (x + (1 << i) <= n) { if (cur + bit[x+(1<<i)] <= v) { cur += bit[x+(1<<i)]; x += 1 << i; } } } return x + 1; } void find_perm(const BI &a, int n, int p[]) { int bit[n+1]; fill(bit, bit + n + 1, 0); for (int i = 1; i <= n; i++) { add(bit, n, i, 1); } for (int i = 0; i < n; i++) { p[i] = qry(bit, n, a[n-i]); add(bit, n, p[i], -1); } } int main() { int n; cin >> n; BI l(n + 2), r(n + 2); r[n+1] = 1; BI one(n + 2); one[2] = 1; while (gr(r, add(l, one))) { BI mid = div2(add(l, r)); int q[n]; find_perm(mid, n, q); cout << "?"; for (int i = 0; i < n; i++) { cout << " " << q[i]; } cout << endl; int res; cin >> res; if (res == 1) { l = mid; } else { r = mid; } } int ans[n]; find_perm(l, n, ans); cout << "!"; for (int i = 0; i < n; i++) { cout << " " << ans[i]; } cout << endl; return 0; }