Submission #780437

# Submission time Handle Problem Language Result Execution time Memory
780437 2023-07-12T09:00:07 Z vjudge1 Ancient Books (IOI17_books) C++17
50 / 100
1063 ms 52508 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int nmax = 1e6 + 5;

int n;

long long minimum_walk(std::vector<int> p, int s) {
  n = sz(p);
  vector<int> col(n, 0);
  vector<int> reqs(n + 2, 0);
  int flag = 0;
  
  multiset<int> lhf;
  
  ll sum = 0;
  
  for(int i = 0; i < n; i++) {
    if(col[i] == 0) {
      flag++;
      int x = i, cnt = 0;
      set<int> vals;
      while(col[x] != flag) {
        col[x] = flag;
        sum += abs(x - p[x]);
        x = p[x];
        cnt++;
        vals.emplace(x);
      }
      int L = *vals.begin();
      int R = *vals.rbegin();
      reqs[L]++;
      reqs[R]--;
      
    }
  }
  for(int i = 1; i <= n; i++)
    reqs[i] += reqs[i - 1];
  int l = 0, r = n - 1;
  while(l < n && p[l] == l) l++;
  while(r > 0 && p[r] == r) r--;
  if(r < l) return 0;
  int dist[2] = {n + 5, n + 5};
  int ptr = s;
  if(l <= s && s <= r) {
      dist[0] = 0;
      while(reqs[ptr] != 0 && p[ptr] == ptr) ptr++, dist[0]++;
      ptr = s;
      dist[1] = 0;
      while(reqs[ptr - 1] != 0 && p[ptr] == ptr) ptr--, dist[1]++;
    
  }
  else {
    dist[0] = dist[1] = min(abs(l - s), abs(r - s));
  }
  r--;
  

  ll mn = min(dist[0], dist[1]);
  for(int i = l; i <= r; i++) mn += reqs[i] == 0;
  return sum + mn * 2;
}

/**
      Anul asta se da centroid.
-- Surse oficiale
*/

# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 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 0 ms 212 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 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 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 0 ms 212 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 0 ms 212 KB Output is correct
30 Correct 1063 ms 52508 KB Output is correct
31 Correct 725 ms 34076 KB Output is correct
32 Correct 114 ms 15848 KB Output is correct
33 Correct 121 ms 15864 KB Output is correct
34 Correct 113 ms 15852 KB Output is correct
35 Correct 118 ms 15868 KB Output is correct
36 Correct 139 ms 15848 KB Output is correct
37 Correct 157 ms 15980 KB Output is correct
38 Correct 175 ms 16076 KB Output is correct
39 Correct 187 ms 16208 KB Output is correct
40 Correct 261 ms 17728 KB Output is correct
41 Correct 650 ms 31692 KB Output is correct
42 Correct 416 ms 23344 KB Output is correct
43 Correct 116 ms 15948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 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 0 ms 212 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 0 ms 212 KB Output is correct
30 Correct 1063 ms 52508 KB Output is correct
31 Correct 725 ms 34076 KB Output is correct
32 Correct 114 ms 15848 KB Output is correct
33 Correct 121 ms 15864 KB Output is correct
34 Correct 113 ms 15852 KB Output is correct
35 Correct 118 ms 15868 KB Output is correct
36 Correct 139 ms 15848 KB Output is correct
37 Correct 157 ms 15980 KB Output is correct
38 Correct 175 ms 16076 KB Output is correct
39 Correct 187 ms 16208 KB Output is correct
40 Correct 261 ms 17728 KB Output is correct
41 Correct 650 ms 31692 KB Output is correct
42 Correct 416 ms 23344 KB Output is correct
43 Correct 116 ms 15948 KB Output is correct
44 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -