Submission #515705

#TimeUsernameProblemLanguageResultExecution timeMemory
515705cadmiumskyCheerleaders (info1cup20_cheerleaders)C++14
84 / 100
555 ms12208 KiB
#include <bits/stdc++.h>

using namespace std;
#define ll long long

int n;
int bit;
int v[(1<<17) + 5];
ll freq[(1<<17) + 5];
int inf;
static ll solve(vector<int> p, int crit = 0, int b = bit - 1) {
  //cout << b << '\n';
  if(b <= -1)
    return 0;
  ll total[2] = {0, 0};
  int temp = crit;
  while(temp) {
    freq[temp] = 0;
    temp = (temp - 1) & crit;
  }
  freq[0] = 0;
  for(auto x : p) {
    if((x & (1 << b)))
      freq[x & crit]++;
    else
      total[0] += freq[x & crit];
    //cout << (x&crit) <<  ' '<< (x&(1<<b)) <<" / ";
  }
  //cout << '\n';
  temp = crit;
  while(temp) {
    freq[temp] = 0;
    temp = (temp - 1) & crit;
  }
  freq[0] = 0;
  for(auto x : p) {
    if(!(x & (1 << b)))
      freq[x & crit]++;
    else
      total[1] += freq[x & crit];
  }
  if(total[0] >= total[1])
    inf |= (1 << b);
  //cout << total[0] << ' ' << total[1] << '\n';
  return min(total[0], total[1]) + solve(p, crit | (1 << b), b - 1);
}

int main() {
  cin >> bit;
  n = (1 << bit);
  for(int i = 0, x; i < n; i++) {
    cin >> x;
    v[x] = i;
  }
  ll minn = (1LL << 35);
  int sol;
  string pref, current = "";
  for(int fix = bit - 1; fix >= 0; fix--) {
    vector<int> temp(n, 0);
    for(int i = 0; i < n; i++)
      temp[i] = v[i];
    inf = 0;
    ll total = solve(temp);
    if(minn > total) {
      minn = total;
      pref = current;
      sol = inf;
    }
    for(int i = 0; i < n; i++)
      v[i] = ((v[i] >> 1) | ((v[i] & 1) << bit - 1));
    current += "2";
  }
  cout << minn << '\n';
  cout << pref;
  if(sol & (1 << bit - 1))
    cout << 1;
  cout << 2;
  for(int i = 0; i < bit - 1; i++) {
    if(sol & (1 << i))
      cout << 1;
    cout << 2;
  }
  cout << '\n';
}

Compilation message (stderr)

cheerleaders.cpp: In function 'int main()':
cheerleaders.cpp:70:48: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   70 |       v[i] = ((v[i] >> 1) | ((v[i] & 1) << bit - 1));
      |                                            ~~~~^~~
cheerleaders.cpp:75:22: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   75 |   if(sol & (1 << bit - 1))
      |                  ~~~~^~~
cheerleaders.cpp:79:12: warning: 'sol' may be used uninitialized in this function [-Wmaybe-uninitialized]
   79 |     if(sol & (1 << 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...