Submission #782269

#TimeUsernameProblemLanguageResultExecution timeMemory
782269ikaurovAncient Books (IOI17_books)C++17
50 / 100
81 ms8056 KiB
#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'

long long minimum_walk(std::vector<int> p, int s) {
	int n = sz(p);
  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;
}
#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...