Submission #302067

# Submission time Handle Problem Language Result Execution time Memory
302067 2020-09-18T12:13:26 Z kevlee Ancient Books (IOI17_books) C++17
0 / 100
1 ms 384 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mod 1000000007
#define h1 7897897897897897
#define h2 7897466719774591
#define b1 98762051
#define b2 98765431
#define inf 1000000000
#define pi 3.1415926535897932384626
#define LMAX 9223372036854775807
#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pii>
#define SET(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
bool first_cycle;
int p[1000005], n;
bool vis[1000005];
ll ans;
void dfs(int x) {
  if (vis[x]) return;
  vis[x] = true;
  ans += abs(x - p[x]);
  dfs(p[x]);
}
long long minimum_walk(vector<int> perm, int s) {
  n = perm.size();
  FOR(i, 0, n-1) p[i] = perm[i];
  int cycle = 0;
  vi v;
  FOR(i, 0, n-1) {
    if (!vis[i]) {
      cycle++;
      if (p[i] == i) {
        vis[i] = true;
        continue;
      }
      dfs(i);
      v.pb(i);
    }
  }
  if (cycle == 1) {
    return ans;
  }
  if (v.size() == 0) return 0;
  int v1 = (v.size() > 1 ? v[1] : 0);
  ans += v.back();
  cout << ans << endl;
  int cur = 0;
  int add = v.back();
  do {
    cout << v1 << ' ' << cur << ' ' << v.back() << endl;
    if (cur < v1) add = min(add, abs(v.back() - cur) - abs(cur - p[cur]));
    else add = min(add, 2 * abs(cur - v1) + abs(v.back() - cur) - abs(cur - p[cur]));
    cur = p[cur];
  } while (cur != 0);
  ans += add;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 384 KB secret mismatch
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 384 KB secret mismatch
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 384 KB secret mismatch
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 384 KB secret mismatch
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 384 KB secret mismatch
2 Halted 0 ms 0 KB -