Submission #616182

# Submission time Handle Problem Language Result Execution time Memory
616182 2022-08-01T01:51:54 Z rrrr10000 Ancient Books (IOI17_books) C++14
100 / 100
224 ms 42836 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<vp> vvp;
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 fi first
#define se second
#define lb(v,k) (lower_bound(all(v),k)-v.bein())
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#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<<endl;}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<endl;}
const ll inf=1001001001001001001;

long long minimum_walk3(std::vector<int> p, int s) {
	ll l=0,r=p.size()-1;
	while(l<s&&p[l]==l)l++;
	while(r>s&&p[r]==r)r--;
	vi v;
	REP(i,l,r+1)v.pb(p[i]-l);
	s-=l;
	ll n=v.size(),ans=0;
	rep(i,n-1){
		rep(j,i+1)if(v[j]>i)ans++;
	}
	vvi dp(n,vi(n,inf));
	dp[s][s]=0;
	for(ll l=s;l>=0;l--){
		ll mi=inf,ma=-inf;
		REP(r,l,n){
			chmax(ma,v[r]);
			chmin(mi,v[r]);
			if(l)chmin(dp[l-1][r],dp[l][r]+1);
			if(r!=n-1)chmin(dp[l][r+1],dp[l][r]+1);
			chmin(dp[min(l,mi)][max(r,ma)],dp[l][r]);
		}
	}
	return (ans+dp[0][n-1])*2;
}
long long minimum_walk(std::vector<int> p, int s) {
	ll l=0,r=p.size()-1;
	while(l<s&&p[l]==l)l++;
	while(r>s&&p[r]==r)r--;
	vi v;
	REP(i,l,r+1)v.pb(p[i]-l);
	s-=l;
	ll n=v.size(),ans=0;
	rep(i,n)ans+=max(0ll,v[i]-i);
	deque<ll> deq;
	vi dis(n,inf);dis[s]=0;
	deq.pb(s);
	while(deq.size()){
		ll i=deq.front();deq.pop_front();
		if(i<s&&chmin(dis[i+1],dis[i]))deq.push_front(i+1);
		if(i>s&&chmin(dis[i-1],dis[i]))deq.push_front(i-1);
		if(chmin(dis[v[i]],dis[i]))deq.push_front(v[i]);
		if(i<n-1&&chmin(dis[i+1],dis[i]+1))deq.pb(i+1);
		if(i>0&&chmin(dis[i-1],dis[i]+1))deq.pb(i-1);
	}
	vi al(1,-1),ar(1,n);
	{
		ll ma=0;
		rep(i,s){
			chmax(ma,v[i]);
			if(ma==i)al.pb(i);
		}
	}
	{
		ll mi=n-1;
		for(int i=n-1;i>s;i--){
			chmin(mi,v[i]);
			if(mi==i)ar.pb(i);
		}
	}
	ans+=al.size()+ar.size()-2;
	ans+=dis[al.back()+1];
	return ans*2;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 223 ms 28484 KB Output is correct
31 Correct 224 ms 35136 KB Output is correct
32 Correct 84 ms 14828 KB Output is correct
33 Correct 132 ms 42836 KB Output is correct
34 Correct 146 ms 42824 KB Output is correct
35 Correct 151 ms 38100 KB Output is correct
36 Correct 125 ms 33620 KB Output is correct
37 Correct 135 ms 30656 KB Output is correct
38 Correct 122 ms 30660 KB Output is correct
39 Correct 133 ms 30772 KB Output is correct
40 Correct 128 ms 30668 KB Output is correct
41 Correct 186 ms 32948 KB Output is correct
42 Correct 150 ms 32368 KB Output is correct
43 Correct 117 ms 32540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 223 ms 28484 KB Output is correct
31 Correct 224 ms 35136 KB Output is correct
32 Correct 84 ms 14828 KB Output is correct
33 Correct 132 ms 42836 KB Output is correct
34 Correct 146 ms 42824 KB Output is correct
35 Correct 151 ms 38100 KB Output is correct
36 Correct 125 ms 33620 KB Output is correct
37 Correct 135 ms 30656 KB Output is correct
38 Correct 122 ms 30660 KB Output is correct
39 Correct 133 ms 30772 KB Output is correct
40 Correct 128 ms 30668 KB Output is correct
41 Correct 186 ms 32948 KB Output is correct
42 Correct 150 ms 32368 KB Output is correct
43 Correct 117 ms 32540 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 1 ms 212 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 122 ms 31056 KB Output is correct
65 Correct 114 ms 30800 KB Output is correct
66 Correct 150 ms 31272 KB Output is correct
67 Correct 139 ms 32592 KB Output is correct
68 Correct 16 ms 3364 KB Output is correct
69 Correct 13 ms 3380 KB Output is correct
70 Correct 11 ms 3360 KB Output is correct
71 Correct 12 ms 3272 KB Output is correct
72 Correct 11 ms 3364 KB Output is correct
73 Correct 13 ms 3496 KB Output is correct
74 Correct 140 ms 37576 KB Output is correct
75 Correct 124 ms 39612 KB Output is correct
76 Correct 122 ms 32624 KB Output is correct
77 Correct 119 ms 32524 KB Output is correct
78 Correct 118 ms 31132 KB Output is correct
79 Correct 129 ms 31044 KB Output is correct
80 Correct 82 ms 14796 KB Output is correct
81 Correct 111 ms 30668 KB Output is correct
82 Correct 136 ms 30668 KB Output is correct
83 Correct 133 ms 30672 KB Output is correct
84 Correct 118 ms 30548 KB Output is correct
85 Correct 144 ms 30656 KB Output is correct
86 Correct 124 ms 30644 KB Output is correct
87 Correct 127 ms 30560 KB Output is correct
88 Correct 110 ms 30656 KB Output is correct
89 Correct 123 ms 30544 KB Output is correct
90 Correct 126 ms 30664 KB Output is correct
91 Correct 135 ms 30712 KB Output is correct
92 Correct 128 ms 30644 KB Output is correct