#include <bits/stdc++.h>
#include "books.h"
//#include "Lgrader.cpp"
using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);
int n;
int p[MAXN];
bool used[MAXN];
pair<int, int> dfs(int u)
{
used[u] = true;
pair<int, int> ret = {u, u};
if(!used[p[u]])
{
pair<int, int> oth = dfs(p[u]);
chkmax(ret.second, oth.second);
chkmin(ret.first, oth.first);
}
return ret;
}
long long minimum_walk(std::vector<int> p, int s)
{
if(s != 0) return -1;
n = p.size();
for(int i = 0; i < n; i++)
::p[i] = p[i];
int64_t answer = 0;
for(int i = 0; i < n; i++)
answer += abs(i - p[i]);
vector<pair<int, int> > li;
for(int i = 0; i < n; i++)
if(i != p[i] && !used[i])
li.push_back(dfs(i));
sort(li.begin(), li.end());
vector<pair<int, int> > tmp;
for(int i = 0; i < (int)li.size(); i++)
if(tmp.empty() || tmp.back().second < li[i].first) tmp.push_back(li[i]);
else chkmax(tmp[tmp.size() - 1].second, li[i].second);
li = tmp;
int last = 0;
for(auto it: li)
{
answer += 2 * (it.first - last);
last = it.second;
}
return answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 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 |
3 ms |
676 KB |
Output is correct |
15 |
Correct |
3 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 |
2 ms |
676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 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 |
3 ms |
676 KB |
Output is correct |
15 |
Correct |
3 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 |
2 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 |
2 ms |
676 KB |
Output is correct |
23 |
Correct |
3 ms |
676 KB |
Output is correct |
24 |
Correct |
3 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 |
676 KB |
Output is correct |
29 |
Correct |
3 ms |
676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 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 |
3 ms |
676 KB |
Output is correct |
15 |
Correct |
3 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 |
2 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 |
2 ms |
676 KB |
Output is correct |
23 |
Correct |
3 ms |
676 KB |
Output is correct |
24 |
Correct |
3 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 |
676 KB |
Output is correct |
29 |
Correct |
3 ms |
676 KB |
Output is correct |
30 |
Correct |
312 ms |
25596 KB |
Output is correct |
31 |
Correct |
269 ms |
25596 KB |
Output is correct |
32 |
Correct |
215 ms |
25596 KB |
Output is correct |
33 |
Correct |
202 ms |
25596 KB |
Output is correct |
34 |
Correct |
227 ms |
25596 KB |
Output is correct |
35 |
Correct |
176 ms |
25596 KB |
Output is correct |
36 |
Correct |
173 ms |
25596 KB |
Output is correct |
37 |
Correct |
215 ms |
25596 KB |
Output is correct |
38 |
Correct |
160 ms |
25596 KB |
Output is correct |
39 |
Correct |
257 ms |
25596 KB |
Output is correct |
40 |
Correct |
169 ms |
25596 KB |
Output is correct |
41 |
Correct |
212 ms |
25596 KB |
Output is correct |
42 |
Correct |
189 ms |
25596 KB |
Output is correct |
43 |
Correct |
212 ms |
25596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
25596 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '-1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 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 |
3 ms |
676 KB |
Output is correct |
15 |
Correct |
3 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 |
2 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 |
2 ms |
676 KB |
Output is correct |
23 |
Correct |
3 ms |
676 KB |
Output is correct |
24 |
Correct |
3 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 |
676 KB |
Output is correct |
29 |
Correct |
3 ms |
676 KB |
Output is correct |
30 |
Correct |
312 ms |
25596 KB |
Output is correct |
31 |
Correct |
269 ms |
25596 KB |
Output is correct |
32 |
Correct |
215 ms |
25596 KB |
Output is correct |
33 |
Correct |
202 ms |
25596 KB |
Output is correct |
34 |
Correct |
227 ms |
25596 KB |
Output is correct |
35 |
Correct |
176 ms |
25596 KB |
Output is correct |
36 |
Correct |
173 ms |
25596 KB |
Output is correct |
37 |
Correct |
215 ms |
25596 KB |
Output is correct |
38 |
Correct |
160 ms |
25596 KB |
Output is correct |
39 |
Correct |
257 ms |
25596 KB |
Output is correct |
40 |
Correct |
169 ms |
25596 KB |
Output is correct |
41 |
Correct |
212 ms |
25596 KB |
Output is correct |
42 |
Correct |
189 ms |
25596 KB |
Output is correct |
43 |
Correct |
212 ms |
25596 KB |
Output is correct |
44 |
Incorrect |
2 ms |
25596 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '-1' |
45 |
Halted |
0 ms |
0 KB |
- |