Submission #418418

#TimeUsernameProblemLanguageResultExecution timeMemory
418418dolphingarlicCheerleaders (info1cup20_cheerleaders)C++14
100 / 100
566 ms3764 KiB
#include <bits/stdc++.h> typedef long long ll; using namespace std; int n, h[1 << 17], at[1 << 17], cp[1 << 17]; int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; for (int i = 0; i < (1 << n); i++) { cin >> h[i]; at[h[i]] = i; } ll mn = LLONG_MAX; string best_seq; for (int shift = 0; shift < max(1, n); shift++) { int mask = 0; ll inv_tot = 0; for (int i = 0; i < (1 << n); i++) cp[i] = h[i]; for (int i = 0; i < n; i++) { ll inv_0 = 0, inv_1 = 0; for (int j = 0; j < (1 << n); j += 1 << i + 1) { int lptr = j, rptr = j + (1 << i), l_used = 0, r_used = 0; vector<int> sorted; while (lptr < j + (1 << i) && rptr < j + (1 << i + 1)) { if (cp[lptr] > cp[rptr]) { inv_1 += l_used; sorted.push_back(cp[rptr]); rptr++, r_used++; } else { inv_0 += r_used; sorted.push_back(cp[lptr]); lptr++, l_used++; } } while (lptr < j + (1 << i)) { inv_0 += r_used; sorted.push_back(cp[lptr]); lptr++, l_used++; } while (rptr < j + (1 << i + 1)) { inv_1 += l_used; sorted.push_back(cp[rptr]); rptr++, r_used++; } for (int k = 0; k < (1 << i + 1); k++) cp[j + k] = sorted[k]; } if (inv_0 > inv_1) mask += 1 << i; inv_tot += min(inv_0, inv_1); } if (inv_tot < mn) { string seq; for (int i = 0; i < shift; i++) seq += "2"; for (int i = 0; i < n; i++) { seq += "2"; if (mask & (1 << i)) seq += "1"; } mn = inv_tot; best_seq = seq; } for (int i = 0; i < (1 << n); i++) { at[i] = (at[i] >> 1) + ((at[i] & 1) << n - 1); h[at[i]] = i; } } cout << mn << '\n' << best_seq; return 0; }

Compilation message (stderr)

cheerleaders.cpp: In function 'int main()':
cheerleaders.cpp:24:55: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   24 |             for (int j = 0; j < (1 << n); j += 1 << i + 1) {
      |                                                     ~~^~~
cheerleaders.cpp:27:66: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   27 |                 while (lptr < j + (1 << i) && rptr < j + (1 << i + 1)) {
      |                                                                ~~^~~
cheerleaders.cpp:43:43: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   43 |                 while (rptr < j + (1 << i + 1)) {
      |                                         ~~^~~
cheerleaders.cpp:48:45: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   48 |                 for (int k = 0; k < (1 << i + 1); k++) cp[j + k] = sorted[k];
      |                                           ~~^~~
cheerleaders.cpp:67:54: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   67 |             at[i] = (at[i] >> 1) + ((at[i] & 1) << n - 1);
      |                                                    ~~^~~
#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...