#include "books.h"
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const ll INF = 1LL << 60;
int N;
ll safecost;
vi vis, P;
void cycle(int i, int &mini, int &maxi, int cycleCnt)
{
if (vis[i] != -1)
return;
mini = min(mini, i);
maxi = max(maxi, i);
vis[i] = cycleCnt;
safecost += (ll)abs(P[i] - i);
cycle(P[i], mini, maxi, cycleCnt);
}
long long minimum_walk(std::vector<int> p, int s)
{
N = sz(p);
P.resize(N);
for (int i = 0; i < N; ++i)
P[i] = p[i];
vis.assign(N, -1);
ll L = INF, R = -1;
safecost = 0;
vpii borders;
for (int i = 0, mini, maxi; i < N; ++i)
{
if (P[i] != i)
L = min(L, (ll)i), R = max(R, (ll)i);
if (vis[i] == -1 && P[i] != i)
{
mini = maxi = i;
cycle(i, mini, maxi, sz(borders));
borders.push_back({mini, maxi});
}
}
if (borders.empty())
return 0;
vi maxL(N, s), maxR(N, s), cost(N, 0);
if (vis[s] != -1)
tie(maxL[s], maxR[s]) = borders[vis[s]];
for (int i = s + 1; i < N; ++i)
{
maxL[i] = maxL[i - 1], maxR[i] = maxR[i - 1], cost[i] = cost[i - 1];
if (maxR[i] < i)
cost[i] += 2, maxR[i] = i;
if (vis[i] != -1)
maxL[i] = min(maxL[i], borders[vis[i]].first),
maxR[i] = max(maxR[i], borders[vis[i]].second);
}
for (int i = s - 1; i >= 0; --i)
{
maxL[i] = maxL[i + 1], maxR[i] = maxR[i + 1], cost[i] = cost[i + 1];
if (maxL[i] > i)
cost[i] += 2, maxL[i] = i;
if (vis[i] != -1)
maxL[i] = min(maxL[i], borders[vis[i]].first),
maxR[i] = max(maxR[i], borders[vis[i]].second);
}
ll mincost = INF;
int l = s, r = N - 1;
while (l >= 0 && r >= 0)
{
if (max(maxR[l], maxR[r]) < R || min(maxL[l], maxL[r]) > L)
--l;
else
{
mincost = min(mincost, cost[l] + cost[r] + safecost);
--r;
}
}
return mincost;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
200 ms |
47276 KB |
Output is correct |
31 |
Correct |
177 ms |
47204 KB |
Output is correct |
32 |
Correct |
84 ms |
23656 KB |
Output is correct |
33 |
Correct |
134 ms |
50388 KB |
Output is correct |
34 |
Correct |
120 ms |
50344 KB |
Output is correct |
35 |
Correct |
130 ms |
50780 KB |
Output is correct |
36 |
Correct |
109 ms |
49848 KB |
Output is correct |
37 |
Correct |
106 ms |
47716 KB |
Output is correct |
38 |
Correct |
112 ms |
47380 KB |
Output is correct |
39 |
Correct |
111 ms |
47464 KB |
Output is correct |
40 |
Correct |
117 ms |
47224 KB |
Output is correct |
41 |
Correct |
134 ms |
47204 KB |
Output is correct |
42 |
Correct |
125 ms |
47180 KB |
Output is correct |
43 |
Correct |
115 ms |
51292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3308' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
200 ms |
47276 KB |
Output is correct |
31 |
Correct |
177 ms |
47204 KB |
Output is correct |
32 |
Correct |
84 ms |
23656 KB |
Output is correct |
33 |
Correct |
134 ms |
50388 KB |
Output is correct |
34 |
Correct |
120 ms |
50344 KB |
Output is correct |
35 |
Correct |
130 ms |
50780 KB |
Output is correct |
36 |
Correct |
109 ms |
49848 KB |
Output is correct |
37 |
Correct |
106 ms |
47716 KB |
Output is correct |
38 |
Correct |
112 ms |
47380 KB |
Output is correct |
39 |
Correct |
111 ms |
47464 KB |
Output is correct |
40 |
Correct |
117 ms |
47224 KB |
Output is correct |
41 |
Correct |
134 ms |
47204 KB |
Output is correct |
42 |
Correct |
125 ms |
47180 KB |
Output is correct |
43 |
Correct |
115 ms |
51292 KB |
Output is correct |
44 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3308' |
45 |
Halted |
0 ms |
0 KB |
- |