#include "books.h"
#include <vector>
#include <algorithm>
#include <stack>
#include <iostream>
using namespace std;
const int N = 1e6 + 7;
int group[N];
int mx[N];
bool used[N];
int par[N],lpar[N],rpar[N];
int fpar(int v) {
return v == par[v] ? v : par[v] = fpar(par[v]);
}
void merge(int v, int u) {
v = fpar(v);
u = fpar(u);
if (rand() & 1) {
swap(u, v);
}
lpar[v] = min(lpar[v], lpar[u]);
rpar[v] = max(rpar[v], rpar[u]);
par[u] = v;
}
long long minimum_walk(std::vector<int> p, int s) {
long long ans = 0;
for (int i = 0; i < p.size(); ++i)
par[i] = lpar[i] = rpar[i] = i;
for (int i = 0; i < p.size(); ++i) {
int a1 = fpar(i);
int a2 = fpar(p[i]);
ans += abs(i - p[i]);
merge(i, p[i]);
}
stack<int> st;
for (int i = 0; i < p.size(); ++i) {
int P = fpar(i);
if (i == lpar[P]) {
st.push(P);
}
else {
merge(P, st.top());
}
if (i == rpar[P])st.pop();
}
int L = 0, R = p.size() - 1;
for (int i = p.size() - 1; i >= 0; i--) { if (p[i] != i)break; R--; }
for (int i = 0; i < p.size(); ++i) { if (p[i] != i)break; L++; }
int Main = fpar(s);
int cl = lpar[Main];
int cr = rpar[Main];
while (cl > L || cr < R) {
int howl = 0, ul = cl;
while (ul > L) {
ul--;
howl++;
int cur = fpar(ul);
ul = lpar[cur];
if (rpar[cur] > cr)break;
}
int howr = 0, ur = cr;
while (ur < R) {
ur++; howr++;
int cur = fpar(ur);
ur = rpar[cur];
if (lpar[cur] < cl)break;
}
if (fpar(ul) == fpar(ur)) {
int x = fpar(ul);
cl = lpar[x];
cr = rpar[x];
ans += min(howl, howr) * 2;
}
else {
cl = ul; cr = ur;
ans += howl * 2 + howr * 2;
}
}
return ans;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:27:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < p.size(); ++i)
^
books.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < p.size(); ++i) {
^
books.cpp:30:7: warning: unused variable 'a1' [-Wunused-variable]
int a1 = fpar(i);
^
books.cpp:31:7: warning: unused variable 'a2' [-Wunused-variable]
int a2 = fpar(p[i]);
^
books.cpp:36:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < p.size(); ++i) {
^
books.cpp:49:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < p.size(); ++i) { if (p[i] != i)break; L++; }
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
552 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
676 KB |
Output is correct |
8 |
Correct |
2 ms |
676 KB |
Output is correct |
9 |
Correct |
2 ms |
676 KB |
Output is correct |
10 |
Correct |
2 ms |
676 KB |
Output is correct |
11 |
Correct |
2 ms |
676 KB |
Output is correct |
12 |
Correct |
2 ms |
676 KB |
Output is correct |
13 |
Correct |
2 ms |
676 KB |
Output is correct |
14 |
Correct |
2 ms |
676 KB |
Output is correct |
15 |
Correct |
2 ms |
676 KB |
Output is correct |
16 |
Correct |
2 ms |
676 KB |
Output is correct |
17 |
Correct |
2 ms |
676 KB |
Output is correct |
18 |
Correct |
1 ms |
676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
552 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
676 KB |
Output is correct |
8 |
Correct |
2 ms |
676 KB |
Output is correct |
9 |
Correct |
2 ms |
676 KB |
Output is correct |
10 |
Correct |
2 ms |
676 KB |
Output is correct |
11 |
Correct |
2 ms |
676 KB |
Output is correct |
12 |
Correct |
2 ms |
676 KB |
Output is correct |
13 |
Correct |
2 ms |
676 KB |
Output is correct |
14 |
Correct |
2 ms |
676 KB |
Output is correct |
15 |
Correct |
2 ms |
676 KB |
Output is correct |
16 |
Correct |
2 ms |
676 KB |
Output is correct |
17 |
Correct |
2 ms |
676 KB |
Output is correct |
18 |
Correct |
1 ms |
676 KB |
Output is correct |
19 |
Correct |
2 ms |
676 KB |
Output is correct |
20 |
Correct |
2 ms |
676 KB |
Output is correct |
21 |
Correct |
2 ms |
676 KB |
Output is correct |
22 |
Correct |
1 ms |
676 KB |
Output is correct |
23 |
Correct |
2 ms |
676 KB |
Output is correct |
24 |
Correct |
2 ms |
676 KB |
Output is correct |
25 |
Correct |
2 ms |
676 KB |
Output is correct |
26 |
Correct |
2 ms |
676 KB |
Output is correct |
27 |
Correct |
2 ms |
676 KB |
Output is correct |
28 |
Correct |
2 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
552 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
676 KB |
Output is correct |
8 |
Correct |
2 ms |
676 KB |
Output is correct |
9 |
Correct |
2 ms |
676 KB |
Output is correct |
10 |
Correct |
2 ms |
676 KB |
Output is correct |
11 |
Correct |
2 ms |
676 KB |
Output is correct |
12 |
Correct |
2 ms |
676 KB |
Output is correct |
13 |
Correct |
2 ms |
676 KB |
Output is correct |
14 |
Correct |
2 ms |
676 KB |
Output is correct |
15 |
Correct |
2 ms |
676 KB |
Output is correct |
16 |
Correct |
2 ms |
676 KB |
Output is correct |
17 |
Correct |
2 ms |
676 KB |
Output is correct |
18 |
Correct |
1 ms |
676 KB |
Output is correct |
19 |
Correct |
2 ms |
676 KB |
Output is correct |
20 |
Correct |
2 ms |
676 KB |
Output is correct |
21 |
Correct |
2 ms |
676 KB |
Output is correct |
22 |
Correct |
1 ms |
676 KB |
Output is correct |
23 |
Correct |
2 ms |
676 KB |
Output is correct |
24 |
Correct |
2 ms |
676 KB |
Output is correct |
25 |
Correct |
2 ms |
676 KB |
Output is correct |
26 |
Correct |
2 ms |
676 KB |
Output is correct |
27 |
Correct |
2 ms |
676 KB |
Output is correct |
28 |
Correct |
2 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
716 KB |
Output is correct |
30 |
Correct |
426 ms |
20204 KB |
Output is correct |
31 |
Correct |
416 ms |
20204 KB |
Output is correct |
32 |
Correct |
186 ms |
20204 KB |
Output is correct |
33 |
Incorrect |
213 ms |
20336 KB |
3rd lines differ - on the 1st token, expected: '2121672', found: '2121766' |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
20336 KB |
Output is correct |
2 |
Correct |
2 ms |
20336 KB |
Output is correct |
3 |
Correct |
2 ms |
20336 KB |
Output is correct |
4 |
Correct |
2 ms |
20336 KB |
Output is correct |
5 |
Correct |
2 ms |
20336 KB |
Output is correct |
6 |
Correct |
2 ms |
20336 KB |
Output is correct |
7 |
Correct |
2 ms |
20336 KB |
Output is correct |
8 |
Correct |
2 ms |
20336 KB |
Output is correct |
9 |
Correct |
2 ms |
20336 KB |
Output is correct |
10 |
Correct |
2 ms |
20336 KB |
Output is correct |
11 |
Correct |
2 ms |
20336 KB |
Output is correct |
12 |
Correct |
2 ms |
20336 KB |
Output is correct |
13 |
Incorrect |
2 ms |
20336 KB |
3rd lines differ - on the 1st token, expected: '33892', found: '33792' |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
552 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
676 KB |
Output is correct |
8 |
Correct |
2 ms |
676 KB |
Output is correct |
9 |
Correct |
2 ms |
676 KB |
Output is correct |
10 |
Correct |
2 ms |
676 KB |
Output is correct |
11 |
Correct |
2 ms |
676 KB |
Output is correct |
12 |
Correct |
2 ms |
676 KB |
Output is correct |
13 |
Correct |
2 ms |
676 KB |
Output is correct |
14 |
Correct |
2 ms |
676 KB |
Output is correct |
15 |
Correct |
2 ms |
676 KB |
Output is correct |
16 |
Correct |
2 ms |
676 KB |
Output is correct |
17 |
Correct |
2 ms |
676 KB |
Output is correct |
18 |
Correct |
1 ms |
676 KB |
Output is correct |
19 |
Correct |
2 ms |
676 KB |
Output is correct |
20 |
Correct |
2 ms |
676 KB |
Output is correct |
21 |
Correct |
2 ms |
676 KB |
Output is correct |
22 |
Correct |
1 ms |
676 KB |
Output is correct |
23 |
Correct |
2 ms |
676 KB |
Output is correct |
24 |
Correct |
2 ms |
676 KB |
Output is correct |
25 |
Correct |
2 ms |
676 KB |
Output is correct |
26 |
Correct |
2 ms |
676 KB |
Output is correct |
27 |
Correct |
2 ms |
676 KB |
Output is correct |
28 |
Correct |
2 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
716 KB |
Output is correct |
30 |
Correct |
426 ms |
20204 KB |
Output is correct |
31 |
Correct |
416 ms |
20204 KB |
Output is correct |
32 |
Correct |
186 ms |
20204 KB |
Output is correct |
33 |
Incorrect |
213 ms |
20336 KB |
3rd lines differ - on the 1st token, expected: '2121672', found: '2121766' |
34 |
Halted |
0 ms |
0 KB |
- |