Submission #258599

# Submission time Handle Problem Language Result Execution time Memory
258599 2020-08-06T08:05:46 Z IOrtroiii Swap (BOI16_swap) C++14
100 / 100
62 ms 5628 KB
#include <bits/stdc++.h>

using namespace std;

const int MX = 400400;

int N;
int A[MX];
int state[MX];
int ans[MX];

int query(int i) {
   int ans = MX;
   for (; i > 0; i /= 2) {
      if (state[i] != 1) ans = min(ans, A[i]);
      if (state[i] == 0) return ans;
      if (i % 2 && state[i - 1] != 0) {
         ans = min(ans, A[i - 1]);
         if (state[i - 1] == 1) return ans;
      }
   }
}

void modify(int i, int val) {
   for (; i > 0; i /= 2) {
      if (A[i] == val) {
         state[i] = 0;
         return;
      }
      if (i % 2) {
         if (A[i - 1] == val) {
            state[i] = state[i - 1] = 1;
            return;
         } else state[i - 1] = 0;
      }
      state[i] = 1;
   }
}

int main() {
   ios_base::sync_with_stdio(false); cin.tie(nullptr);
   cin >> N;
   for (int i = 1; i <= N; ++i) cin >> A[i];
   for (int i = N + 1; i <= 2 * N + 1; ++i) A[i] = MX;
   state[1] = 0; for (int i = 2; i <= 2 * N + 1; ++i) state[i] = -1;
   for (int i = 1; i <= N; ++i) {
      int val = query(i);
      ans[i] = min({val, A[i * 2], A[i * 2 + 1]});
      if (ans[i] == val) {
         modify(i, val);
         state[i * 2] = 0;
         state[i * 2 + 1] = 0;
      } else if (ans[i] == A[i * 2]) {
         state[i * 2] = 1;
         state[i * 2 + 1] = 0;
      } else {
         assert(ans[i] == A[i * 2 + 1]);
         state[i * 2 + 1] = 1;
      }
   }
   for (int i = 1; i <= N; ++i) cout << ans[i] << ' '; cout << '\n';
   return 0;
}

Compilation message

swap.cpp: In function 'int main()':
swap.cpp:61:4: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
    for (int i = 1; i <= N; ++i) cout << ans[i] << ' '; cout << '\n';
    ^~~
swap.cpp:61:56: 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 << ans[i] << ' '; cout << '\n';
                                                        ^~~~
swap.cpp: In function 'int query(int)':
swap.cpp:22:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 12 ms 1664 KB Output is correct
17 Correct 12 ms 1664 KB Output is correct
18 Correct 12 ms 1664 KB Output is correct
19 Correct 18 ms 1664 KB Output is correct
20 Correct 14 ms 1664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 12 ms 1664 KB Output is correct
17 Correct 12 ms 1664 KB Output is correct
18 Correct 12 ms 1664 KB Output is correct
19 Correct 18 ms 1664 KB Output is correct
20 Correct 14 ms 1664 KB Output is correct
21 Correct 48 ms 5624 KB Output is correct
22 Correct 49 ms 5628 KB Output is correct
23 Correct 48 ms 5500 KB Output is correct
24 Correct 59 ms 5624 KB Output is correct
25 Correct 62 ms 5624 KB Output is correct