Submission #378147

# Submission time Handle Problem Language Result Execution time Memory
378147 2021-03-16T05:15:54 Z rrrr10000 Ancient Books (IOI17_books) C++14
0 / 100
52 ms 48324 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pb emplace_back
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){for(auto x:v)outv(x);}
template<class T> void outp(T p){cout<<'('<<p.fi<<','<<p.se<<')'<<endl;}
template<class T> void outvp(T v){for(auto p:v)cout<<'('<<p.fi<<','<<p.se<<')';cout<<endl;}
const ll inf=1001001001001001001;
ll minimum_walk(vector<int> p,int s){
    int n=p.size();
    vi gr(n,-1);
    rep(i,n)if(p[i]==i)gr[i]=-2;
    ll ans=0,c=0;
    vp v;
    rep(i,n)if(gr[i]==-1){
        ll mi=i,ma=i;
        gr[i]=c;
        ll nw=p[i];
        ans+=abs(nw-i);
        while(nw!=i){
            gr[nw]=c;
            chmax(ma,nw);chmin(mi,nw);
            ans+=abs(nw-p[nw]);
            nw=p[nw];
        }
        c++;
        v.pb(mi,ma);
    }
    v.pb(s,s);
    if(gr[s]==-2)gr[s]=c;
    if(n<=1000){
        vvi dp(n,vi(n,inf));
        ll a=0,b=n-1;
        while(gr[a]==-2)a++;
        while(gr[b]==-2)b--;
        vvi ma(n,vi(n,-inf)),mi(n,vi(n,inf));
        rep(i,n)REP(j,i,n){
            if(gr[j]!=-2){
                ma[i][j]=v[gr[j]].se;
                mi[i][j]=v[gr[j]].fi;
            }
            if(j){
                chmax(ma[i][j],ma[i][j-1]);
                chmin(mi[i][j],mi[i][j-1]);
            }
        }
        dp[s][s]=0;
        rep(len,n)rep(i,n){
            ll j=i+len;if(j>=n)continue;
            chmin(dp[mi[i][j]][ma[i][j]],dp[i][j]);
            if(j!=n-1)chmin(dp[i][j+1],dp[i][j]+2);
            if(i)chmin(dp[i-1][j],dp[i][j]+2);
        }
        return dp[a][b]+ans;
    }
    sort(all(v));
    vp st;
    for(auto x:v){
        if(st.size()&&st.back().se>=x.fi)chmax(st.back().se,x.se);
        else{
            if(st.size())ans+=(x.fi-st.back().se)*2;
            st.pb(x);
        }
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Runtime error 1 ms 492 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Runtime error 1 ms 492 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Runtime error 1 ms 492 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 52 ms 48324 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Runtime error 1 ms 492 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -