Submission #378142

# Submission time Handle Problem Language Result Execution time Memory
378142 2021-03-16T05:07:48 Z rrrr10000 Ancient Books (IOI17_books) C++14
12 / 100
696 ms 1048580 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--;
        dp[a][b]=0;
        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]);
            }
        }
        function<ll(ll,ll)> dfs=[&](ll l,ll r){
            if(dp[l][r]!=inf)return dp[l][r];
            if(mi[l][r]!=l){
                dp[l][r]=dfs(mi[l][r],r);
                return dp[l][r];
            }
            if(ma[l][r]!=r){
                dp[l][r]=dfs(l,ma[l][r]);
                return dp[l][r];
            }
            if(r!=n+1)chmin(dp[l][r],dfs(l,r+1)+2);
            if(l)chmin(dp[l][r],dfs(l-1,r)+2);
            return dp[l][r];
        };
        return dfs(s,s)+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 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 23 ms 23976 KB Output is correct
20 Correct 19 ms 23916 KB Output is correct
21 Correct 16 ms 23916 KB Output is correct
22 Runtime error 696 ms 1048576 KB Execution killed with signal 9
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 23 ms 23976 KB Output is correct
20 Correct 19 ms 23916 KB Output is correct
21 Correct 16 ms 23916 KB Output is correct
22 Runtime error 696 ms 1048576 KB Execution killed with signal 9
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 599 ms 1048580 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 23 ms 23976 KB Output is correct
20 Correct 19 ms 23916 KB Output is correct
21 Correct 16 ms 23916 KB Output is correct
22 Runtime error 696 ms 1048576 KB Execution killed with signal 9
23 Halted 0 ms 0 KB -