Submission #258753

#TimeUsernameProblemLanguageResultExecution timeMemory
258753atoizSwap (BOI16_swap)C++14
100 / 100
60 ms5264 KiB
#include <iostream> #include <vector> #include <algorithm> #include <cassert> using namespace std; const int MAXN = 200007; int N, A[MAXN * 2], B[MAXN]; int getMin(int i) { if (B[i] == 0) return A[i]; int ans; if ((i & 1)) { if (B[i ^ 1] == 1) ans = A[i ^ 1]; else if (B[i ^ 1] == -1) ans = min(A[i ^ 1], getMin(i >> 1)); else ans = getMin(i >> 1); } else { ans = getMin(i >> 1); } if (B[i] == -1) ans = min(ans, A[i]); return ans; } void retrieve(int i, int val) { if (B[i] == 0) { assert(A[i] == val); return; } if (A[i] == val) { assert(B[i] == -1); B[i] = 0; return; } B[i] = 1; if ((i & 1)) { if (B[i ^ 1] == 1) assert(A[i ^ 1] == val); else if (B[i ^ 1] == -1) { B[i ^ 1] = (A[i ^ 1] == val); if (!B[i ^ 1]) retrieve(i >> 1, val); } else retrieve(i >> 1, val); } else retrieve(i >> 1, val); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N; for (int i = 1; i <= N; ++i) cin >> A[i]; for (int i = N + 1; i <= 2 * N + 1; ++i) A[i] = MAXN; for (int i = 1; i <= N; ++i) { int cur = getMin(i); int minVal = min(cur, min(A[i << 1], A[i << 1 | 1])); if (cur == minVal) retrieve(i, cur); else if (A[i << 1] == minVal) { B[i << 1] = 1; } else { B[i << 1] = -1, B[i << 1 | 1] = 1; } } for (int i = 2; i <= N; ++i) if (B[i]) swap(A[i >> 1], A[i]); for (int i = 1; i <= N; ++i) cout << A[i] << ' '; cout << endl; }

Compilation message (stderr)

swap.cpp: In function 'int main()':
swap.cpp:69:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (int i = 1; i <= N; ++i) cout << A[i] << ' '; cout << endl;
  ^~~
swap.cpp:69:52: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (int i = 1; i <= N; ++i) cout << A[i] << ' '; cout << endl;
                                                    ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...