Submission #282582

# Submission time Handle Problem Language Result Execution time Memory
282582 2020-08-24T15:14:35 Z catalystgma Arranging Shoes (IOI19_shoes) C++14
10 / 100
65 ms 17896 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define fi first
#define se second
#define inf (INT_MAX/2-1)
#define infl (1LL<<60)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a),end(a)
#define y0 y5656
#define y1 y7878
#define y2 y9090
#define aaa system("pause");
#define dbg(x) cerr<<(#x)<<": "<<(x)<<'\n',aaa
#define dbga(x,n) cerr<<(#x)<<"[]: ";for(int _=0;_<n;_++)cerr<<x[_]<<' ';cerr<<'\n',aaa
#define dbgs(x) cerr<<(#x)<<"[stl]: ";for(auto _:x)cerr<<_<<' ';cerr<<'\n',aaa
#define dbgp(x) cerr<<(#x)<<": "<<x.fi<<' '<<x.se<<'\n',aaa
#define maxn 2000
#include "shoes.h"

using namespace std;

bool viz[maxn+5];

ll f_la_fel (vi v) {
  int n = sz(v);
  auto to_equal = [&n] (vi v, vi to) {
    vi p1, p2;
    int i, j, z;
    for (i = 0; i < n; i++)
      if (v[i] < 0) p1.pb(i);
    for (i = 0; i < n; i++)
      if (to[i] < 0) p2.pb(i);
    ll ans = 0;
    for (i = 0; i < n/2; i++) ans += abs(p2[i]-p1[i]);
    return ans;
  };
  int x = abs(v[0]);
  vi to1(n, x), to2(n, -x);
  for (int i = 0; i < n; i += 2) {
    to1[i] *= -1;
    to2[i] *= -1;
  }
  return min(to_equal(v, to1), to_equal(v, to2));
}

ll f1000 (vi v) {
  int n = sz(v), i, j, z;
  ll ans = 0;
  auto cbt = [&n, &v] () {
    int i, j;
    i = 0;
    while (i < n) {
      if (!viz[i] && v[i] < 0) {
        j = i+1;
        while (j < n && viz[j]) j++;
        if (j < n && v[j] == -v[i]) {
          viz[i] = viz[j] = 1;
          i = j+1;
        } else i = j;
      } else i++;
    }
  };
  vector<vi> ap(n/2+1);
  for (int _ = 0; _ < n/2; _++) {
    for (i = 1; i <= n/2; i++) ap[i].clear();
    for (i = 0; i < n; i++) ap[abs(v[i])].pb(i);
    pii best = pii(0, inf);
    int len = inf;
    cbt();
    for (i = 1; i <= n/2; i++) {
      int m = sz(ap[i]);
      ///-x ... x
      for (j = 0; j < m; j++)
        if (!viz[ap[i][j]] && v[ap[i][j]] < 0) {
          z = j+1;
          while (z < m && (viz[ap[i][z]] || v[ap[i][z]] != i)) z++;
          if (z < m && ap[i][z] > ap[i][j]+1 && ap[i][z]-ap[i][j]-1 < len) {
            best = pii(ap[i][j], ap[i][z]);
            len = ap[i][z]-ap[i][j]-1;
          }
        }
      ///x ... -x
      for (j = 0; j < m; j++)
        if (!viz[ap[i][j]] && v[ap[i][j]] > 0) {
          z = j+1;
          while (z < m && (viz[ap[i][z]] && v[ap[i][z]] != -i)) z++;
          if (z < m && ap[i][z]-ap[i][j] < len) {
            best = pii(ap[i][j], ap[i][z]);
            len = ap[i][z]-ap[i][j];
          }
        }
    }
    if (len == inf) break;
    while (len--) {
      swap(v[best.se], v[best.se-1]);
      swap(viz[best.se], viz[best.se-1]);
      best.se--;
      ans++;
    }
  }
  return ans;
}

ll count_swaps (vi v) {
  int n = sz(v);
  if (n == 2) {
    if (v[0] > v[1]) return 1;
    return 0;
  }
//  if (n <= 2000) return f1000(v);
//  return f_la_fel(v);
  return min(f1000(v), f_la_fel(v));
}

//int main () {
//  int n; cin >> n;
//  int i, j, z;
//  vi v(n*2);
//  for (i = 0; i < 2*n; i++) cin >> v[i];
//  cout << count_swaps(v);
//  return 0;
//}

Compilation message

shoes.cpp: In lambda function:
shoes.cpp:35:12: warning: unused variable 'j' [-Wunused-variable]
   35 |     int i, j, z;
      |            ^
shoes.cpp:35:15: warning: unused variable 'z' [-Wunused-variable]
   35 |     int i, j, z;
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Incorrect 0 ms 256 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 416 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Incorrect 1 ms 384 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Runtime error 65 ms 17896 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Incorrect 0 ms 256 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Incorrect 0 ms 256 KB Output isn't correct
4 Halted 0 ms 0 KB -