#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, a, b) for(int i=a; i<b; i++)
#define ffi For(i, 0, N)
#define ffj For(j, 0, M)
#define ffa ffi ffj
//#define s <<" "<<
#define c <<" : "<<
#define w cout
#define e "\n"
#define pb push_back
#define mp make_pair
#define a first
#define b second
//#define int ll
const int MAXN = 1000000;
//Global Variables
ll N, out = 0, loc[MAXN], comp[MAXN], at = 0;
/// element , element
pair<int, int> rng[MAXN];
/// comp
vector<pair<int, int> > rng2;
bool vis[MAXN];
/// element , comp
vector<int> elements[MAXN];
void go1(int at, int co) {
if (comp[at] != -1) return;
rng[co].a = min(rng[co].a, at);
rng[co].b = max(rng[co].b, at);
comp[at] = -2;
elements[co].pb(at);
out += abs(loc[at]-at);
go1(loc[at], co);
}
void go(int at) {
if (vis[at]) return;
vis[at] = true;
go(loc[at]);
}
void chg(int a, int b) {
for (int i: elements[a]) comp[i] = b;
}
ll minimum_walk(vector<int> p, int s) {
N = p.size();
ffi {rng[i] = mp(N, -1); comp[i] = -1; loc[i] = p[i];}
ffi if (comp[i] == -1) {
go1(i, at);
at++;
}
//w<< "from components: " << out<<e;
sort(rng, rng+at);
For (i, 0, at) {
/// only one element - delete
if (rng[i].a == rng[i].b) continue;
/// vector is empty
if (rng2.empty()) {
rng2.pb(rng[i]);
chg(i, 0);
continue;
}
/// fully emcompassed - delete
/// intersect - combine
if (rng[i].a < rng2.back().b && rng2.back().b < rng[i].b) {
chg(i, rng2.size()-1);
rng2.back().b = rng[i].b;
}
/// don't intersect
if (rng2.back().b < rng[i].a) {
chg(i, rng2.size());
rng2.pb(rng[i]);
}
}
at = rng2.size();
For (i, 0, at) {
rng[i] = rng2[i];
//w<< rng[i].a << " " << rng[i].b<<e;
}
bool lef, right, inter = false;
For (i, 0, at) {
if (rng[i].a > s) right = true;
if (rng[i].b < s) lef = true;
if (rng[i].a <= s && s <= rng[i].b) inter = true;
}
int a1, a2;
if (inter) {
int dist = N+1;
ffi if (comp[i] != -2) {
if (dist > abs(s-i)) {
dist = min(dist, abs(s-i));
a1 = rng[comp[i]].a;
a2 = rng[comp[i]].b;
}
}
//w<< "dist: " << dist<<e;
if (dist == N+1) dist = 0;
out += 2*dist;
}
if (right) {
int far = s;
if (inter) {
far = a2;
}
For (i, 0, at) if (rng[i].a > s) {
out += 2*(rng[i].a - far);
far = rng[i].b;
}
}
if (left) {
int far = s;
if (inter) {
far = a1;
}
for (int i=at-1; i>=0; i--) if (rng[i].b < s) {
out += 2*(far - rng[i].b);
far = rng[i].a;
//w<< out<<e;
}
}
return out;
}
Compilation message
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:82:7: warning: variable 'lef' set but not used [-Wunused-but-set-variable]
bool lef, right, inter = false;
^~~
books.cpp:122:27: warning: 'far' may be used uninitialized in this function [-Wmaybe-uninitialized]
out += 2*(far - rng[i].b);
~~~~~^~~~~~~~~~~
books.cpp:111:32: warning: 'far' may be used uninitialized in this function [-Wmaybe-uninitialized]
out += 2*(rng[i].a - far);
~~~~~~~~~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23808 KB |
Output is correct |
2 |
Correct |
21 ms |
23808 KB |
Output is correct |
3 |
Correct |
20 ms |
23808 KB |
Output is correct |
4 |
Correct |
20 ms |
23808 KB |
Output is correct |
5 |
Correct |
21 ms |
23808 KB |
Output is correct |
6 |
Correct |
21 ms |
23808 KB |
Output is correct |
7 |
Correct |
21 ms |
23808 KB |
Output is correct |
8 |
Correct |
20 ms |
23808 KB |
Output is correct |
9 |
Correct |
21 ms |
23808 KB |
Output is correct |
10 |
Correct |
21 ms |
23808 KB |
Output is correct |
11 |
Correct |
20 ms |
23808 KB |
Output is correct |
12 |
Correct |
20 ms |
23808 KB |
Output is correct |
13 |
Correct |
20 ms |
23800 KB |
Output is correct |
14 |
Correct |
21 ms |
23800 KB |
Output is correct |
15 |
Correct |
21 ms |
23808 KB |
Output is correct |
16 |
Correct |
20 ms |
23808 KB |
Output is correct |
17 |
Correct |
21 ms |
23808 KB |
Output is correct |
18 |
Correct |
21 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23808 KB |
Output is correct |
2 |
Correct |
21 ms |
23808 KB |
Output is correct |
3 |
Correct |
20 ms |
23808 KB |
Output is correct |
4 |
Correct |
20 ms |
23808 KB |
Output is correct |
5 |
Correct |
21 ms |
23808 KB |
Output is correct |
6 |
Correct |
21 ms |
23808 KB |
Output is correct |
7 |
Correct |
21 ms |
23808 KB |
Output is correct |
8 |
Correct |
20 ms |
23808 KB |
Output is correct |
9 |
Correct |
21 ms |
23808 KB |
Output is correct |
10 |
Correct |
21 ms |
23808 KB |
Output is correct |
11 |
Correct |
20 ms |
23808 KB |
Output is correct |
12 |
Correct |
20 ms |
23808 KB |
Output is correct |
13 |
Correct |
20 ms |
23800 KB |
Output is correct |
14 |
Correct |
21 ms |
23800 KB |
Output is correct |
15 |
Correct |
21 ms |
23808 KB |
Output is correct |
16 |
Correct |
20 ms |
23808 KB |
Output is correct |
17 |
Correct |
21 ms |
23808 KB |
Output is correct |
18 |
Correct |
21 ms |
23808 KB |
Output is correct |
19 |
Correct |
21 ms |
23832 KB |
Output is correct |
20 |
Correct |
22 ms |
23936 KB |
Output is correct |
21 |
Correct |
22 ms |
24064 KB |
Output is correct |
22 |
Correct |
21 ms |
23936 KB |
Output is correct |
23 |
Correct |
20 ms |
23912 KB |
Output is correct |
24 |
Correct |
20 ms |
23928 KB |
Output is correct |
25 |
Correct |
20 ms |
23808 KB |
Output is correct |
26 |
Correct |
22 ms |
23936 KB |
Output is correct |
27 |
Correct |
24 ms |
23936 KB |
Output is correct |
28 |
Correct |
21 ms |
23808 KB |
Output is correct |
29 |
Correct |
21 ms |
23936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23808 KB |
Output is correct |
2 |
Correct |
21 ms |
23808 KB |
Output is correct |
3 |
Correct |
20 ms |
23808 KB |
Output is correct |
4 |
Correct |
20 ms |
23808 KB |
Output is correct |
5 |
Correct |
21 ms |
23808 KB |
Output is correct |
6 |
Correct |
21 ms |
23808 KB |
Output is correct |
7 |
Correct |
21 ms |
23808 KB |
Output is correct |
8 |
Correct |
20 ms |
23808 KB |
Output is correct |
9 |
Correct |
21 ms |
23808 KB |
Output is correct |
10 |
Correct |
21 ms |
23808 KB |
Output is correct |
11 |
Correct |
20 ms |
23808 KB |
Output is correct |
12 |
Correct |
20 ms |
23808 KB |
Output is correct |
13 |
Correct |
20 ms |
23800 KB |
Output is correct |
14 |
Correct |
21 ms |
23800 KB |
Output is correct |
15 |
Correct |
21 ms |
23808 KB |
Output is correct |
16 |
Correct |
20 ms |
23808 KB |
Output is correct |
17 |
Correct |
21 ms |
23808 KB |
Output is correct |
18 |
Correct |
21 ms |
23808 KB |
Output is correct |
19 |
Correct |
21 ms |
23832 KB |
Output is correct |
20 |
Correct |
22 ms |
23936 KB |
Output is correct |
21 |
Correct |
22 ms |
24064 KB |
Output is correct |
22 |
Correct |
21 ms |
23936 KB |
Output is correct |
23 |
Correct |
20 ms |
23912 KB |
Output is correct |
24 |
Correct |
20 ms |
23928 KB |
Output is correct |
25 |
Correct |
20 ms |
23808 KB |
Output is correct |
26 |
Correct |
22 ms |
23936 KB |
Output is correct |
27 |
Correct |
24 ms |
23936 KB |
Output is correct |
28 |
Correct |
21 ms |
23808 KB |
Output is correct |
29 |
Correct |
21 ms |
23936 KB |
Output is correct |
30 |
Correct |
339 ms |
84420 KB |
Output is correct |
31 |
Correct |
345 ms |
72168 KB |
Output is correct |
32 |
Correct |
224 ms |
86420 KB |
Output is correct |
33 |
Correct |
232 ms |
79372 KB |
Output is correct |
34 |
Correct |
238 ms |
79340 KB |
Output is correct |
35 |
Correct |
233 ms |
75120 KB |
Output is correct |
36 |
Correct |
209 ms |
66976 KB |
Output is correct |
37 |
Correct |
191 ms |
61432 KB |
Output is correct |
38 |
Correct |
191 ms |
61048 KB |
Output is correct |
39 |
Correct |
185 ms |
61048 KB |
Output is correct |
40 |
Correct |
203 ms |
61192 KB |
Output is correct |
41 |
Correct |
286 ms |
70152 KB |
Output is correct |
42 |
Correct |
254 ms |
64648 KB |
Output is correct |
43 |
Correct |
234 ms |
78456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
23936 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3590' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23808 KB |
Output is correct |
2 |
Correct |
21 ms |
23808 KB |
Output is correct |
3 |
Correct |
20 ms |
23808 KB |
Output is correct |
4 |
Correct |
20 ms |
23808 KB |
Output is correct |
5 |
Correct |
21 ms |
23808 KB |
Output is correct |
6 |
Correct |
21 ms |
23808 KB |
Output is correct |
7 |
Correct |
21 ms |
23808 KB |
Output is correct |
8 |
Correct |
20 ms |
23808 KB |
Output is correct |
9 |
Correct |
21 ms |
23808 KB |
Output is correct |
10 |
Correct |
21 ms |
23808 KB |
Output is correct |
11 |
Correct |
20 ms |
23808 KB |
Output is correct |
12 |
Correct |
20 ms |
23808 KB |
Output is correct |
13 |
Correct |
20 ms |
23800 KB |
Output is correct |
14 |
Correct |
21 ms |
23800 KB |
Output is correct |
15 |
Correct |
21 ms |
23808 KB |
Output is correct |
16 |
Correct |
20 ms |
23808 KB |
Output is correct |
17 |
Correct |
21 ms |
23808 KB |
Output is correct |
18 |
Correct |
21 ms |
23808 KB |
Output is correct |
19 |
Correct |
21 ms |
23832 KB |
Output is correct |
20 |
Correct |
22 ms |
23936 KB |
Output is correct |
21 |
Correct |
22 ms |
24064 KB |
Output is correct |
22 |
Correct |
21 ms |
23936 KB |
Output is correct |
23 |
Correct |
20 ms |
23912 KB |
Output is correct |
24 |
Correct |
20 ms |
23928 KB |
Output is correct |
25 |
Correct |
20 ms |
23808 KB |
Output is correct |
26 |
Correct |
22 ms |
23936 KB |
Output is correct |
27 |
Correct |
24 ms |
23936 KB |
Output is correct |
28 |
Correct |
21 ms |
23808 KB |
Output is correct |
29 |
Correct |
21 ms |
23936 KB |
Output is correct |
30 |
Correct |
339 ms |
84420 KB |
Output is correct |
31 |
Correct |
345 ms |
72168 KB |
Output is correct |
32 |
Correct |
224 ms |
86420 KB |
Output is correct |
33 |
Correct |
232 ms |
79372 KB |
Output is correct |
34 |
Correct |
238 ms |
79340 KB |
Output is correct |
35 |
Correct |
233 ms |
75120 KB |
Output is correct |
36 |
Correct |
209 ms |
66976 KB |
Output is correct |
37 |
Correct |
191 ms |
61432 KB |
Output is correct |
38 |
Correct |
191 ms |
61048 KB |
Output is correct |
39 |
Correct |
185 ms |
61048 KB |
Output is correct |
40 |
Correct |
203 ms |
61192 KB |
Output is correct |
41 |
Correct |
286 ms |
70152 KB |
Output is correct |
42 |
Correct |
254 ms |
64648 KB |
Output is correct |
43 |
Correct |
234 ms |
78456 KB |
Output is correct |
44 |
Incorrect |
21 ms |
23936 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3590' |
45 |
Halted |
0 ms |
0 KB |
- |