# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
918993 | 2024-01-31T02:31:28 Z | viwlesxq | Xylophone (JOI18_xylophone) | C++17 | 0 ms | 0 KB |
#include <bits/stdc++.h> using namespace std; //#include "grader.cpp" void solve(int n) { vector<int> a(n + 1), d(n + 1); for (int i = 1; i < n; ++i) { d[i] = query(i, i + 1); } int min = 1, hi = n; while (min + 1 < hi) { int mid = (min + hi) / 2; if ((mid + 1 < n ? query(mid, n) : d[mid]) == n - 1) min = mid; else hi = mid; } a[min] = 1; int last = min, sum = 0; bool increasing = true; map<int, bool> used; used[1] = true; for (int i = min - 1; i > 0; --i) { int add = d[i]; if (used[a[i + 1] + add] || a[i + 1] - add > n) { if (increasing) increasing = false, sum = 0; } else if (used[a[i + 1] - add] || a[i + 1] + add <= 0) { if (!increasing) increasing = true, sum = 0; } else { int diff = (i + 1 < last ? query(i, last) : d[i]); if (sum + add != diff) { last = i + 1, sum = 0; increasing ^= true; } } a[i] = a[i + 1] + (increasing ? add : -add); used[a[i]] = true; sum += add; } last = min, sum = 0; increasing = true; for (int i = min + 1; i <= n; ++i) { int add = d[i - 1]; if (used[a[i - 1] + add] || a[i - 1] - add > n) { if (increasing) increasing = false, sum = 0; } else if (used[a[i - 1] - add] || a[i - 1] + add <= 0) { if (!increasing) increasing = true, sum = 0; } else { int diff = (last + 1 < i ? query(last, i) : d[last]); if (sum + add != diff) { last = i - 1, sum = 0; increasing ^= true; } } a[i] = a[i - 1] + (increasing ? add : -add); used[a[i]] = true; sum += add; } for (int i = 1; i <= n; ++i) { cout << a[i] << ' '; answer(i, a[i]); } }