#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;
}
int L, R;
int mn[1 << 10][1 << 10];
int mx[1 << 10][1 << 10];
int dp[1 << 10][1 << 10];
int Min(int l, int r)
{
if(l == r) return p[l];
int &memo = mn[l][r];
if(memo != -1) return memo;
return memo = min(Min(l + 1, r), Min(l, r - 1));
}
int Max(int l, int r)
{
if(l == r) return p[l];
int &memo = mx[l][r];
if(memo != -1) return memo;
return memo = max(Max(l + 1, r), Max(l, r - 1));
}
int rec(int l, int r)
{
if(l == L && r == R)
return 0;
int &memo = dp[l][r];
if(memo != -1) return memo;
int nl = Min(l, r);
if(nl < l) return memo = rec(nl, r);
int nr = Max(l, r);
if(r < nr) return memo = rec(l, nr);
memo = (int)1e9;
if(L < l) chkmin(memo, rec(l - 1, r) + 2);
if(r < R) chkmin(memo, rec(l, r + 1) + 2);
return memo;
}
long long minimum_walk(std::vector<int> p, int s)
{
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));
li.push_back({s, s});
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 = li[0].first;
for(auto it: li)
{
answer += 2 * (it.first - last);
last = it.second;
}
if(s != 0)
{
int l, r;
for(auto it: li)
if(it.first <= s && s <= it.second)
l = it.first, r = it.second;
memset(mn, -1, sizeof(mn));
memset(mx, -1, sizeof(mx));
::L = l;
::R = r;
memset(dp, -1, sizeof(dp));
answer += rec(s, s);
}
return answer;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:115:7: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
::R = r;
~~~~^~~
books.cpp:114:7: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
::L = l;
~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
416 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
672 KB |
Output is correct |
7 |
Correct |
2 ms |
672 KB |
Output is correct |
8 |
Correct |
2 ms |
672 KB |
Output is correct |
9 |
Correct |
2 ms |
672 KB |
Output is correct |
10 |
Correct |
2 ms |
672 KB |
Output is correct |
11 |
Correct |
2 ms |
672 KB |
Output is correct |
12 |
Correct |
2 ms |
672 KB |
Output is correct |
13 |
Correct |
2 ms |
672 KB |
Output is correct |
14 |
Correct |
2 ms |
672 KB |
Output is correct |
15 |
Correct |
2 ms |
672 KB |
Output is correct |
16 |
Correct |
2 ms |
672 KB |
Output is correct |
17 |
Correct |
2 ms |
672 KB |
Output is correct |
18 |
Correct |
2 ms |
672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
416 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
672 KB |
Output is correct |
7 |
Correct |
2 ms |
672 KB |
Output is correct |
8 |
Correct |
2 ms |
672 KB |
Output is correct |
9 |
Correct |
2 ms |
672 KB |
Output is correct |
10 |
Correct |
2 ms |
672 KB |
Output is correct |
11 |
Correct |
2 ms |
672 KB |
Output is correct |
12 |
Correct |
2 ms |
672 KB |
Output is correct |
13 |
Correct |
2 ms |
672 KB |
Output is correct |
14 |
Correct |
2 ms |
672 KB |
Output is correct |
15 |
Correct |
2 ms |
672 KB |
Output is correct |
16 |
Correct |
2 ms |
672 KB |
Output is correct |
17 |
Correct |
2 ms |
672 KB |
Output is correct |
18 |
Correct |
2 ms |
672 KB |
Output is correct |
19 |
Correct |
2 ms |
672 KB |
Output is correct |
20 |
Correct |
2 ms |
672 KB |
Output is correct |
21 |
Correct |
2 ms |
672 KB |
Output is correct |
22 |
Correct |
2 ms |
672 KB |
Output is correct |
23 |
Correct |
2 ms |
672 KB |
Output is correct |
24 |
Correct |
2 ms |
672 KB |
Output is correct |
25 |
Correct |
2 ms |
672 KB |
Output is correct |
26 |
Correct |
2 ms |
672 KB |
Output is correct |
27 |
Correct |
2 ms |
748 KB |
Output is correct |
28 |
Correct |
2 ms |
748 KB |
Output is correct |
29 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
416 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
672 KB |
Output is correct |
7 |
Correct |
2 ms |
672 KB |
Output is correct |
8 |
Correct |
2 ms |
672 KB |
Output is correct |
9 |
Correct |
2 ms |
672 KB |
Output is correct |
10 |
Correct |
2 ms |
672 KB |
Output is correct |
11 |
Correct |
2 ms |
672 KB |
Output is correct |
12 |
Correct |
2 ms |
672 KB |
Output is correct |
13 |
Correct |
2 ms |
672 KB |
Output is correct |
14 |
Correct |
2 ms |
672 KB |
Output is correct |
15 |
Correct |
2 ms |
672 KB |
Output is correct |
16 |
Correct |
2 ms |
672 KB |
Output is correct |
17 |
Correct |
2 ms |
672 KB |
Output is correct |
18 |
Correct |
2 ms |
672 KB |
Output is correct |
19 |
Correct |
2 ms |
672 KB |
Output is correct |
20 |
Correct |
2 ms |
672 KB |
Output is correct |
21 |
Correct |
2 ms |
672 KB |
Output is correct |
22 |
Correct |
2 ms |
672 KB |
Output is correct |
23 |
Correct |
2 ms |
672 KB |
Output is correct |
24 |
Correct |
2 ms |
672 KB |
Output is correct |
25 |
Correct |
2 ms |
672 KB |
Output is correct |
26 |
Correct |
2 ms |
672 KB |
Output is correct |
27 |
Correct |
2 ms |
748 KB |
Output is correct |
28 |
Correct |
2 ms |
748 KB |
Output is correct |
29 |
Correct |
2 ms |
748 KB |
Output is correct |
30 |
Correct |
235 ms |
25616 KB |
Output is correct |
31 |
Correct |
209 ms |
25616 KB |
Output is correct |
32 |
Correct |
132 ms |
25616 KB |
Output is correct |
33 |
Correct |
168 ms |
25616 KB |
Output is correct |
34 |
Correct |
167 ms |
25616 KB |
Output is correct |
35 |
Correct |
177 ms |
25616 KB |
Output is correct |
36 |
Correct |
212 ms |
25616 KB |
Output is correct |
37 |
Correct |
154 ms |
25616 KB |
Output is correct |
38 |
Correct |
158 ms |
25616 KB |
Output is correct |
39 |
Correct |
157 ms |
25616 KB |
Output is correct |
40 |
Correct |
246 ms |
25616 KB |
Output is correct |
41 |
Correct |
287 ms |
25616 KB |
Output is correct |
42 |
Correct |
184 ms |
25616 KB |
Output is correct |
43 |
Correct |
174 ms |
25616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
25616 KB |
Output is correct |
2 |
Correct |
26 ms |
25616 KB |
Output is correct |
3 |
Correct |
26 ms |
25616 KB |
Output is correct |
4 |
Correct |
11 ms |
25616 KB |
Output is correct |
5 |
Correct |
11 ms |
25616 KB |
Output is correct |
6 |
Correct |
24 ms |
25616 KB |
Output is correct |
7 |
Correct |
22 ms |
25616 KB |
Output is correct |
8 |
Correct |
20 ms |
25616 KB |
Output is correct |
9 |
Correct |
19 ms |
25616 KB |
Output is correct |
10 |
Correct |
11 ms |
25616 KB |
Output is correct |
11 |
Correct |
21 ms |
25616 KB |
Output is correct |
12 |
Correct |
23 ms |
25616 KB |
Output is correct |
13 |
Correct |
20 ms |
25616 KB |
Output is correct |
14 |
Correct |
22 ms |
25616 KB |
Output is correct |
15 |
Correct |
26 ms |
25616 KB |
Output is correct |
16 |
Correct |
28 ms |
25616 KB |
Output is correct |
17 |
Correct |
25 ms |
25616 KB |
Output is correct |
18 |
Correct |
21 ms |
25616 KB |
Output is correct |
19 |
Correct |
22 ms |
25616 KB |
Output is correct |
20 |
Correct |
24 ms |
25616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
416 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
672 KB |
Output is correct |
7 |
Correct |
2 ms |
672 KB |
Output is correct |
8 |
Correct |
2 ms |
672 KB |
Output is correct |
9 |
Correct |
2 ms |
672 KB |
Output is correct |
10 |
Correct |
2 ms |
672 KB |
Output is correct |
11 |
Correct |
2 ms |
672 KB |
Output is correct |
12 |
Correct |
2 ms |
672 KB |
Output is correct |
13 |
Correct |
2 ms |
672 KB |
Output is correct |
14 |
Correct |
2 ms |
672 KB |
Output is correct |
15 |
Correct |
2 ms |
672 KB |
Output is correct |
16 |
Correct |
2 ms |
672 KB |
Output is correct |
17 |
Correct |
2 ms |
672 KB |
Output is correct |
18 |
Correct |
2 ms |
672 KB |
Output is correct |
19 |
Correct |
2 ms |
672 KB |
Output is correct |
20 |
Correct |
2 ms |
672 KB |
Output is correct |
21 |
Correct |
2 ms |
672 KB |
Output is correct |
22 |
Correct |
2 ms |
672 KB |
Output is correct |
23 |
Correct |
2 ms |
672 KB |
Output is correct |
24 |
Correct |
2 ms |
672 KB |
Output is correct |
25 |
Correct |
2 ms |
672 KB |
Output is correct |
26 |
Correct |
2 ms |
672 KB |
Output is correct |
27 |
Correct |
2 ms |
748 KB |
Output is correct |
28 |
Correct |
2 ms |
748 KB |
Output is correct |
29 |
Correct |
2 ms |
748 KB |
Output is correct |
30 |
Correct |
235 ms |
25616 KB |
Output is correct |
31 |
Correct |
209 ms |
25616 KB |
Output is correct |
32 |
Correct |
132 ms |
25616 KB |
Output is correct |
33 |
Correct |
168 ms |
25616 KB |
Output is correct |
34 |
Correct |
167 ms |
25616 KB |
Output is correct |
35 |
Correct |
177 ms |
25616 KB |
Output is correct |
36 |
Correct |
212 ms |
25616 KB |
Output is correct |
37 |
Correct |
154 ms |
25616 KB |
Output is correct |
38 |
Correct |
158 ms |
25616 KB |
Output is correct |
39 |
Correct |
157 ms |
25616 KB |
Output is correct |
40 |
Correct |
246 ms |
25616 KB |
Output is correct |
41 |
Correct |
287 ms |
25616 KB |
Output is correct |
42 |
Correct |
184 ms |
25616 KB |
Output is correct |
43 |
Correct |
174 ms |
25616 KB |
Output is correct |
44 |
Correct |
26 ms |
25616 KB |
Output is correct |
45 |
Correct |
26 ms |
25616 KB |
Output is correct |
46 |
Correct |
26 ms |
25616 KB |
Output is correct |
47 |
Correct |
11 ms |
25616 KB |
Output is correct |
48 |
Correct |
11 ms |
25616 KB |
Output is correct |
49 |
Correct |
24 ms |
25616 KB |
Output is correct |
50 |
Correct |
22 ms |
25616 KB |
Output is correct |
51 |
Correct |
20 ms |
25616 KB |
Output is correct |
52 |
Correct |
19 ms |
25616 KB |
Output is correct |
53 |
Correct |
11 ms |
25616 KB |
Output is correct |
54 |
Correct |
21 ms |
25616 KB |
Output is correct |
55 |
Correct |
23 ms |
25616 KB |
Output is correct |
56 |
Correct |
20 ms |
25616 KB |
Output is correct |
57 |
Correct |
22 ms |
25616 KB |
Output is correct |
58 |
Correct |
26 ms |
25616 KB |
Output is correct |
59 |
Correct |
28 ms |
25616 KB |
Output is correct |
60 |
Correct |
25 ms |
25616 KB |
Output is correct |
61 |
Correct |
21 ms |
25616 KB |
Output is correct |
62 |
Correct |
22 ms |
25616 KB |
Output is correct |
63 |
Correct |
24 ms |
25616 KB |
Output is correct |
64 |
Runtime error |
195 ms |
59676 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
65 |
Halted |
0 ms |
0 KB |
- |