#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9;
ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<int,int>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
///Dremix10 's template
const int N = 2e3+1;
template<typename T>
struct SPARSE{
vector<vector<T> > sparse;
vector<int> log;
const int maxPow = 22;
int N;
void init(int n){
sparse.assign(n,vector<T>(maxPow));
log.assign(n+1,0);
N = n;
for(int i = 2;i <= n;i++)
log[i] = log[i/2] + 1;
}
// supports 0-indexing
void build(T arr[]){
int i,j;
for(i = 0;i < N ;i++)
sparse[i][0] = arr[i];
for(j = 1;j < maxPow;j++)
for(i = 0;i + (1<<j) <= N;i++)
sparse[i][j] = min(sparse[i][j-1],sparse[i+(1<<(j-1))][j-1]);
}
T query(int l, int r){
int k = log[r-l+1];
return min(sparse[l][k],sparse[r-(1<<k)+1][k]);
}
};
template<typename T>
struct SPARSE2{
vector<vector<T> > sparse;
vector<int> log;
const int maxPow = 22;
int N;
void init(int n){
sparse.assign(n,vector<T>(maxPow));
log.assign(n+1,0);
N = n;
for(int i = 2;i <= n;i++)
log[i] = log[i/2] + 1;
}
// supports 0-indexing
void build(T arr[]){
int i,j;
for(i = 0;i < N ;i++)
sparse[i][0] = arr[i];
for(j = 1;j < maxPow;j++)
for(i = 0;i + (1<<j) <= N;i++)
sparse[i][j] = max(sparse[i][j-1],sparse[i+(1<<(j-1))][j-1]);
}
T query(int l, int r){
int k = log[r-l+1];
return max(sparse[l][k],sparse[r-(1<<k)+1][k]);
}
};
SPARSE<ll>S;
SPARSE2<ll>s2;
#include "books.h"
ll dp[1005][1005];
ll n;
ll Left[1005];
ll Right[1005];
ll maxi = 0;
ll solve(ll i,ll j){
if(i < 0 || j > maxi){
return INF;
}
if(i == 0 && j == maxi){
return 0;
}
if(dp[i][j] != -1){
return dp[i][j];
}
ll L = min(i,S.query(i,j)),R = max(j,s2.query(i,j));
if(i == L && j == R){
ll ans = min(solve(i - 1,j) + 2,solve(i,j + 1) + 2);
return dp[i][j] = ans;
}
return dp[i][j] = solve(L,R);
}
long long minimum_walk(std::vector<int> p, int s) {
ll ans = 0;
n = p.size();
bool v[n] = {0};
ll dist = 0;
ll l[n],r[n];
ll L = -1,R;
vector<ii>rang;
if(p[s] == s){
L = R = s;
rang.pb(ii(s,s));
}
f(i,0,n){
Left[i] = n;
}
f(i,0,n){
if(p[i] != i && !v[i]){
ll last = i;
ll st = i;
ll pos = p[i];
v[i] = 1;
l[st] = i,r[st] = pos;
while(pos != st){
l[st] = min(l[st],pos);
r[st] = max(r[st],pos);
v[pos] = 1;
ans += abs(pos - last);
last = pos;
pos = p[pos];
}
ans += abs(pos - last);
l[st] = min(l[st],pos);
r[st] = max(r[st],pos);
maxi = max(maxi,r[st]);
rang.pb(ii(l[st],r[st]));
if(v[s] == 1 && L == -1){
L = l[st];
R = r[st];
}
}
}
if(rang.empty()){
return ans;
}
for(auto x:rang){
Left[x.S] = min(Left[x.S],x.F);
Right[x.F] = max(Right[x.F],x.S);
}
S.init(n);
s2.init(n);
S.build(Left);
s2.build(Right);
memset(dp,-1,sizeof dp);
dist = solve(L,R);
ans += dist;
return ans;
}
/*
int main() {
int n, s;
assert(2 == scanf("%d %d", &n, &s));
vector<int> p((unsigned) n);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &p[(unsigned) i]));
long long res = minimum_walk(p, s);
printf("%lld\n", res);
return 0;
}
*/
/*
4 0
0 2 3 1
*/
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:171:17: warning: 'R' may be used uninitialized in this function [-Wmaybe-uninitialized]
171 | dist = solve(L,R);
| ~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
3 ms |
8148 KB |
Output is correct |
4 |
Correct |
5 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8128 KB |
Output is correct |
8 |
Correct |
4 ms |
8180 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8148 KB |
Output is correct |
16 |
Correct |
5 ms |
8148 KB |
Output is correct |
17 |
Correct |
6 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
3 ms |
8148 KB |
Output is correct |
4 |
Correct |
5 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8128 KB |
Output is correct |
8 |
Correct |
4 ms |
8180 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8148 KB |
Output is correct |
16 |
Correct |
5 ms |
8148 KB |
Output is correct |
17 |
Correct |
6 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8660 KB |
Output is correct |
20 |
Correct |
4 ms |
8660 KB |
Output is correct |
21 |
Correct |
4 ms |
8660 KB |
Output is correct |
22 |
Correct |
4 ms |
8660 KB |
Output is correct |
23 |
Correct |
4 ms |
8660 KB |
Output is correct |
24 |
Correct |
4 ms |
8660 KB |
Output is correct |
25 |
Correct |
4 ms |
8660 KB |
Output is correct |
26 |
Correct |
5 ms |
8660 KB |
Output is correct |
27 |
Correct |
4 ms |
8660 KB |
Output is correct |
28 |
Correct |
5 ms |
8624 KB |
Output is correct |
29 |
Correct |
4 ms |
8660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
3 ms |
8148 KB |
Output is correct |
4 |
Correct |
5 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8128 KB |
Output is correct |
8 |
Correct |
4 ms |
8180 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8148 KB |
Output is correct |
16 |
Correct |
5 ms |
8148 KB |
Output is correct |
17 |
Correct |
6 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8660 KB |
Output is correct |
20 |
Correct |
4 ms |
8660 KB |
Output is correct |
21 |
Correct |
4 ms |
8660 KB |
Output is correct |
22 |
Correct |
4 ms |
8660 KB |
Output is correct |
23 |
Correct |
4 ms |
8660 KB |
Output is correct |
24 |
Correct |
4 ms |
8660 KB |
Output is correct |
25 |
Correct |
4 ms |
8660 KB |
Output is correct |
26 |
Correct |
5 ms |
8660 KB |
Output is correct |
27 |
Correct |
4 ms |
8660 KB |
Output is correct |
28 |
Correct |
5 ms |
8624 KB |
Output is correct |
29 |
Correct |
4 ms |
8660 KB |
Output is correct |
30 |
Incorrect |
820 ms |
464212 KB |
3rd lines differ - on the 1st token, expected: '333035179244', found: '334035179244' |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
8660 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3308' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
3 ms |
8148 KB |
Output is correct |
4 |
Correct |
5 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8128 KB |
Output is correct |
8 |
Correct |
4 ms |
8180 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8148 KB |
Output is correct |
16 |
Correct |
5 ms |
8148 KB |
Output is correct |
17 |
Correct |
6 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8660 KB |
Output is correct |
20 |
Correct |
4 ms |
8660 KB |
Output is correct |
21 |
Correct |
4 ms |
8660 KB |
Output is correct |
22 |
Correct |
4 ms |
8660 KB |
Output is correct |
23 |
Correct |
4 ms |
8660 KB |
Output is correct |
24 |
Correct |
4 ms |
8660 KB |
Output is correct |
25 |
Correct |
4 ms |
8660 KB |
Output is correct |
26 |
Correct |
5 ms |
8660 KB |
Output is correct |
27 |
Correct |
4 ms |
8660 KB |
Output is correct |
28 |
Correct |
5 ms |
8624 KB |
Output is correct |
29 |
Correct |
4 ms |
8660 KB |
Output is correct |
30 |
Incorrect |
820 ms |
464212 KB |
3rd lines differ - on the 1st token, expected: '333035179244', found: '334035179244' |
31 |
Halted |
0 ms |
0 KB |
- |