답안 #782309

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
782309 2023-07-13T19:17:29 Z ikaurov 고대 책들 (IOI17_books) C++14
70 / 100
642 ms 1048576 KB
// #include "books.h"
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define all(arr) (arr).begin(), (arr).end()
#define ll long long
#define ld long double
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define endl '\n'

const int INF = 1e9;

long long minimum_walk(std::vector<int> p, int s) {
	int n = sz(p);
  if (s == 0){
    ll ans = 0;
    for (int i = 0; i < n; i++){
      ans += abs(i - p[i]);
    }
    for (int i = 0, mx = -1; i < n;){
      mx = max(mx, p[i]);
      if (mx <= i){
        int j = i + 1;
        while (j < n && j == p[j]) j++;
        if (j == n) break;
        ans += 2 * (j - i), i = j;
      }
      else i++;
    }
    return ans;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++){
    ans += abs(i - p[i]);
  }
  int mincycle = -1, maxcycle = -1;
  for (int i = 0; i < n; i++) if (i != p[i]){
    mincycle = i;
    break;
  }
  for (int i = n - 1; i >= 0; i--) if (i != p[i]){
    maxcycle = i;
    break;
  }
  if (mincycle == -1) return 0;
  for (int i = mincycle, mx = -1; i < n;){
    mx = max(mx, p[i]);
    if (mx <= i){
      int j = i + 1;
      while (j < n && j == p[j]) j++;
      if (j == n) break;
      ans += 2 * (j - i), i = j;
    }
    else i++;
  }
  if (s < mincycle) return ans + 2 * (mincycle - s);
  if (s > maxcycle) return ans + 2 * (s - maxcycle);
  vector<bool> cycleend(n);
  for (int i = 0, mx = -1; i < n; i++){
    mx = max(mx, p[i]);
    if (mx <= i) cycleend[i] = 1;
  }

  vector<int> cur, used(n), cid(n), minpos(n, INF), maxpos(n, -1);
  vector<int> minl(n, INF), maxr(n, -1);

  int id = 0;

  function<void(int)> dfs = [&] (int v){
    used[v] = 1, cid[v] = id;
    minpos[id] = min(minpos[id], v);
    maxpos[id] = max(maxpos[id], v);
    if (!used[p[v]]) dfs(p[v]);
  };

  for (int i = 0; i < n; i++){
    if (used[i]) continue;
    dfs(i);
    id++;
  }

  vector<vector<int>> g(id);

  for (int i = 0; i < id; i++){
    for (int j = minpos[i]; j <= maxpos[i]; j++){
      g[cid[j]].pb(i);
    }
  }

  function<void(int, int)> dfs1 = [&] (int v, int val){
    minl[v] = val;
    for (auto u : g[v]) if (minl[u] == INF) dfs1(u, val);
  };

  function<void(int, int)> dfs2 = [&] (int v, int val){
    maxr[v] = val;
    for (auto u : g[v]){
      if (maxr[u] == -1) dfs2(u, val);
    }
  };

  vector<int> order(id);
  iota(all(order), 0);

  sort(all(order), [&] (int i, int j){
    return minpos[i] < minpos[j];
  });

  for (auto i : order){
    if (minl[i] == INF) dfs1(i, minpos[i]);
  }

  sort(all(order), [&] (int i, int j){
    return maxpos[i] > maxpos[j];
  });

  for (auto i : order){
    if (maxr[i] == -1) dfs2(i, maxpos[i]);
  }

  vector<vector<int>> dp(n, vector<int>(n, INF));

  dp[minl[cid[s]]][maxr[cid[s]]] = 0;

  ll ans1;

  for (int len = 1; len <= n; len++){
    for (int l = 0; l + len - 1 < n; l++){
      int r = l + len - 1;
      if (dp[l][r] == INF) continue;
      ans1 = dp[l][r];
      if (l > 0 && !cycleend[l - 1]){
        int newl = min(l, minl[cid[l - 1]]), newr = max(r, maxr[cid[l - 1]]);
        dp[newl][newr] = min(dp[newl][newr], dp[l][r] + 2);
      }
      if (!cycleend[r] && r < n - 1){
        int newl = min(l, minl[cid[r + 1]]), newr = max(r, maxr[cid[r + 1]]);
        dp[newl][newr] = min(dp[newl][newr], dp[l][r] + 2);
      }
    }
  }

  return ans + ans1;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:146:16: warning: 'ans1' may be used uninitialized in this function [-Wmaybe-uninitialized]
  146 |   return ans + ans1;
      |                ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 300 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 300 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 296 KB Output is correct
30 Correct 69 ms 8300 KB Output is correct
31 Correct 70 ms 8296 KB Output is correct
32 Correct 70 ms 8300 KB Output is correct
33 Correct 77 ms 8296 KB Output is correct
34 Correct 79 ms 8404 KB Output is correct
35 Correct 72 ms 8300 KB Output is correct
36 Correct 69 ms 8312 KB Output is correct
37 Correct 68 ms 8300 KB Output is correct
38 Correct 68 ms 8288 KB Output is correct
39 Correct 68 ms 8336 KB Output is correct
40 Correct 87 ms 8296 KB Output is correct
41 Correct 91 ms 8300 KB Output is correct
42 Correct 70 ms 8312 KB Output is correct
43 Correct 70 ms 8316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4308 KB Output is correct
2 Correct 3 ms 4308 KB Output is correct
3 Correct 3 ms 4308 KB Output is correct
4 Correct 3 ms 4308 KB Output is correct
5 Correct 3 ms 4308 KB Output is correct
6 Correct 3 ms 4308 KB Output is correct
7 Correct 3 ms 4308 KB Output is correct
8 Correct 3 ms 4308 KB Output is correct
9 Correct 3 ms 4308 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 4 ms 5588 KB Output is correct
12 Correct 5 ms 5716 KB Output is correct
13 Correct 3 ms 4308 KB Output is correct
14 Correct 3 ms 4256 KB Output is correct
15 Correct 2 ms 4308 KB Output is correct
16 Correct 3 ms 4308 KB Output is correct
17 Correct 3 ms 4692 KB Output is correct
18 Correct 3 ms 4564 KB Output is correct
19 Correct 3 ms 4308 KB Output is correct
20 Correct 3 ms 4308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 300 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 296 KB Output is correct
30 Correct 69 ms 8300 KB Output is correct
31 Correct 70 ms 8296 KB Output is correct
32 Correct 70 ms 8300 KB Output is correct
33 Correct 77 ms 8296 KB Output is correct
34 Correct 79 ms 8404 KB Output is correct
35 Correct 72 ms 8300 KB Output is correct
36 Correct 69 ms 8312 KB Output is correct
37 Correct 68 ms 8300 KB Output is correct
38 Correct 68 ms 8288 KB Output is correct
39 Correct 68 ms 8336 KB Output is correct
40 Correct 87 ms 8296 KB Output is correct
41 Correct 91 ms 8300 KB Output is correct
42 Correct 70 ms 8312 KB Output is correct
43 Correct 70 ms 8316 KB Output is correct
44 Correct 5 ms 4308 KB Output is correct
45 Correct 3 ms 4308 KB Output is correct
46 Correct 3 ms 4308 KB Output is correct
47 Correct 3 ms 4308 KB Output is correct
48 Correct 3 ms 4308 KB Output is correct
49 Correct 3 ms 4308 KB Output is correct
50 Correct 3 ms 4308 KB Output is correct
51 Correct 3 ms 4308 KB Output is correct
52 Correct 3 ms 4308 KB Output is correct
53 Correct 1 ms 256 KB Output is correct
54 Correct 4 ms 5588 KB Output is correct
55 Correct 5 ms 5716 KB Output is correct
56 Correct 3 ms 4308 KB Output is correct
57 Correct 3 ms 4256 KB Output is correct
58 Correct 2 ms 4308 KB Output is correct
59 Correct 3 ms 4308 KB Output is correct
60 Correct 3 ms 4692 KB Output is correct
61 Correct 3 ms 4564 KB Output is correct
62 Correct 3 ms 4308 KB Output is correct
63 Correct 3 ms 4308 KB Output is correct
64 Runtime error 642 ms 1048576 KB Execution killed with signal 9
65 Halted 0 ms 0 KB -