//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
#include "books.h"
llo ans=0;
llo n;
vector<llo> adj[1000001];
llo vis[1000001];
pair<llo,llo> cot[1000001];
llo par[1000001];
llo find(llo no){
if(par[no]==no){
return no;
}
par[no]=find(par[no]);
return par[no];
}
vector<llo> ss;
void dfs(int no){
vis[no]=1;
ss.pb(no);
for(auto j:adj[no]){
if(vis[j]==0){
dfs(j);
}
}
}
vector<llo> adj2[1000001];
llo ind[1000001];
pair<llo,llo> pp;
vector<pair<llo,llo>> tt;
llo dp[1001][1001];
pair<llo,llo> rr[1000001];
void dfs2(int no,int par=-1){
pp.a=min(pp.a,tt[no].a);
pp.b=max(pp.b,tt[no].b);
vis[no]=1;
for(auto j:adj2[no]){
if(vis[j]==0){
dfs2(j);
}
}
}
long long minimum_walk(std::vector<int> p, int s) {
while(p.size()){
if(p.back()+1==p.size()){
p.pop_back();
}
else{
break;
}
}
n=p.size();
//cout<<p.size()<<":"<<endl;
if(n==0){
return 0;
}
for(int i=0;i<n;i++){
adj[i].pb(p[i]);
}
//llo co=0;
llo ans=0;
for(int i=0;i<n;i++){
if(vis[i]==0){
ss.clear();
dfs(i);
/*if(ss.size()==0){
continue;
}*/
llo mi=ss[0];
llo ma=ss[0];
for(auto j:ss){
mi=min(mi,j);
ma=max(ma,j);
}
for(auto j:ss){
cot[j]={mi,ma};
}
for(auto j:ss){
ind[j]=tt.size();
}
tt.pb({mi,ma});
//co++;
for(int j=0;j+1<ss.size();j++){
ans+=abs(ss[j]-ss[j+1]);
}
/* for(auto j:ss){
cout<<j<<",,";
}
cout<<endl;*/
ans+=(abs(ss[0]-ss.back()));
}
}
if(s==0){
sort(tt.begin(),tt.end());
vector<pair<llo,llo>> tt2;
for(auto j:tt){
if(tt2.size()==0){
tt2.pb(j);
}
else{
if(tt2.back().b<j.a){
tt2.pb(j);
}
else{
tt2[tt2.size()-1].b=max(tt2[tt2.size()-1].b,j.b);
}
}
}
llo xx=tt2.size();
ans+=2*(xx-1);
//ans+=2*(co-1);
return ans;
}
for(int i=0;i<tt.size();i++){
for(int j=0;j<n;j++){
if(ind[j]!=i){
if(j>=tt[i].a and j<=tt[i].b){
adj2[i].pb(ind[j]);
//cout<<i<<",,"<<ind[j]<<endl;
}
}
}
}
for(int j=0;j<tt.size();j++){
for(int i=0;i<tt.size();i++){
vis[i]=0;
}
pp=tt[j];
dfs2(j);
for(int i=0;i<n;i++){
if(ind[i]==j){
rr[i]=pp;
}
}
//cout<<pp.a<<","<<pp.b<<endl;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
dp[i][j]=-1;
}
}
dp[rr[s].a][rr[s].b]=0;
/* for(int i=0;i<n;i++){
cout<<rr[i].a<<"<"<<rr[i].b<<endl;
}*/
// solve(rr[s].a,rr[s].b);
for(llo ii=0;ii<n;ii++){
for(llo i=0;i+ii<n;i++){
llo j=i+ii;
if(dp[i][j]==-1){
continue;
}
if(i>0){
pair<llo,llo> ac={rr[i-1].a,max(rr[i-1].b,j)};
if(dp[ac.a][ac.b]==-1){
dp[ac.a][ac.b]=dp[i][j]+2;
}
dp[ac.a][ac.b]=min(dp[ac.a][ac.b],dp[i][j]+2);
}
if(j<n-1){
pair<llo,llo> ac={min(rr[j+1].a,i),rr[j+1].b};
if(dp[ac.a][ac.b]==-1){
dp[ac.a][ac.b]=dp[i][j]+2;
}
dp[ac.a][ac.b]=min(dp[ac.a][ac.b],dp[i][j]+2);
}
}
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(rr[i].b<rr[j].a){
continue;
}
if(rr[j].b<rr[i].a){
continue;
}
if(rr[j].a<=rr[i].a and rr[i].b<=rr[j].b){
continue;
}
if(rr[i].a<=rr[j].a and rr[j].b<=rr[i].b){
continue;
}
while(true){
continue;
}
}
}
/*for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<dp[i][j]<<",";
}
cout<<endl;
}
*/
//ans+=2*(co-1);
//cout<<ans<<":"<<endl;
return ans+dp[0][n-1];
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:58:16: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | if(p.back()+1==p.size()){
| ~~~~~~~~~~^~~~~~~~~~
books.cpp:100:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int j=0;j+1<ss.size();j++){
| ~~~^~~~~~~~~~
books.cpp:134:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for(int i=0;i<tt.size();i++){
| ~^~~~~~~~~~
books.cpp:144:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | for(int j=0;j<tt.size();j++){
| ~^~~~~~~~~~
books.cpp:145:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
145 | for(int i=0;i<tt.size();i++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
47340 KB |
Output is correct |
2 |
Correct |
34 ms |
47340 KB |
Output is correct |
3 |
Correct |
33 ms |
47340 KB |
Output is correct |
4 |
Correct |
35 ms |
47320 KB |
Output is correct |
5 |
Correct |
33 ms |
47340 KB |
Output is correct |
6 |
Correct |
33 ms |
47340 KB |
Output is correct |
7 |
Correct |
34 ms |
47340 KB |
Output is correct |
8 |
Correct |
33 ms |
47340 KB |
Output is correct |
9 |
Correct |
35 ms |
47340 KB |
Output is correct |
10 |
Correct |
33 ms |
47340 KB |
Output is correct |
11 |
Correct |
33 ms |
47340 KB |
Output is correct |
12 |
Correct |
32 ms |
47340 KB |
Output is correct |
13 |
Correct |
33 ms |
47340 KB |
Output is correct |
14 |
Correct |
34 ms |
47340 KB |
Output is correct |
15 |
Correct |
33 ms |
47340 KB |
Output is correct |
16 |
Correct |
33 ms |
47340 KB |
Output is correct |
17 |
Correct |
33 ms |
47340 KB |
Output is correct |
18 |
Correct |
33 ms |
47340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
47340 KB |
Output is correct |
2 |
Correct |
34 ms |
47340 KB |
Output is correct |
3 |
Correct |
33 ms |
47340 KB |
Output is correct |
4 |
Correct |
35 ms |
47320 KB |
Output is correct |
5 |
Correct |
33 ms |
47340 KB |
Output is correct |
6 |
Correct |
33 ms |
47340 KB |
Output is correct |
7 |
Correct |
34 ms |
47340 KB |
Output is correct |
8 |
Correct |
33 ms |
47340 KB |
Output is correct |
9 |
Correct |
35 ms |
47340 KB |
Output is correct |
10 |
Correct |
33 ms |
47340 KB |
Output is correct |
11 |
Correct |
33 ms |
47340 KB |
Output is correct |
12 |
Correct |
32 ms |
47340 KB |
Output is correct |
13 |
Correct |
33 ms |
47340 KB |
Output is correct |
14 |
Correct |
34 ms |
47340 KB |
Output is correct |
15 |
Correct |
33 ms |
47340 KB |
Output is correct |
16 |
Correct |
33 ms |
47340 KB |
Output is correct |
17 |
Correct |
33 ms |
47340 KB |
Output is correct |
18 |
Correct |
33 ms |
47340 KB |
Output is correct |
19 |
Correct |
33 ms |
47468 KB |
Output is correct |
20 |
Correct |
33 ms |
47368 KB |
Output is correct |
21 |
Correct |
32 ms |
47340 KB |
Output is correct |
22 |
Correct |
33 ms |
47468 KB |
Output is correct |
23 |
Correct |
33 ms |
47468 KB |
Output is correct |
24 |
Correct |
33 ms |
47468 KB |
Output is correct |
25 |
Correct |
33 ms |
47468 KB |
Output is correct |
26 |
Correct |
33 ms |
47468 KB |
Output is correct |
27 |
Correct |
33 ms |
47340 KB |
Output is correct |
28 |
Correct |
33 ms |
47340 KB |
Output is correct |
29 |
Correct |
34 ms |
47468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
47340 KB |
Output is correct |
2 |
Correct |
34 ms |
47340 KB |
Output is correct |
3 |
Correct |
33 ms |
47340 KB |
Output is correct |
4 |
Correct |
35 ms |
47320 KB |
Output is correct |
5 |
Correct |
33 ms |
47340 KB |
Output is correct |
6 |
Correct |
33 ms |
47340 KB |
Output is correct |
7 |
Correct |
34 ms |
47340 KB |
Output is correct |
8 |
Correct |
33 ms |
47340 KB |
Output is correct |
9 |
Correct |
35 ms |
47340 KB |
Output is correct |
10 |
Correct |
33 ms |
47340 KB |
Output is correct |
11 |
Correct |
33 ms |
47340 KB |
Output is correct |
12 |
Correct |
32 ms |
47340 KB |
Output is correct |
13 |
Correct |
33 ms |
47340 KB |
Output is correct |
14 |
Correct |
34 ms |
47340 KB |
Output is correct |
15 |
Correct |
33 ms |
47340 KB |
Output is correct |
16 |
Correct |
33 ms |
47340 KB |
Output is correct |
17 |
Correct |
33 ms |
47340 KB |
Output is correct |
18 |
Correct |
33 ms |
47340 KB |
Output is correct |
19 |
Correct |
33 ms |
47468 KB |
Output is correct |
20 |
Correct |
33 ms |
47368 KB |
Output is correct |
21 |
Correct |
32 ms |
47340 KB |
Output is correct |
22 |
Correct |
33 ms |
47468 KB |
Output is correct |
23 |
Correct |
33 ms |
47468 KB |
Output is correct |
24 |
Correct |
33 ms |
47468 KB |
Output is correct |
25 |
Correct |
33 ms |
47468 KB |
Output is correct |
26 |
Correct |
33 ms |
47468 KB |
Output is correct |
27 |
Correct |
33 ms |
47340 KB |
Output is correct |
28 |
Correct |
33 ms |
47340 KB |
Output is correct |
29 |
Correct |
34 ms |
47468 KB |
Output is correct |
30 |
Correct |
561 ms |
161740 KB |
Output is correct |
31 |
Correct |
545 ms |
140120 KB |
Output is correct |
32 |
Correct |
165 ms |
56172 KB |
Output is correct |
33 |
Correct |
310 ms |
154296 KB |
Output is correct |
34 |
Correct |
307 ms |
154300 KB |
Output is correct |
35 |
Correct |
298 ms |
142776 KB |
Output is correct |
36 |
Correct |
284 ms |
128984 KB |
Output is correct |
37 |
Correct |
297 ms |
119660 KB |
Output is correct |
38 |
Correct |
312 ms |
119276 KB |
Output is correct |
39 |
Correct |
312 ms |
119276 KB |
Output is correct |
40 |
Correct |
331 ms |
120936 KB |
Output is correct |
41 |
Correct |
471 ms |
137336 KB |
Output is correct |
42 |
Correct |
441 ms |
127584 KB |
Output is correct |
43 |
Correct |
305 ms |
130640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
55276 KB |
Output is correct |
2 |
Correct |
42 ms |
55276 KB |
Output is correct |
3 |
Correct |
42 ms |
55276 KB |
Output is correct |
4 |
Correct |
43 ms |
55276 KB |
Output is correct |
5 |
Correct |
44 ms |
55276 KB |
Output is correct |
6 |
Correct |
44 ms |
55276 KB |
Output is correct |
7 |
Correct |
43 ms |
55404 KB |
Output is correct |
8 |
Correct |
44 ms |
55404 KB |
Output is correct |
9 |
Correct |
41 ms |
55296 KB |
Output is correct |
10 |
Correct |
34 ms |
47340 KB |
Output is correct |
11 |
Correct |
65 ms |
57580 KB |
Output is correct |
12 |
Correct |
65 ms |
57580 KB |
Output is correct |
13 |
Correct |
41 ms |
55404 KB |
Output is correct |
14 |
Correct |
41 ms |
55404 KB |
Output is correct |
15 |
Correct |
40 ms |
55276 KB |
Output is correct |
16 |
Correct |
40 ms |
55276 KB |
Output is correct |
17 |
Correct |
47 ms |
56044 KB |
Output is correct |
18 |
Correct |
45 ms |
55916 KB |
Output is correct |
19 |
Correct |
43 ms |
55532 KB |
Output is correct |
20 |
Correct |
40 ms |
55276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
47340 KB |
Output is correct |
2 |
Correct |
34 ms |
47340 KB |
Output is correct |
3 |
Correct |
33 ms |
47340 KB |
Output is correct |
4 |
Correct |
35 ms |
47320 KB |
Output is correct |
5 |
Correct |
33 ms |
47340 KB |
Output is correct |
6 |
Correct |
33 ms |
47340 KB |
Output is correct |
7 |
Correct |
34 ms |
47340 KB |
Output is correct |
8 |
Correct |
33 ms |
47340 KB |
Output is correct |
9 |
Correct |
35 ms |
47340 KB |
Output is correct |
10 |
Correct |
33 ms |
47340 KB |
Output is correct |
11 |
Correct |
33 ms |
47340 KB |
Output is correct |
12 |
Correct |
32 ms |
47340 KB |
Output is correct |
13 |
Correct |
33 ms |
47340 KB |
Output is correct |
14 |
Correct |
34 ms |
47340 KB |
Output is correct |
15 |
Correct |
33 ms |
47340 KB |
Output is correct |
16 |
Correct |
33 ms |
47340 KB |
Output is correct |
17 |
Correct |
33 ms |
47340 KB |
Output is correct |
18 |
Correct |
33 ms |
47340 KB |
Output is correct |
19 |
Correct |
33 ms |
47468 KB |
Output is correct |
20 |
Correct |
33 ms |
47368 KB |
Output is correct |
21 |
Correct |
32 ms |
47340 KB |
Output is correct |
22 |
Correct |
33 ms |
47468 KB |
Output is correct |
23 |
Correct |
33 ms |
47468 KB |
Output is correct |
24 |
Correct |
33 ms |
47468 KB |
Output is correct |
25 |
Correct |
33 ms |
47468 KB |
Output is correct |
26 |
Correct |
33 ms |
47468 KB |
Output is correct |
27 |
Correct |
33 ms |
47340 KB |
Output is correct |
28 |
Correct |
33 ms |
47340 KB |
Output is correct |
29 |
Correct |
34 ms |
47468 KB |
Output is correct |
30 |
Correct |
561 ms |
161740 KB |
Output is correct |
31 |
Correct |
545 ms |
140120 KB |
Output is correct |
32 |
Correct |
165 ms |
56172 KB |
Output is correct |
33 |
Correct |
310 ms |
154296 KB |
Output is correct |
34 |
Correct |
307 ms |
154300 KB |
Output is correct |
35 |
Correct |
298 ms |
142776 KB |
Output is correct |
36 |
Correct |
284 ms |
128984 KB |
Output is correct |
37 |
Correct |
297 ms |
119660 KB |
Output is correct |
38 |
Correct |
312 ms |
119276 KB |
Output is correct |
39 |
Correct |
312 ms |
119276 KB |
Output is correct |
40 |
Correct |
331 ms |
120936 KB |
Output is correct |
41 |
Correct |
471 ms |
137336 KB |
Output is correct |
42 |
Correct |
441 ms |
127584 KB |
Output is correct |
43 |
Correct |
305 ms |
130640 KB |
Output is correct |
44 |
Correct |
44 ms |
55276 KB |
Output is correct |
45 |
Correct |
42 ms |
55276 KB |
Output is correct |
46 |
Correct |
42 ms |
55276 KB |
Output is correct |
47 |
Correct |
43 ms |
55276 KB |
Output is correct |
48 |
Correct |
44 ms |
55276 KB |
Output is correct |
49 |
Correct |
44 ms |
55276 KB |
Output is correct |
50 |
Correct |
43 ms |
55404 KB |
Output is correct |
51 |
Correct |
44 ms |
55404 KB |
Output is correct |
52 |
Correct |
41 ms |
55296 KB |
Output is correct |
53 |
Correct |
34 ms |
47340 KB |
Output is correct |
54 |
Correct |
65 ms |
57580 KB |
Output is correct |
55 |
Correct |
65 ms |
57580 KB |
Output is correct |
56 |
Correct |
41 ms |
55404 KB |
Output is correct |
57 |
Correct |
41 ms |
55404 KB |
Output is correct |
58 |
Correct |
40 ms |
55276 KB |
Output is correct |
59 |
Correct |
40 ms |
55276 KB |
Output is correct |
60 |
Correct |
47 ms |
56044 KB |
Output is correct |
61 |
Correct |
45 ms |
55916 KB |
Output is correct |
62 |
Correct |
43 ms |
55532 KB |
Output is correct |
63 |
Correct |
40 ms |
55276 KB |
Output is correct |
64 |
Execution timed out |
2089 ms |
159928 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |