#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define sz(v) ((int)(v).size())
typedef long long lint;
int n,sz;
int v[1000002];
int d[1000002];
int le[1000002],ri[1000002];
vector<int>go;
vector<int>cycle[1000002];
lint cost[1000002];
void dfs(int p,int par)
{
cycle[sz].push_back(p);
cost[sz]+=abs(par-p);
v[p]=1;
if(!v[go[p]])dfs(go[p],p);
else cost[sz]+=abs(go[p]-p);
}
lint minimum_walk(vector<int> p, int s) {
n=sz(p);
go=p;
for(int i=0;i<n;i++)
{
if(!v[i])
{
sz++;
dfs(i,i);
}
}
vector<int>X;
lint ans=0;
int maxi=-1,mini=1e9,tmp=0;
for(int i=1;i<=sz;i++)
{
if(!sz(cycle[i]))continue;
ans+=cost[i];
sort(cycle[i].begin(),cycle[i].end());
d[cycle[i][0]]++;
d[cycle[i][sz(cycle[i])-1]]--;
maxi=max(maxi,cycle[i][sz(cycle[i])-1]-1);
mini=min(mini,cycle[i][0]);
if(cycle[i][0]<s && s<cycle[i][sz(cycle[i])-1])
{
int ch=0;
for(auto &k:cycle[i])
{
if(k==s)
{
ch=1;break;
}
}
if(!ch)
{
for(auto &k:cycle[i])
{
if(!tmp)tmp=abs(s-k);
else tmp=min(tmp,abs(s-k));
}
}
}
}
ans+=2*tmp;
int c=0;
for(int i=0;i<n;i++)
{
c+=d[i];
d[i]=c;
}
if(s<=mini)
{
for(int i=s;i<=maxi;i++)
{
if(!d[i])ans+=2;
}
}
else if(s>maxi)
{
for(int i=mini;i<s;i++)
{
if(!d[i])ans+=2;
}
}
else
{
for(int i=mini;i<=maxi;i++)
{
if(!d[i])ans+=2;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23784 KB |
Output is correct |
4 |
Correct |
11 ms |
23816 KB |
Output is correct |
5 |
Incorrect |
13 ms |
23748 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23784 KB |
Output is correct |
4 |
Correct |
11 ms |
23816 KB |
Output is correct |
5 |
Incorrect |
13 ms |
23748 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23784 KB |
Output is correct |
4 |
Correct |
11 ms |
23816 KB |
Output is correct |
5 |
Incorrect |
13 ms |
23748 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
23764 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2746' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23784 KB |
Output is correct |
4 |
Correct |
11 ms |
23816 KB |
Output is correct |
5 |
Incorrect |
13 ms |
23748 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |