Submission #356094

# Submission time Handle Problem Language Result Execution time Memory
356094 2021-01-23T07:18:41 Z beksultan04 Ancient Books (IOI17_books) C++14
0 / 100
1 ms 364 KB
#include "books.h"
#ifndef EVAL
#include "grader.cpp"
#endif // EVAL
#include <bits/stdc++.h>
using namespace std;
#define lol long long
#define pii pair<int,int>
#define pll pair<lol,lol>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scanl(a) scanf("%lld",&a);
#define scanll(a,b) scanf("%lld %lld",&a, &b);
#define scanlll(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define scan1(a) scanf("%d",&a);
#define scan2(a,b) scanf("%d %d",&a, &b);
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define endi puts("");
#define eps 1e-12
int vis[1000001],n,tl,tr,t[1000001];
lol dp[1002][1002];
map<vector<int>,int> mp;
pii q[1000001];

void extend(int &l,int &r){
    int ll = min(q[t[l]].fr,q[t[r]].fr),rr = max(q[t[l]].sc,q[t[r]].sc);
    while (1){
        if (ll < l){
            ll = min(ll,q[t[--l]].fr);
            rr = max(rr,q[t[l]].sc);
        }
        else if (r < rr){
            ll = min(ll,q[t[++r]].fr);
            rr = max(rr,q[t[r]].sc);
        }
        else break;
    }
}


lol rec(int l,int r){
    if (l < tl || tr < r)ret 1e9+7;
    extend(l,r);
    if (l == tl && r == tr)ret 0;
    lol &res = dp[l][r];
    if (res != -1)ret dp[l][r];
    res = min(rec(l-1,r),rec(l,r+1))+2;
    ret res;
}



lol minimum_walk(vector<int> p, int s) {
    n = p.size();
    int i,col=0;
    tl = tr = s;
    lol ans=0;
    vector <pll> v;
    for (i=0;i<n;++i){
        ans += abs(p[i]-i);
        int x = i,l=i,r=i;
        while (vis[x] == 0){
            vis[x] = 1;
            l = min(l,x);
            r = max(x,r);
            t[x] = col;
            x = p[x];
        }
        if (l != r){
            tl = min(tl,l);
            tr = max(tr,r);
            q[col++] = {l,r};
        }
    }
    ret ans+rec(s,s);
}






/*
4 0
0 2 3 1
*/




# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 312 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 Incorrect 1 ms 364 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -