Submission #321280

# Submission time Handle Problem Language Result Execution time Memory
321280 2020-11-11T22:05:05 Z VROOM_VARUN Ancient Books (IOI17_books) C++14
0 / 100
2000 ms 364 KB
/*
ID: varunra2
LANG: C++
TASK: books
*/

#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

int s;
int n;
int targl, targr;
int cycs = 0;
VI wcyc;
VI lcyc;
VI rcyc;
ll retgood = 0;
ll retbad = 0;

void init(VI& order) {
  n = sz(order);
  targl = s;
  targr = s;
  wcyc.assign(n, -1);
  lcyc.resize(n);
  rcyc.resize(n);
}

void extend(int& l, int &r) {
  // can we move l or r?
  int nl, nr;
  nl = min(lcyc[wcyc[l]], lcyc[wcyc[r]]);
  nr = max(rcyc[wcyc[l]], rcyc[wcyc[r]]);

  while(nl < l or nr > r) {
    if(l >= nl) {
      l--;
      nl = min(nl, lcyc[wcyc[l]]);
      nr = max(nr, rcyc[wcyc[r]]);
    }
    else {
      r++;
      nl = min(nl, lcyc[wcyc[r]]);
      nr = max(nr, rcyc[wcyc[r]]);
    }
  }

}

ll solve() {
  int l = s, r = s;
  do {
    extend(l, r);

    int costl = 0, costr = 0, ll = l, lr = r, rl = l, rr = r;
    bool next = false;

    while(true) {
      if(ll <= targl) break;
      ll--;
      extend(ll, lr);
      costl += 2;
      if(lr > r) {
        next = true;
        break;
      }
    }

    while(true) {
      if(rr >= targr or rl < l) break;
      rr++;
      extend(rl, rr);
      costl += 2;
    }

    if(next) 
      retbad += min(costl, costr);
    else retbad += (costl + costr);

    l = min(ll, rl);
    r = max(rr, lr);

  } while(l > targl or r < targr);
}

ll minimum_walk(VI order, int s) {
  ::s = s;
  init(order);
  for (int i = 0; i < n; i++) {
    retgood += abs(i - order[i]);
    if (wcyc[i] != -1) continue;
    int pos = i;
    lcyc[cycs] = i;
    rcyc[cycs] = i;
    do {
      wcyc[pos] = cycs;
      rcyc[cycs] = max(rcyc[cycs], pos);
      pos = order[pos];
    } while (pos != i);
    if(i != order[i]) {
      targl = min(targl, lcyc[cycs]);
      targr = max(targr, rcyc[cycs]);
    }
    cycs++;
  }
  return retgood + solve();
}

// int main() {
// #ifndef ONLINE_JUDGE
//   freopen("books.in", "r", stdin);
//   freopen("books.out", "w", stdout);
// #endif
//   cin.sync_with_stdio(0);
//   cin.tie(0);

//   return 0;
// }

Compilation message

books.cpp: In function 'll solve()':
books.cpp:129:1: warning: no return statement in function returning non-void [-Wreturn-type]
  129 | }
      | ^
# Verdict Execution time Memory Grader output
1 Execution timed out 2057 ms 364 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2057 ms 364 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2057 ms 364 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2074 ms 364 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2057 ms 364 KB Time limit exceeded
2 Halted 0 ms 0 KB -