Submission #293787

# Submission time Handle Problem Language Result Execution time Memory
293787 2020-09-08T12:07:36 Z Bruteforceman Ancient Books (IOI17_books) C++11
50 / 100
295 ms 20828 KB
#include "bits/stdc++.h"
#include "books.h"
using namespace std;

long long minimum_walk(std::vector<int> p, int s) {
  assert(s == 0);
  int n = p.size();
  long long ans = 0;
  vector <bool> vis (n, false);
  vector <pair <int, int>> range;
  for(int i = 0; i < n; i++) ans += abs(p[i] - i);
  for(int i = 0; i < n; i++) {
    if(vis[i] == true) continue;
    vector <int> cycle;
    int cur = i;
    do {
      vis[cur] = true;
      cycle.push_back(cur);
      cur = p[cur];
    } while(vis[cur] == false);
    range.emplace_back(*min_element(cycle.begin(), cycle.end()),
                       *max_element(cycle.begin(), cycle.end()));
  }
  vector <int> pre (n, 0); 
  for(auto i : range) {
    pre[i.second] -= 1;
    pre[i.first] += 1;
  }
  for(int i = 1; i < pre.size(); i++) pre[i] += pre[i - 1];
  ans += count(pre.begin(), pre.end(), 0) * 2;
  for(int i = pre.size() - 1; i >= s; i--) {
    if(pre[i] == 1) break;
    ans -= 2;
  }
  for(int i = 0; i < s; i++) {
    if(pre[i] == 1) break;
    ans -= 2;
  }
	return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:29:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |   for(int i = 1; i < pre.size(); i++) pre[i] += pre[i - 1];
      |                  ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 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 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 256 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 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 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 256 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 288 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 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 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 256 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 288 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 244 ms 13288 KB Output is correct
31 Correct 253 ms 14972 KB Output is correct
32 Correct 295 ms 20828 KB Output is correct
33 Correct 227 ms 18396 KB Output is correct
34 Correct 261 ms 18396 KB Output is correct
35 Correct 224 ms 17448 KB Output is correct
36 Correct 200 ms 15080 KB Output is correct
37 Correct 212 ms 13424 KB Output is correct
38 Correct 193 ms 13124 KB Output is correct
39 Correct 180 ms 13048 KB Output is correct
40 Correct 190 ms 13272 KB Output is correct
41 Correct 220 ms 13824 KB Output is correct
42 Correct 196 ms 13872 KB Output is correct
43 Correct 256 ms 18784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 512 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 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 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 256 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 288 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 244 ms 13288 KB Output is correct
31 Correct 253 ms 14972 KB Output is correct
32 Correct 295 ms 20828 KB Output is correct
33 Correct 227 ms 18396 KB Output is correct
34 Correct 261 ms 18396 KB Output is correct
35 Correct 224 ms 17448 KB Output is correct
36 Correct 200 ms 15080 KB Output is correct
37 Correct 212 ms 13424 KB Output is correct
38 Correct 193 ms 13124 KB Output is correct
39 Correct 180 ms 13048 KB Output is correct
40 Correct 190 ms 13272 KB Output is correct
41 Correct 220 ms 13824 KB Output is correct
42 Correct 196 ms 13872 KB Output is correct
43 Correct 256 ms 18784 KB Output is correct
44 Runtime error 1 ms 512 KB Execution killed with signal 11
45 Halted 0 ms 0 KB -