Submission #1110786

# Submission time Handle Problem Language Result Execution time Memory
1110786 2024-11-10T13:28:08 Z epicci23 Arranging Shoes (IOI19_shoes) C++17
0 / 100
1000 ms 336 KB
#include "bits/stdc++.h"
using namespace std;
//#define int long long
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)x.size()
 
struct Fenwick{
  vector<int64_t> fw;
  int N;
  Fenwick(int64_t _n){
  	N = _n + 15;
  	fw.assign(N,0);
  }
  void upd(int64_t c){
    for(;c<N;c+=c&-c) fw[c]++;
  }
  int64_t query(int64_t c,int64_t res=0){
  	for(;c;c-=c&-c) res+=fw[c];
  	return res;
  }
};
 
int64_t count_swaps(vector<int> S){
  int64_t n , ans = 0; 
  n = sz(S) / 2;
  int64_t ar[2 * n + 5];
  for(int i=1;i<=2*n;i++) ar[i] = S[i - 1];
 
  if(n<=8){

  	vector<int64_t> xd;
  	for(int i=1;i<=2*n;i++) if(ar[i]<0) xd.push_back(ar[i]);
    sort(all(xd));
    int64_t res = (int)1e9;

    do{

      int64_t hm = 0, p = 1;
      queue<int64_t> q1[n+5];
      queue<int64_t> q2[n+5];

      for(int i=1;i<=2*n;i++){
      	if(ar[i]<0) q1[-ar[i]].push(i);
      	else q2[ar[i]].push(i);
      }

      Fenwick F(2*n);
      for(int u:xd){
       int64_t c = q1[u].front();
  	   q1[u].pop();
  	   int64_t x = q2[u].front();
  	   q1[u].pop();

  	   F.upd(c);
       c+=F.query(2*n)-F.query(c);
       hm+=c-p;
       p++;

       F.upd(x);
       x+=F.query(2*n)-F.query(x);
       hm+=x-p;
       p++;
      }
      res=min(res,hm);
    }while(next_permutation(all(xd)));

    return res;
  }

  /*int p = 1;
  while(!q.empty()){
  	int64_t c = q.front();
  	q.pop();
  	int64_t x = pos[-ar[c]].back();
  	pos[-ar[c]].pop_back();

  	F.upd(c);
    c+=F.query(2*n)-F.query(c);
    ans+=c-p;
    p++;

    F.upd(x);
    x+=F.query(2*n)-F.query(x);
    ans+=x-p;
    p++;
  }
 
  return ans;*/
}
 
 
/*void _(){
  int n,ans=0; cin >> n;
  int ar[2*n+5];
  for(int i=1;i<=2*n;i++) cin >> ar[i];
  
  queue<int> q;
  vector<int> pos[n+5];
 
  Fenwick F(2*n);
 
  for(int i=1;i<=2*n;i++){
    if(ar[i]<0) q.push(i);
    else pos[ar[i]].push_back(i);
  }
  
  for(int i=1;i<=n;i++) reverse(all(pos[i]));
 
  int p = 1;
  while(!q.empty()){
  	int64_t c = q.front();
  	q.pop();
  	int64_t x = pos[-ar[c]].back();
  	pos[-ar[c]].pop_back();

  	F.upd(c);
    c+=F.query(2*n)-F.query(c);
    ans+=c-p;
    p++;

    F.upd(x);
    x+=F.query(2*n)-F.query(x);
    ans+=x-p;
    p++;
  }
 
  cout << ans << '\n';
}
 
int32_t main(){
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tc=1;//cin >> tc;
  while(tc--) _();
}*/

Compilation message

shoes.cpp: In function 'int64_t count_swaps(std::vector<int>)':
shoes.cpp:24:15: warning: unused variable 'ans' [-Wunused-variable]
   24 |   int64_t n , ans = 0;
      |               ^~~
shoes.cpp:89:1: warning: control reaches end of non-void function [-Wreturn-type]
   89 | }
      | ^
# Verdict Execution time Memory Grader output
1 Execution timed out 1068 ms 336 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1068 ms 336 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1068 ms 336 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1054 ms 336 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1068 ms 336 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1068 ms 336 KB Time limit exceeded
2 Halted 0 ms 0 KB -