Submission #515703

#TimeUsernameProblemLanguageResultExecution timeMemory
515703cadmiumskyCheerleaders (info1cup20_cheerleaders)C++14
46 / 100
477 ms12396 KiB
#include <bits/stdc++.h>

using namespace std;
#define ll long long

int n;
int bit;
int v[(1<<17) + 5];
int freq[(1<<17) + 5];
string 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";
  inf += "2";
  //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);
  string sol, 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 = current;
    ll total = solve(temp);
    if(minn > total) {
      minn = total;
      sol = inf;
    }
    for(int i = 0; i < n; i++)
      v[i] = ((v[i] >> 1) | ((v[i] & 1) << bit - 1));
    current += "2";
  }
  cout << minn << '\n' << sol << '\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));
      |                                            ~~~~^~~
#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...