Submission #25736

#TimeUsernameProblemLanguageResultExecution timeMemory
25736minimarioSwap (BOI16_swap)C++14
21 / 100
0 ms2412 KiB
#include <bits/stdc++.h> using namespace std; const int MAX = 100005; int sz; int ar[MAX]; map<pair<int, int>, int > memo; int dp(int n, int v) { if (memo.find({n, v}) != memo.end()) { return memo[{n, v}]; } if (2*n+1 <= sz) { if (v < ar[2*n] && v < ar[2*n+1]) { } else if (ar[2*n] < v && ar[2*n] < ar[2*n+1]) { memo[{n, v}] = dp(2*n, v); return memo[{n, v}]; } else if (ar[2*n+1] < ar[2*n] && ar[2*n+1] < v) { if (v > ar[2*n]) { if (dp(2*n, ar[2*n]) < dp(2*n+1, ar[2*n])) { memo[{n, v}] = dp(2*n+1, ar[2*n]); } else { memo[{n, v}] = dp(2*n, ar[2*n]); } } else { memo[{n, v}] = min(dp(2*n, v), dp(2*n+1, v)); } return memo[{n, v}]; } } else if (2*n == sz && v > ar[2*n]) { memo[{n, v}] = dp(2*n, v); return memo[{n, v}]; } memo[{n, v}] = n; return n; } void go(int n) { if (2*n+1 <= sz) { if (ar[n] < ar[2*n] && ar[n] < ar[2*n+1]) { } else if (ar[2*n] < ar[n] && ar[2*n] < ar[2*n+1]) { swap(ar[n], ar[2*n]); } else if (ar[2*n+1] < ar[2*n] && ar[2*n+1] < ar[n]) { int mini = min(ar[n], ar[2*n]); int maxi = max(ar[n], ar[2*n]); int pos1 = dp(2*n, mini); int pos2 = dp(2*n+1, mini); ar[n] = ar[2*n+1]; if (pos1 < pos2) { ar[2*n] = mini; ar[2*n+1] = maxi; } else { ar[2*n] = maxi; ar[2*n+1] = mini; } } go(2*n); go(2*n+1); } else if (2*n == sz && ar[n] > ar[2*n]) { swap(ar[n], ar[2*n]); } } int main(int argc, char const *argv[]) { scanf("%d", &sz); for (int i = 1; i <= sz; ++i) { scanf("%d", &ar[i]); } go(1); for (int i = 1; i <= sz; ++i) { printf("%d", ar[i]); if (i != sz) { printf(" "); } else { printf("\n"); } } return 0; }

Compilation message (stderr)

swap.cpp: In function 'int main(int, const char**)':
swap.cpp:65:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &sz);
                  ^
swap.cpp:68:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &ar[i]);
                      ^
#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...