#include <bits/stdc++.h>
#include "books.h"
#define rep(i,n) for(int i = 0; i < n; i++)
#define lint long long
using namespace std;
vector<int>fen;
int n;
void up(int i, int val){
i++;
for(; i <=n; i+=i&-i){
fen[i]+=val;
}
}
int get(int i){
i++;
int s=0;
for(;i>0;i-=i&-i){
s+=fen[i];
}
return s;
}
int get_range(int l, int r){
return get(r) - get(l-1);
}
lint getCost(int end, int s, vector<int>const&p){
fen.resize(n+1);
vector<bool>unhappy(n);
rep(i,n)
if(p[i]!=i&&i!=end) {up(i,1); unhappy[i]=true;}
lint cost=0;
while(get(n-1)>0){
int begin=-1, e=n;
while(e-begin>1){
int cur = (begin+e)/2;
if(get_range(max(0, s-cur), min(n-1, s+cur)))
{
e=cur;
} else
{
begin=cur;
}
}
if(unhappy[max(0, s-e)])
{
s=max(0, s-e);
} else if (unhappy[min(n-1, s+e)])
{
s=min(n-1, s+e);
}
cost+=e;
up(s, -1);
unhappy[s]=false;
cost+=abs(p[s]-s);
s=p[s];
}
if(p[end]!=end){cost+=abs(s-end)+abs(end-p[end]);}
return cost;
}
long long minimum_walk(std::vector<int> p, int s) {
n = p.size();
lint minn=1e19;
rep(i, n){
minn=min(minn, getCost(i,s,p));
}
return minn;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:76:12: warning: overflow in conversion from 'double' to 'long long int' changes value from '1.0e+19' to '9223372036854775807' [-Woverflow]
76 | lint minn=1e19;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
3rd lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
3rd lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
3rd lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
122 ms |
376 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4165' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
3rd lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |