#include "books.h"
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
using namespace std;
bool visit[1000100];
long long minimum_walk(vector<int> p, int s) {
int n=(int)p.size();
vector<pair<int,int>> coord;
long long ans=0;
for (int i=0;i<n;i++) {
if (visit[i]) continue;
visit[i]=true;
int mn=i;
int mx=i;
int cur=p[i];
ans+=abs(p[i]-i);
while (cur!=i) {
visit[cur]=true;
mx=max(mx,cur);
mn=min(mn,cur);
ans+=abs(p[cur]-cur);
cur=p[cur];
}
coord.push_back({mn,mx});
}
sort(coord.begin(),coord.end(),[](const pair<int,int> &a,const pair<int,int> &b) {
if (a.second!=b.second) return a.second<b.second;
return a.first<b.first;
});
vector<pair<int,int>> re;
for (int i=0;i<(int)coord.size();i++) {
int curl=coord[i].first;
//if (!re.empty()&&coord[i].second==re.back().second) continue;
while (!re.empty()&&coord[i].first<=re.back().second) {
curl=min(curl,re.back().first);
re.pop_back();
}
re.push_back({curl,coord[i].second});
}
for (int i=1;i<(int)re.size();i++) {
assert(re[i].first>=re[i-1].second);
ans+=(re[i].first-re[i-1].second)*2;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
536 KB |
Output is correct |
4 |
Correct |
2 ms |
536 KB |
Output is correct |
5 |
Incorrect |
2 ms |
540 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
536 KB |
Output is correct |
4 |
Correct |
2 ms |
536 KB |
Output is correct |
5 |
Incorrect |
2 ms |
540 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
536 KB |
Output is correct |
4 |
Correct |
2 ms |
536 KB |
Output is correct |
5 |
Incorrect |
2 ms |
540 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
596 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
536 KB |
Output is correct |
4 |
Correct |
2 ms |
536 KB |
Output is correct |
5 |
Incorrect |
2 ms |
540 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |