Submission #1349699

#TimeUsernameProblemLanguageResultExecution timeMemory
1349699cpismayilmmdv985Arranging Shoes (IOI19_shoes)C++20
0 / 100
0 ms344 KiB
#include "shoes.h"
#include "bits/stdc++.h"
using namespace std;

long long count_swaps(vector<int> a) {
   int N = (int)a.size() >> 1;
   // 1 based indexed..
   vector<int> A((N << 1) + 5), L, R;  for (int i = 0; i < (N << 1); i++) 
      A[i + 1] = a[i];
   auto isIn = [&](array<int, 2> x, array<int, 2> y) -> bool {
      if (x[0] <= y[0] && y[0] <= x[1] && x[1] <= y[1])  return true;
      swap(x, y);
      if (x[0] <= y[0] && y[0] <= x[1] && x[1] <= y[1])  return true;
      return false;
   };
   vector<int> match((N << 1) + 5, -1);
   vector<queue<int>> que((N << 1) + 5);
   for (int i = 1; i <= (N << 1); i++) {
      int num = (A[i] > 0 ? A[i] : N + abs(A[i])), wanted = (A[i] > 0 ? A[i] + N : abs(A[i]));
      if (!que[wanted].empty()) {
         int j = que[wanted].front(); que[wanted].pop();
         match[j] = i, match[i] = j;
      } else {
         que[num].push(i);
      }
   }
   int64_t total = 0, res;
   int i = 1;
   vector<bool> used((N << 1) + 5);
   while (i <= (N << 1)) {
      if (used[i]) {
         i++;
         continue;
      }
      res = 0;
      int li = i, ri = match[i];
      // cerr << li << ' ' << ri << '\n';
      used[li] = used[ri] = true;
      if (A[li] < 0 && A[ri] > 0)   res = abs(ri - li) + 1;
      else           res = abs(ri - li);
      for (int j = 1; j < i; j++) {
         int lj = min(j, match[j]), rj = max(j, match[j]);
         res -= isIn({min(li, ri), max(li, ri)}, {min(lj, rj), max(lj, rj)});
      }
      total += res, i++;
   }
   return total;
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...