Submission #207207

#TimeUsernameProblemLanguageResultExecution timeMemory
207207AlexLuchianovAncient Books (IOI17_books)C++14
50 / 100
219 ms22812 KiB
#include "books.h"
#include <cmath>
#include <iostream>
using namespace std;

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
using ll = long long;

int const nmax = 1000000;
int sumright[1 + nmax], sumleft[1 + nmax];

long long minimum_walk(std::vector<int> p, int start) {
  int n = p.size();
  ll result = 0;

  for(int i = 0; i < n; i++){
    result += fabs(p[i] - i);
    if(i < p[i]){
      sumright[i]++;
      sumright[p[i]]--;
    } else if(p[i] < i){
      sumleft[i - 1]++;
      if(0 <= p[i])
        sumleft[p[i] - 1]--;
    }
  }
  for(int i = 1; i < n; i++)
    sumright[i] += sumright[i - 1];
  for(int i = n - 1; 0 <= i; i--)
    sumleft[i] += sumleft[i + 1];

  int active = 0;
  for(int i = n; start <= i; i--){
    active |= (0 < sumright[i]);
    active |= (0 < sumleft[i]);

    if(0 == sumright[i] && 0 == sumleft[i])
      result += active * 2;
    else
      result += fabs(sumright[i] - sumleft[i]);
  }
  active = 0;
  for(int i = 0; i < start; i++){
    active |= (0 < sumright[i]);
    active |= (0 < sumleft[i]);

    if(0 == sumright[i] && 0 == sumleft[i])
      result += active * 2;
    else
      result += fabs(sumright[i] - sumleft[i]);
  }
	return result;
}
/*
6 0
0 1 2 5 4 3
*/
#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...