#include "books.h"
#include <vector>
#include <map>
#include <queue>
typedef struct range range;
struct range
{
int e;
long long m;
};
// bool operator<(range a, range b)
// {
// return a.s < b.s;
// }
int vis[1000005];
int still[1000005];
long long abs(long long a)
{
return (a < 0)?-a:a;
}
long long min(long long a, long long b)
{
return (a < b)?a:b;
}
long long max(long long a, long long b)
{
return (a > b)?a:b;
}
long long minimum_walk(std::vector<int> p, int ss) {
std::map<int, range> R;
std::queue<int> Q;
int i, n = p.size();
int s, e;
long long m;
for (i = 0; i < n; i++)
still[i] = 1;
for (i = 0; i < n; i++)
{
if (!vis[i])
{
s = i;
e = i;
m = 0;
while (!Q.empty())
Q.pop();
while (!vis[i])
{
vis[i] = 1;
Q.push(i);
m += abs(p[i] - i);
i = p[i];
s = min(s, i);
e = max(e, i);
}
// printf("%d %d %lld\n", s, e, m);
if (m != 0)
{
R.insert(std::make_pair(s, range{e, m}));
while (!Q.empty())
{
still[Q.front()] = 0;
Q.pop();
}
}
}
}
std::map<int, range>::iterator it, it2;
for (it = R.begin(); it != R.end();)
{
if (it != R.begin() && it->second.e < it2->second.e)
{
it2->second.m += it->second.m;
it = R.erase(it);
}
else if (it != R.begin() && it2->second.e > it->first)
{
it2->second.m += it->second.m;
it2->second.e = it->second.e;
it = R.erase(it);
}
else
{
it2 = it;
it++;
}
}
long long Ans = 0;
// for (i = 0; i < n; i++)
// printf("%d ", still[i]);
// printf("\n");
if (still[ss])
{
for (i = ss; i >= 0 && still[i]; i--);
if (i >= 0) Ans = 2LL*(ss - i);
else Ans = 1e18;
// printf("i%d\n", i);
for (i = ss; i < n && still[i]; i++);
if (i < n) Ans = min(Ans, 2LL*(i - ss));
// printf("i%d\n", i);
}
if (Ans == 1e18)
Ans = 0;
for (it = R.begin(); it != R.end(); it++)
{
// printf("%d %d %lld\n", it->first, it->second.e, it->second.m);
Ans += it->second.m;
if (it != R.begin())
Ans += 2LL*(it->first - it2->second.e);
it2 = it;
}
return Ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
552 KB |
Output is correct |
4 |
Correct |
2 ms |
552 KB |
Output is correct |
5 |
Correct |
1 ms |
628 KB |
Output is correct |
6 |
Correct |
2 ms |
648 KB |
Output is correct |
7 |
Correct |
2 ms |
648 KB |
Output is correct |
8 |
Correct |
1 ms |
648 KB |
Output is correct |
9 |
Correct |
1 ms |
648 KB |
Output is correct |
10 |
Correct |
1 ms |
648 KB |
Output is correct |
11 |
Correct |
1 ms |
648 KB |
Output is correct |
12 |
Correct |
1 ms |
648 KB |
Output is correct |
13 |
Correct |
1 ms |
648 KB |
Output is correct |
14 |
Correct |
1 ms |
648 KB |
Output is correct |
15 |
Correct |
1 ms |
648 KB |
Output is correct |
16 |
Correct |
1 ms |
648 KB |
Output is correct |
17 |
Correct |
1 ms |
648 KB |
Output is correct |
18 |
Correct |
1 ms |
648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
552 KB |
Output is correct |
4 |
Correct |
2 ms |
552 KB |
Output is correct |
5 |
Correct |
1 ms |
628 KB |
Output is correct |
6 |
Correct |
2 ms |
648 KB |
Output is correct |
7 |
Correct |
2 ms |
648 KB |
Output is correct |
8 |
Correct |
1 ms |
648 KB |
Output is correct |
9 |
Correct |
1 ms |
648 KB |
Output is correct |
10 |
Correct |
1 ms |
648 KB |
Output is correct |
11 |
Correct |
1 ms |
648 KB |
Output is correct |
12 |
Correct |
1 ms |
648 KB |
Output is correct |
13 |
Correct |
1 ms |
648 KB |
Output is correct |
14 |
Correct |
1 ms |
648 KB |
Output is correct |
15 |
Correct |
1 ms |
648 KB |
Output is correct |
16 |
Correct |
1 ms |
648 KB |
Output is correct |
17 |
Correct |
1 ms |
648 KB |
Output is correct |
18 |
Correct |
1 ms |
648 KB |
Output is correct |
19 |
Correct |
1 ms |
648 KB |
Output is correct |
20 |
Correct |
1 ms |
648 KB |
Output is correct |
21 |
Correct |
1 ms |
648 KB |
Output is correct |
22 |
Correct |
1 ms |
648 KB |
Output is correct |
23 |
Correct |
1 ms |
648 KB |
Output is correct |
24 |
Correct |
1 ms |
648 KB |
Output is correct |
25 |
Correct |
1 ms |
648 KB |
Output is correct |
26 |
Correct |
2 ms |
648 KB |
Output is correct |
27 |
Correct |
1 ms |
648 KB |
Output is correct |
28 |
Correct |
1 ms |
648 KB |
Output is correct |
29 |
Correct |
1 ms |
648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
552 KB |
Output is correct |
4 |
Correct |
2 ms |
552 KB |
Output is correct |
5 |
Correct |
1 ms |
628 KB |
Output is correct |
6 |
Correct |
2 ms |
648 KB |
Output is correct |
7 |
Correct |
2 ms |
648 KB |
Output is correct |
8 |
Correct |
1 ms |
648 KB |
Output is correct |
9 |
Correct |
1 ms |
648 KB |
Output is correct |
10 |
Correct |
1 ms |
648 KB |
Output is correct |
11 |
Correct |
1 ms |
648 KB |
Output is correct |
12 |
Correct |
1 ms |
648 KB |
Output is correct |
13 |
Correct |
1 ms |
648 KB |
Output is correct |
14 |
Correct |
1 ms |
648 KB |
Output is correct |
15 |
Correct |
1 ms |
648 KB |
Output is correct |
16 |
Correct |
1 ms |
648 KB |
Output is correct |
17 |
Correct |
1 ms |
648 KB |
Output is correct |
18 |
Correct |
1 ms |
648 KB |
Output is correct |
19 |
Correct |
1 ms |
648 KB |
Output is correct |
20 |
Correct |
1 ms |
648 KB |
Output is correct |
21 |
Correct |
1 ms |
648 KB |
Output is correct |
22 |
Correct |
1 ms |
648 KB |
Output is correct |
23 |
Correct |
1 ms |
648 KB |
Output is correct |
24 |
Correct |
1 ms |
648 KB |
Output is correct |
25 |
Correct |
1 ms |
648 KB |
Output is correct |
26 |
Correct |
2 ms |
648 KB |
Output is correct |
27 |
Correct |
1 ms |
648 KB |
Output is correct |
28 |
Correct |
1 ms |
648 KB |
Output is correct |
29 |
Correct |
1 ms |
648 KB |
Output is correct |
30 |
Correct |
304 ms |
19480 KB |
Output is correct |
31 |
Correct |
332 ms |
19480 KB |
Output is correct |
32 |
Correct |
166 ms |
19480 KB |
Output is correct |
33 |
Correct |
232 ms |
28600 KB |
Output is correct |
34 |
Correct |
209 ms |
28600 KB |
Output is correct |
35 |
Correct |
268 ms |
29872 KB |
Output is correct |
36 |
Correct |
249 ms |
29872 KB |
Output is correct |
37 |
Correct |
167 ms |
29872 KB |
Output is correct |
38 |
Correct |
167 ms |
29872 KB |
Output is correct |
39 |
Correct |
183 ms |
29872 KB |
Output is correct |
40 |
Correct |
184 ms |
29872 KB |
Output is correct |
41 |
Correct |
238 ms |
29872 KB |
Output is correct |
42 |
Correct |
219 ms |
29872 KB |
Output is correct |
43 |
Correct |
220 ms |
31852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
31852 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
552 KB |
Output is correct |
4 |
Correct |
2 ms |
552 KB |
Output is correct |
5 |
Correct |
1 ms |
628 KB |
Output is correct |
6 |
Correct |
2 ms |
648 KB |
Output is correct |
7 |
Correct |
2 ms |
648 KB |
Output is correct |
8 |
Correct |
1 ms |
648 KB |
Output is correct |
9 |
Correct |
1 ms |
648 KB |
Output is correct |
10 |
Correct |
1 ms |
648 KB |
Output is correct |
11 |
Correct |
1 ms |
648 KB |
Output is correct |
12 |
Correct |
1 ms |
648 KB |
Output is correct |
13 |
Correct |
1 ms |
648 KB |
Output is correct |
14 |
Correct |
1 ms |
648 KB |
Output is correct |
15 |
Correct |
1 ms |
648 KB |
Output is correct |
16 |
Correct |
1 ms |
648 KB |
Output is correct |
17 |
Correct |
1 ms |
648 KB |
Output is correct |
18 |
Correct |
1 ms |
648 KB |
Output is correct |
19 |
Correct |
1 ms |
648 KB |
Output is correct |
20 |
Correct |
1 ms |
648 KB |
Output is correct |
21 |
Correct |
1 ms |
648 KB |
Output is correct |
22 |
Correct |
1 ms |
648 KB |
Output is correct |
23 |
Correct |
1 ms |
648 KB |
Output is correct |
24 |
Correct |
1 ms |
648 KB |
Output is correct |
25 |
Correct |
1 ms |
648 KB |
Output is correct |
26 |
Correct |
2 ms |
648 KB |
Output is correct |
27 |
Correct |
1 ms |
648 KB |
Output is correct |
28 |
Correct |
1 ms |
648 KB |
Output is correct |
29 |
Correct |
1 ms |
648 KB |
Output is correct |
30 |
Correct |
304 ms |
19480 KB |
Output is correct |
31 |
Correct |
332 ms |
19480 KB |
Output is correct |
32 |
Correct |
166 ms |
19480 KB |
Output is correct |
33 |
Correct |
232 ms |
28600 KB |
Output is correct |
34 |
Correct |
209 ms |
28600 KB |
Output is correct |
35 |
Correct |
268 ms |
29872 KB |
Output is correct |
36 |
Correct |
249 ms |
29872 KB |
Output is correct |
37 |
Correct |
167 ms |
29872 KB |
Output is correct |
38 |
Correct |
167 ms |
29872 KB |
Output is correct |
39 |
Correct |
183 ms |
29872 KB |
Output is correct |
40 |
Correct |
184 ms |
29872 KB |
Output is correct |
41 |
Correct |
238 ms |
29872 KB |
Output is correct |
42 |
Correct |
219 ms |
29872 KB |
Output is correct |
43 |
Correct |
220 ms |
31852 KB |
Output is correct |
44 |
Incorrect |
2 ms |
31852 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |