제출 #94567

#제출 시각아이디문제언어결과실행 시간메모리
94567wxh010910Ancient Books (IOI17_books)C++17
100 / 100
147 ms19072 KiB
#include <bits/stdc++.h>
#include "books.h"

using namespace std;

long long minimum_walk(vector<int> p, int s) {
  int n = p.size();
  int l = n, r = -1;
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    if (i != p[i]) {
      ans += abs(i - p[i]);
      l = min(l, i);
      r = max(r, i);
    }
  }
  int cur_l = s, cur_r = s;
  queue<int> q;
  q.push(s);
  while (true) {
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      while (cur_l > p[x]) {
        q.push(--cur_l);
      }
      while (cur_r < p[x]) {
        q.push(++cur_r);
      }
    }
    int new_l = cur_l, new_r = cur_r;
    int cost_l = 0, cost_r = 0;
    bool found_l = false, found_r = false;
    while (new_l > l && !found_l) {
      q.push(--new_l);
      ++cost_l;
      while (!q.empty()) {
        int x = q.front();
        q.pop();
        if (p[x] > s) {
          found_l = true;
        }
        while (new_l > p[x]) {
          q.push(--new_l);
        }
      }
    }
    while (new_r < r && !found_r) {
      q.push(++new_r);
      ++cost_r;
      while (!q.empty()) {
        int x = q.front();
        q.pop();
        if (p[x] < s) {
          found_r = true;
        }
        while (new_r < p[x]) {
          q.push(++new_r);
        }
      }
    }
    if (found_l && found_r) {
      ans += min(cost_l, cost_r) << 1;
      while (cur_l > new_l) {
        q.push(--cur_l);
      }
      while (cur_r < new_r) {
        q.push(++cur_r);
      }
    } else {
      ans += cost_l + cost_r << 1;
      break;
    }
  }
  return ans;
}

컴파일 시 표준 에러 (stderr) 메시지

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:71:21: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
       ans += cost_l + cost_r << 1;
              ~~~~~~~^~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...