답안 #1025532

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1025532 2024-07-17T06:29:55 Z huutuan 고대 책들 (IOI17_books) C++14
0 / 100
0 ms 348 KB
#include "books.h"

#include <bits/stdc++.h>

using namespace std;

const int N=1e6;
int vis[N], n;

long long minimum_walk(vector<int> p, int s) {
   n=p.size();
   vector<pair<int, int>> v;
   long long ans=0;
   vector<int> vv;
   for (int i=0; i<n; ++i) if (!vis[i]){
      int mx=i, mn=i;
      while (!vis[i]){
         if (i!=p[i]) vv.push_back(i);
         mx=max(mx, i);
         mn=min(mn, i);
         vis[i]=1;
         ans+=abs(p[i]-i);
         i=p[i];
      }
      v.emplace_back(mn, mx);
   }
   int pf=0, sf=0;
   for (int i=0; i<n; ++i) if (p[i]==i) ++pf; else break;
   for (int i=n-1; i>=0; --i) if (p[i]==i) ++sf; else break;
   sort(v.begin(), v.end());
   int cur=0;
   for (auto &i:v){
      if (cur<=i.first) ans+=(i.first-cur)*2;
      cur=max(cur, i.second);
   }
   bool check=0;
   for (auto &i:v) check|=i.first<=s && s<=i.second;
   if (pf>=s) ans-=s*2;
   else ans-=pf*2;
   if (sf>=n-s-1) ans-=(n-s-1)*2;
   else ans-=sf*2;
   sort(vv.begin(), vv.end());
   if (check){
      auto it=lower_bound(vv.begin(), vv.end(), s);
      if (it==vv.begin()) ans+=(*it-s)*2;
      else ans+=min(*it-s, s-*prev(it))*2;
   }
   return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
2 Halted 0 ms 0 KB -