답안 #293847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
293847 2020-09-08T13:00:40 Z Bruteforceman 고대 책들 (IOI17_books) C++11
50 / 100
236 ms 20572 KB
#include "bits/stdc++.h"
#include "books.h"
using namespace std;

long long minimum_walk(std::vector<int> p, int s) {
  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;
  }
  int opt = INT_MAX;
  for(int i = 0; i < n; i++) {
    if(p[i] != i) {
      opt = min(opt, abs(i - s));
    }
  }
  if(opt < INT_MAX) {
    if(s > 0 && pre[s - 1] > 0) {
      if(pre[s] > 0) {
        ans += 2 * opt;
      }
    }
  }
  return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:28:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |   for(int i = 1; i < pre.size(); i++) pre[i] += pre[i - 1];
      |                  ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 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 0 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 0 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 1 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
# 결과 실행 시간 메모리 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 0 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 0 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 1 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 384 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 256 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 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 0 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 0 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 1 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 384 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 256 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 227 ms 13020 KB Output is correct
31 Correct 225 ms 14716 KB Output is correct
32 Correct 210 ms 20572 KB Output is correct
33 Correct 225 ms 18288 KB Output is correct
34 Correct 236 ms 18144 KB Output is correct
35 Correct 225 ms 17120 KB Output is correct
36 Correct 207 ms 14808 KB Output is correct
37 Correct 182 ms 13168 KB Output is correct
38 Correct 182 ms 12792 KB Output is correct
39 Correct 186 ms 12920 KB Output is correct
40 Correct 187 ms 12952 KB Output is correct
41 Correct 204 ms 13712 KB Output is correct
42 Correct 197 ms 13748 KB Output is correct
43 Correct 227 ms 18524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 384 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 0 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 0 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 1 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 384 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 256 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 227 ms 13020 KB Output is correct
31 Correct 225 ms 14716 KB Output is correct
32 Correct 210 ms 20572 KB Output is correct
33 Correct 225 ms 18288 KB Output is correct
34 Correct 236 ms 18144 KB Output is correct
35 Correct 225 ms 17120 KB Output is correct
36 Correct 207 ms 14808 KB Output is correct
37 Correct 182 ms 13168 KB Output is correct
38 Correct 182 ms 12792 KB Output is correct
39 Correct 186 ms 12920 KB Output is correct
40 Correct 187 ms 12952 KB Output is correct
41 Correct 204 ms 13712 KB Output is correct
42 Correct 197 ms 13748 KB Output is correct
43 Correct 227 ms 18524 KB Output is correct
44 Incorrect 1 ms 384 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -