#include "books.h"
#include <cstdio>
#include <vector>
#include<queue>
#include <cassert>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define N 1001000
#define mp make_pair
#define fs first
#define sn second
using namespace std;
ll n,m,w,x,y,s,ans,tot,h[N],b[N],p[N],to[N],mx[N],mi[N],fa[N],fr[N];
priority_queue<pair<ll,ll> >d;
struct rec
{
int to,nx,l;
}e[N<<3];
void dfs(ll x)
{
if(fr[x])return;
fr[x]=w;
mx[w]=max(mx[w],x);
mi[w]=min(mi[w],x);
ans+=abs(x-to[x]);
dfs(to[x]);
return;
}
void addl(ll x,ll y,ll z)
{
e[++tot].to=y;
e[tot].nx=h[x];
e[tot].l=z;
h[x]=tot;
return;
}
void dij()
{
memset(p,0,sizeof(p));
memset(b,127/3,sizeof(b));
b[w]=0;
d.push(mp(0,w));
while(!d.empty()){
ll x=d.top().sn;
d.pop();
if(p[x])continue;
p[x]=1;
ans+=b[x];
for(ll i=h[x];i;i=e[i].nx){
ll y=e[i].to;
if(p[y]||e[i].l>b[y])continue;
b[y]=e[i].l;
d.push(mp(-b[y],y));
}
}
return;
}
long long minimum_walk(std::vector<int> too, int ss) {
n=too.size();
for(ll i=1;i<=n;++i)
to[i]=too[i-1]+1;
s=ss+1;
w=m=0;
for(ll i=1;i<=n;++i)
if(!fr[i]&&to[i]!=i){
++w;
mi[w]=1001000;
mx[w]=0;
dfs(i);
}
for(ll i=1;i<=n;++i)
for(ll j=i+1;j<=n;++j)
if(fr[i]!=fr[j]&&fr[i]&&fr[j]){
addl(fr[i],fr[j],abs(j-i)*2);
addl(fr[j],fr[i],abs(j-i)*2);
}
// l[++m]=mp((j-i)*2,mp(fr[i],fr[j]));
for(ll i=1;i<=n;++i)
for(ll j=1;j<=w;++j)
if(fr[i]&&fr[i]!=j&&mi[j]<=i&&i<=mx[j]){
addl(j,fr[i],0);
}
// l[++m]=mp(0,mp(fr[i],j));
w++;
for(ll i=1;i<=n;++i)
if(fr[i]){
addl(w,fr[i],abs(s-i)*2);
}
// l[++m]=mp(abs(s-i)*2,mp(w,fr[i]));
dij();
// printf("%d\n",ans);
// for(ll i=1;i<=m;++i){
// x=l[i].sn.fs;
// y=l[i].sn.sn;
// x=find(x);
// y=find(y);
// if(x==y)continue;
// ans+=l[i].fs;
// fa[x]=y;
// }
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15976 KB |
Output is correct |
2 |
Correct |
5 ms |
15956 KB |
Output is correct |
3 |
Correct |
6 ms |
15956 KB |
Output is correct |
4 |
Correct |
5 ms |
15936 KB |
Output is correct |
5 |
Correct |
5 ms |
15960 KB |
Output is correct |
6 |
Correct |
5 ms |
15956 KB |
Output is correct |
7 |
Correct |
6 ms |
15956 KB |
Output is correct |
8 |
Correct |
5 ms |
15956 KB |
Output is correct |
9 |
Correct |
6 ms |
15936 KB |
Output is correct |
10 |
Correct |
5 ms |
15956 KB |
Output is correct |
11 |
Correct |
5 ms |
15956 KB |
Output is correct |
12 |
Correct |
5 ms |
15956 KB |
Output is correct |
13 |
Correct |
5 ms |
15956 KB |
Output is correct |
14 |
Correct |
5 ms |
15936 KB |
Output is correct |
15 |
Correct |
5 ms |
15956 KB |
Output is correct |
16 |
Correct |
5 ms |
15956 KB |
Output is correct |
17 |
Correct |
5 ms |
15956 KB |
Output is correct |
18 |
Correct |
5 ms |
15956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15976 KB |
Output is correct |
2 |
Correct |
5 ms |
15956 KB |
Output is correct |
3 |
Correct |
6 ms |
15956 KB |
Output is correct |
4 |
Correct |
5 ms |
15936 KB |
Output is correct |
5 |
Correct |
5 ms |
15960 KB |
Output is correct |
6 |
Correct |
5 ms |
15956 KB |
Output is correct |
7 |
Correct |
6 ms |
15956 KB |
Output is correct |
8 |
Correct |
5 ms |
15956 KB |
Output is correct |
9 |
Correct |
6 ms |
15936 KB |
Output is correct |
10 |
Correct |
5 ms |
15956 KB |
Output is correct |
11 |
Correct |
5 ms |
15956 KB |
Output is correct |
12 |
Correct |
5 ms |
15956 KB |
Output is correct |
13 |
Correct |
5 ms |
15956 KB |
Output is correct |
14 |
Correct |
5 ms |
15936 KB |
Output is correct |
15 |
Correct |
5 ms |
15956 KB |
Output is correct |
16 |
Correct |
5 ms |
15956 KB |
Output is correct |
17 |
Correct |
5 ms |
15956 KB |
Output is correct |
18 |
Correct |
5 ms |
15956 KB |
Output is correct |
19 |
Correct |
13 ms |
24700 KB |
Output is correct |
20 |
Correct |
13 ms |
23156 KB |
Output is correct |
21 |
Correct |
6 ms |
15924 KB |
Output is correct |
22 |
Correct |
14 ms |
19788 KB |
Output is correct |
23 |
Correct |
15 ms |
20372 KB |
Output is correct |
24 |
Correct |
18 ms |
22824 KB |
Output is correct |
25 |
Correct |
20 ms |
23756 KB |
Output is correct |
26 |
Correct |
18 ms |
27536 KB |
Output is correct |
27 |
Correct |
17 ms |
27132 KB |
Output is correct |
28 |
Correct |
16 ms |
27092 KB |
Output is correct |
29 |
Correct |
13 ms |
19316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15976 KB |
Output is correct |
2 |
Correct |
5 ms |
15956 KB |
Output is correct |
3 |
Correct |
6 ms |
15956 KB |
Output is correct |
4 |
Correct |
5 ms |
15936 KB |
Output is correct |
5 |
Correct |
5 ms |
15960 KB |
Output is correct |
6 |
Correct |
5 ms |
15956 KB |
Output is correct |
7 |
Correct |
6 ms |
15956 KB |
Output is correct |
8 |
Correct |
5 ms |
15956 KB |
Output is correct |
9 |
Correct |
6 ms |
15936 KB |
Output is correct |
10 |
Correct |
5 ms |
15956 KB |
Output is correct |
11 |
Correct |
5 ms |
15956 KB |
Output is correct |
12 |
Correct |
5 ms |
15956 KB |
Output is correct |
13 |
Correct |
5 ms |
15956 KB |
Output is correct |
14 |
Correct |
5 ms |
15936 KB |
Output is correct |
15 |
Correct |
5 ms |
15956 KB |
Output is correct |
16 |
Correct |
5 ms |
15956 KB |
Output is correct |
17 |
Correct |
5 ms |
15956 KB |
Output is correct |
18 |
Correct |
5 ms |
15956 KB |
Output is correct |
19 |
Correct |
13 ms |
24700 KB |
Output is correct |
20 |
Correct |
13 ms |
23156 KB |
Output is correct |
21 |
Correct |
6 ms |
15924 KB |
Output is correct |
22 |
Correct |
14 ms |
19788 KB |
Output is correct |
23 |
Correct |
15 ms |
20372 KB |
Output is correct |
24 |
Correct |
18 ms |
22824 KB |
Output is correct |
25 |
Correct |
20 ms |
23756 KB |
Output is correct |
26 |
Correct |
18 ms |
27536 KB |
Output is correct |
27 |
Correct |
17 ms |
27132 KB |
Output is correct |
28 |
Correct |
16 ms |
27092 KB |
Output is correct |
29 |
Correct |
13 ms |
19316 KB |
Output is correct |
30 |
Runtime error |
286 ms |
239068 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
18876 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15976 KB |
Output is correct |
2 |
Correct |
5 ms |
15956 KB |
Output is correct |
3 |
Correct |
6 ms |
15956 KB |
Output is correct |
4 |
Correct |
5 ms |
15936 KB |
Output is correct |
5 |
Correct |
5 ms |
15960 KB |
Output is correct |
6 |
Correct |
5 ms |
15956 KB |
Output is correct |
7 |
Correct |
6 ms |
15956 KB |
Output is correct |
8 |
Correct |
5 ms |
15956 KB |
Output is correct |
9 |
Correct |
6 ms |
15936 KB |
Output is correct |
10 |
Correct |
5 ms |
15956 KB |
Output is correct |
11 |
Correct |
5 ms |
15956 KB |
Output is correct |
12 |
Correct |
5 ms |
15956 KB |
Output is correct |
13 |
Correct |
5 ms |
15956 KB |
Output is correct |
14 |
Correct |
5 ms |
15936 KB |
Output is correct |
15 |
Correct |
5 ms |
15956 KB |
Output is correct |
16 |
Correct |
5 ms |
15956 KB |
Output is correct |
17 |
Correct |
5 ms |
15956 KB |
Output is correct |
18 |
Correct |
5 ms |
15956 KB |
Output is correct |
19 |
Correct |
13 ms |
24700 KB |
Output is correct |
20 |
Correct |
13 ms |
23156 KB |
Output is correct |
21 |
Correct |
6 ms |
15924 KB |
Output is correct |
22 |
Correct |
14 ms |
19788 KB |
Output is correct |
23 |
Correct |
15 ms |
20372 KB |
Output is correct |
24 |
Correct |
18 ms |
22824 KB |
Output is correct |
25 |
Correct |
20 ms |
23756 KB |
Output is correct |
26 |
Correct |
18 ms |
27536 KB |
Output is correct |
27 |
Correct |
17 ms |
27132 KB |
Output is correct |
28 |
Correct |
16 ms |
27092 KB |
Output is correct |
29 |
Correct |
13 ms |
19316 KB |
Output is correct |
30 |
Runtime error |
286 ms |
239068 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |