#include <bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long int ll;
typedef complex<double> point;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
//#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
const int N=1e6+10;
ll vis[N];
long long minimum_walk(std::vector<int32_t> p, int32_t s) {
memset(vis,0,sizeof vis);
vector <pii> baz;
ll n=p.size();
ll ans=0;
for (int i=0;i<n;i++){
// cout << i << " rf" << endl;
if (vis[i] || p[i]==i) continue;
ll mn=i,mx=i;
ll j=p[i];
ans+=abs(p[i]-i);
while(j!=i){
ans+=abs(p[j]-j);
// cout << j << " " << p[j] << endl;
mn=min(mn,j);
mx=max(mx,j);
vis[j]=1;
j=p[j];
}
vis[i]=1;
baz.pb({mn,-mx});
}
// cout << ans << endl;
sort(baz.begin(),baz.end());
vector <pii> a;
if (baz.size()){
a.pb(baz[0]);
for (int i=1;i<baz.size();i++){
pii p=baz[i],q=a.back();
if (-p.S<=-q.S) continue;
if (p.F<=-q.S){
a.pop_back();
q.S=p.S;
a.pb(q);
}
else
a.pb(baz[i]);
}
if (a[0].F>s){
ans+=(a[0].F-s)*2;
}
if (-a.back().S<s){
ans+=(s+a.back().S)*2;
}
for (int i=1;i<a.size();i++){
ans+=(a[i].F+a[i-1].S)*2;
}
}
return ans;
}
/*
int32_t main() {
int32_t n, s;
assert(2 == scanf("%d %d", &n, &s));
vector<int32_t> p((unsigned) n);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &p[(unsigned) i]));
long long res = minimum_walk(p, s);
printf("%lld\n", res);
return 0;
}
*/
/*
4 0
0 2 3 1
*/
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int32_t)':
books.cpp:47:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i=1;i<baz.size();i++){
| ~^~~~~~~~~~~
books.cpp:64:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i=1;i<a.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8012 KB |
Output is correct |
2 |
Correct |
5 ms |
8012 KB |
Output is correct |
3 |
Correct |
4 ms |
8012 KB |
Output is correct |
4 |
Correct |
4 ms |
8012 KB |
Output is correct |
5 |
Correct |
5 ms |
8064 KB |
Output is correct |
6 |
Correct |
4 ms |
8012 KB |
Output is correct |
7 |
Correct |
4 ms |
8012 KB |
Output is correct |
8 |
Correct |
4 ms |
8012 KB |
Output is correct |
9 |
Correct |
4 ms |
8012 KB |
Output is correct |
10 |
Correct |
4 ms |
8012 KB |
Output is correct |
11 |
Correct |
4 ms |
8012 KB |
Output is correct |
12 |
Correct |
4 ms |
8012 KB |
Output is correct |
13 |
Correct |
6 ms |
8016 KB |
Output is correct |
14 |
Correct |
4 ms |
8012 KB |
Output is correct |
15 |
Correct |
4 ms |
8012 KB |
Output is correct |
16 |
Correct |
4 ms |
8012 KB |
Output is correct |
17 |
Correct |
5 ms |
8012 KB |
Output is correct |
18 |
Correct |
4 ms |
8012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8012 KB |
Output is correct |
2 |
Correct |
5 ms |
8012 KB |
Output is correct |
3 |
Correct |
4 ms |
8012 KB |
Output is correct |
4 |
Correct |
4 ms |
8012 KB |
Output is correct |
5 |
Correct |
5 ms |
8064 KB |
Output is correct |
6 |
Correct |
4 ms |
8012 KB |
Output is correct |
7 |
Correct |
4 ms |
8012 KB |
Output is correct |
8 |
Correct |
4 ms |
8012 KB |
Output is correct |
9 |
Correct |
4 ms |
8012 KB |
Output is correct |
10 |
Correct |
4 ms |
8012 KB |
Output is correct |
11 |
Correct |
4 ms |
8012 KB |
Output is correct |
12 |
Correct |
4 ms |
8012 KB |
Output is correct |
13 |
Correct |
6 ms |
8016 KB |
Output is correct |
14 |
Correct |
4 ms |
8012 KB |
Output is correct |
15 |
Correct |
4 ms |
8012 KB |
Output is correct |
16 |
Correct |
4 ms |
8012 KB |
Output is correct |
17 |
Correct |
5 ms |
8012 KB |
Output is correct |
18 |
Correct |
4 ms |
8012 KB |
Output is correct |
19 |
Correct |
4 ms |
8012 KB |
Output is correct |
20 |
Correct |
4 ms |
8012 KB |
Output is correct |
21 |
Correct |
4 ms |
8012 KB |
Output is correct |
22 |
Correct |
4 ms |
8140 KB |
Output is correct |
23 |
Correct |
4 ms |
8012 KB |
Output is correct |
24 |
Correct |
4 ms |
8140 KB |
Output is correct |
25 |
Correct |
4 ms |
8012 KB |
Output is correct |
26 |
Correct |
4 ms |
8012 KB |
Output is correct |
27 |
Correct |
5 ms |
8012 KB |
Output is correct |
28 |
Correct |
4 ms |
8012 KB |
Output is correct |
29 |
Correct |
4 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8012 KB |
Output is correct |
2 |
Correct |
5 ms |
8012 KB |
Output is correct |
3 |
Correct |
4 ms |
8012 KB |
Output is correct |
4 |
Correct |
4 ms |
8012 KB |
Output is correct |
5 |
Correct |
5 ms |
8064 KB |
Output is correct |
6 |
Correct |
4 ms |
8012 KB |
Output is correct |
7 |
Correct |
4 ms |
8012 KB |
Output is correct |
8 |
Correct |
4 ms |
8012 KB |
Output is correct |
9 |
Correct |
4 ms |
8012 KB |
Output is correct |
10 |
Correct |
4 ms |
8012 KB |
Output is correct |
11 |
Correct |
4 ms |
8012 KB |
Output is correct |
12 |
Correct |
4 ms |
8012 KB |
Output is correct |
13 |
Correct |
6 ms |
8016 KB |
Output is correct |
14 |
Correct |
4 ms |
8012 KB |
Output is correct |
15 |
Correct |
4 ms |
8012 KB |
Output is correct |
16 |
Correct |
4 ms |
8012 KB |
Output is correct |
17 |
Correct |
5 ms |
8012 KB |
Output is correct |
18 |
Correct |
4 ms |
8012 KB |
Output is correct |
19 |
Correct |
4 ms |
8012 KB |
Output is correct |
20 |
Correct |
4 ms |
8012 KB |
Output is correct |
21 |
Correct |
4 ms |
8012 KB |
Output is correct |
22 |
Correct |
4 ms |
8140 KB |
Output is correct |
23 |
Correct |
4 ms |
8012 KB |
Output is correct |
24 |
Correct |
4 ms |
8140 KB |
Output is correct |
25 |
Correct |
4 ms |
8012 KB |
Output is correct |
26 |
Correct |
4 ms |
8012 KB |
Output is correct |
27 |
Correct |
5 ms |
8012 KB |
Output is correct |
28 |
Correct |
4 ms |
8012 KB |
Output is correct |
29 |
Correct |
4 ms |
8140 KB |
Output is correct |
30 |
Correct |
177 ms |
15856 KB |
Output is correct |
31 |
Correct |
174 ms |
16072 KB |
Output is correct |
32 |
Correct |
116 ms |
15976 KB |
Output is correct |
33 |
Correct |
138 ms |
25208 KB |
Output is correct |
34 |
Correct |
133 ms |
25192 KB |
Output is correct |
35 |
Correct |
141 ms |
25568 KB |
Output is correct |
36 |
Correct |
133 ms |
21848 KB |
Output is correct |
37 |
Correct |
123 ms |
16636 KB |
Output is correct |
38 |
Correct |
123 ms |
16252 KB |
Output is correct |
39 |
Correct |
124 ms |
16248 KB |
Output is correct |
40 |
Correct |
128 ms |
15980 KB |
Output is correct |
41 |
Correct |
146 ms |
15980 KB |
Output is correct |
42 |
Correct |
136 ms |
16068 KB |
Output is correct |
43 |
Correct |
136 ms |
20276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
8012 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8012 KB |
Output is correct |
2 |
Correct |
5 ms |
8012 KB |
Output is correct |
3 |
Correct |
4 ms |
8012 KB |
Output is correct |
4 |
Correct |
4 ms |
8012 KB |
Output is correct |
5 |
Correct |
5 ms |
8064 KB |
Output is correct |
6 |
Correct |
4 ms |
8012 KB |
Output is correct |
7 |
Correct |
4 ms |
8012 KB |
Output is correct |
8 |
Correct |
4 ms |
8012 KB |
Output is correct |
9 |
Correct |
4 ms |
8012 KB |
Output is correct |
10 |
Correct |
4 ms |
8012 KB |
Output is correct |
11 |
Correct |
4 ms |
8012 KB |
Output is correct |
12 |
Correct |
4 ms |
8012 KB |
Output is correct |
13 |
Correct |
6 ms |
8016 KB |
Output is correct |
14 |
Correct |
4 ms |
8012 KB |
Output is correct |
15 |
Correct |
4 ms |
8012 KB |
Output is correct |
16 |
Correct |
4 ms |
8012 KB |
Output is correct |
17 |
Correct |
5 ms |
8012 KB |
Output is correct |
18 |
Correct |
4 ms |
8012 KB |
Output is correct |
19 |
Correct |
4 ms |
8012 KB |
Output is correct |
20 |
Correct |
4 ms |
8012 KB |
Output is correct |
21 |
Correct |
4 ms |
8012 KB |
Output is correct |
22 |
Correct |
4 ms |
8140 KB |
Output is correct |
23 |
Correct |
4 ms |
8012 KB |
Output is correct |
24 |
Correct |
4 ms |
8140 KB |
Output is correct |
25 |
Correct |
4 ms |
8012 KB |
Output is correct |
26 |
Correct |
4 ms |
8012 KB |
Output is correct |
27 |
Correct |
5 ms |
8012 KB |
Output is correct |
28 |
Correct |
4 ms |
8012 KB |
Output is correct |
29 |
Correct |
4 ms |
8140 KB |
Output is correct |
30 |
Correct |
177 ms |
15856 KB |
Output is correct |
31 |
Correct |
174 ms |
16072 KB |
Output is correct |
32 |
Correct |
116 ms |
15976 KB |
Output is correct |
33 |
Correct |
138 ms |
25208 KB |
Output is correct |
34 |
Correct |
133 ms |
25192 KB |
Output is correct |
35 |
Correct |
141 ms |
25568 KB |
Output is correct |
36 |
Correct |
133 ms |
21848 KB |
Output is correct |
37 |
Correct |
123 ms |
16636 KB |
Output is correct |
38 |
Correct |
123 ms |
16252 KB |
Output is correct |
39 |
Correct |
124 ms |
16248 KB |
Output is correct |
40 |
Correct |
128 ms |
15980 KB |
Output is correct |
41 |
Correct |
146 ms |
15980 KB |
Output is correct |
42 |
Correct |
136 ms |
16068 KB |
Output is correct |
43 |
Correct |
136 ms |
20276 KB |
Output is correct |
44 |
Incorrect |
5 ms |
8012 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |