Submission #20353

# Submission time Handle Problem Language Result Execution time Memory
20353 2017-02-07T07:51:28 Z Can polan into space? (OJUZ11_space) C++
0 / 100
364 ms 33072 KB
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;

int N;
const int MAX_N = 200010;

ll a[MAX_N][3],sum[MAX_N][3];

vector<int> pmax_id;

struct mem{
	ll c,v,id;
	mem(){}
	mem(ll c_,ll v_,ll id_):c(c_),v(v_),id(id_){}
	bool operator<(const mem& r)const{
		return c<r.c;
	}
}tree[MAX_N*4],dp[MAX_N];
ll add[MAX_N*4];

#define LL (2*k+1)
#define RR (2*k+2)

void make(int s,int e,int k){
	if(s==e){
		tree[k]=mem(-1e9,s,0);
		return;
	}
	int mid=(s+e)/2;
	make(s,mid,LL);
	make(mid+1,e,RR);
}

void up(int k){
	tree[k]=max(tree[LL],tree[RR]);
}

void down(int k){
	tree[LL].c+=add[k];
	tree[RR].c+=add[k];
	add[LL]+=add[k];
	add[RR]+=add[k];
	add[k]=0;
}

void addRange(int s,int e,int L,int R,int c,int id,int k){
	if(s>R||e<L)return;
	if(L<=s&&e<=R){
		add[k]+=c;
		tree[k].c+=c;
		tree[k].id=id;
		return;
	}
	down(k);

	int mid=(s+e)/2;
	addRange(s,mid,L,R,c,id,LL);
	addRange(mid+1,e,L,R,c,id,RR);
	up(k);
}

void update(int s,int e,int x,int c,int v,int id,int k){
	if(s==e){
		tree[k].c=c;
		tree[k].v=v;
		tree[k].id=id;
		return;
	}
	down(k);

	int mid=(s+e)/2;
	if(x<=mid)update(s,mid,x,c,v,id,LL);
	else update(mid+1,e,x,c,v,id,RR);
	up(k);
}

mem query(int s,int e,int L,int R,int k){
	if(s>R||e<L)return mem(-1e9,0,0);
	if(L<=s&&e<=R){
		return tree[k];
	}
	int mid=(s+e)/2;
	return max(query(s,mid,L,R,LL),query(mid+1,e,L,R,RR));
}

void f(int x,vector<int>& s){
	if(x==0)return;
	f(dp[x].v,s);
	s.push_back(x);
}

int main(){
	
	scanf("%d",&N);
	for(int i=1;i<=N;i++){
		for(int j=0;j<3;j++){
			scanf("%lld",&a[i][j]);
			sum[i][j]+=sum[i-1][j]+a[i][j];
		}
	}

	make(1,N,0);

	for(int i=1;i<=N;i++){
		dp[i]=mem(sum[i-1][1]+a[i][0],0,0);
		int sz=pmax_id.size();
		while(sz>1&&a[pmax_id[sz-1]][2]-a[pmax_id[sz-1]][1]<a[i-1][2]-a[i-1][1]){
			ll pre_max=a[pmax_id[sz-1]][2]-a[pmax_id[sz-1]][1];
			ll cur_max=a[i-1][2]-a[i-1][1];
			addRange(1,N,pmax_id[sz-2],pmax_id[sz-1]-1,cur_max-pre_max,i-1,0);
			pmax_id.pop_back();sz--;
		}
		pmax_id.push_back(i-1);
/*
		for(int j=1;j<=i-2;j++){
			mem t = query(1,N,j,j,0);
			printf("(%lld,%lld,%lld) ",t.c,t.v,t.id);
		}printf("   :   ");
*/
		mem t = query(1,N,1,i-2,0);
		ll dp_j=t.c;

//		printf("%lld  -->  ",dp_j+a[i][0]-a[i][1]+sum[i][1]);


		if(dp[i].c<dp_j+a[i][0]-a[i][1]+sum[i][1]){
			dp[i]=t;
			dp[i].c=dp_j+a[i][0]-a[i][1]+sum[i][1];
		}

//		printf("(%lld,%lld,%lld)\n",dp[i].c,dp[i].v,dp[i].id);

		update(1,N,i,dp[i].c-sum[i][1]+a[i+1][2]-a[i+1][1],i,i+1,0);

		/*
		ll pmax=a[i-1][2]-a[i-1][1],v=i-1;
		for(int j=i-2;j>0;j--){
			if(pmax<a[j+1][2]-a[j+1][1]){
				pmax=a[j+1][2]-a[j+1][1];
				v=j+1;
			}
			if(dp[i]<dp[j]+pmax+sum[i-1][1]-sum[j][1]+a[i][0]){
				dp[i]=dp[j]+pmax+sum[i-1][1]-sum[j][1]+a[i][0];
				pick[i]=v;
				path[i]=j;
			}
		}
		*/
	}

	ll ans=0,v;
	for(int i=1;i<=N;i++){
		if(ans<dp[i].c+sum[N][1]-sum[i][1]){
			ans=dp[i].c+sum[N][1]-sum[i][1];
			v=i;
		}
	}

	printf("%lld\n",ans);

	vector<int> st;
	f(v,st);

	assert(!st.empty());

	for(int i=0;i<st.size();i++){
		printf("%d ",st[i]);
	}

	for(int i=st.front()-1;i>=1;i--)printf("%d ",i);
	for(int i=st.back()+1;i<=N;i++)printf("%d ",i);
	
	for(int i=1;i<st.size();i++){
		for(int j=st[i-1]+1;j<dp[st[i]].id;j++)printf("%d ",j);
		for(int j=st[i]-1;j>=dp[st[i]].id;j--)printf("%d ",j);
	}
	puts("");

	return 0;
}

Compilation message

space.cpp: In function 'int main()':
space.cpp:172:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<st.size();i++){
               ^
space.cpp:179:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1;i<st.size();i++){
               ^
space.cpp:100:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&N);
                ^
space.cpp:103:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%lld",&a[i][j]);
                          ^
space.cpp:168:3: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
  f(v,st);
   ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 1 ms 380 KB Output is correct
3 Correct 1 ms 416 KB Output is correct
4 Correct 1 ms 420 KB Output is correct
5 Correct 1 ms 420 KB Output is correct
6 Correct 1 ms 420 KB Output is correct
7 Correct 1 ms 420 KB Output is correct
8 Correct 1 ms 424 KB Output is correct
9 Correct 1 ms 424 KB Output is correct
10 Correct 1 ms 424 KB Output is correct
11 Correct 1 ms 424 KB Output is correct
12 Correct 2 ms 424 KB Output is correct
13 Correct 1 ms 424 KB Output is correct
14 Correct 1 ms 424 KB Output is correct
15 Correct 1 ms 424 KB Output is correct
16 Correct 1 ms 424 KB Output is correct
17 Correct 1 ms 424 KB Output is correct
18 Incorrect 1 ms 424 KB Output isn't correct
19 Incorrect 1 ms 424 KB Output isn't correct
20 Incorrect 1 ms 424 KB Output isn't correct
21 Incorrect 2 ms 424 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 1 ms 380 KB Output is correct
3 Correct 1 ms 416 KB Output is correct
4 Correct 1 ms 420 KB Output is correct
5 Correct 1 ms 420 KB Output is correct
6 Correct 1 ms 420 KB Output is correct
7 Correct 1 ms 420 KB Output is correct
8 Correct 1 ms 424 KB Output is correct
9 Correct 1 ms 424 KB Output is correct
10 Correct 1 ms 424 KB Output is correct
11 Correct 1 ms 424 KB Output is correct
12 Correct 2 ms 424 KB Output is correct
13 Correct 1 ms 424 KB Output is correct
14 Correct 1 ms 424 KB Output is correct
15 Correct 1 ms 424 KB Output is correct
16 Correct 1 ms 424 KB Output is correct
17 Correct 1 ms 424 KB Output is correct
18 Incorrect 1 ms 424 KB Output isn't correct
19 Incorrect 1 ms 424 KB Output isn't correct
20 Incorrect 1 ms 424 KB Output isn't correct
21 Incorrect 2 ms 424 KB Output isn't correct
22 Correct 1 ms 424 KB Output is correct
23 Incorrect 1 ms 424 KB Output isn't correct
24 Correct 1 ms 424 KB Output is correct
25 Incorrect 1 ms 424 KB Output isn't correct
26 Incorrect 1 ms 424 KB Output isn't correct
27 Correct 1 ms 424 KB Output is correct
28 Correct 1 ms 424 KB Output is correct
29 Incorrect 1 ms 424 KB Output isn't correct
30 Incorrect 2 ms 424 KB Output isn't correct
31 Correct 1 ms 424 KB Output is correct
32 Incorrect 1 ms 424 KB Output isn't correct
33 Incorrect 1 ms 424 KB Output isn't correct
34 Incorrect 1 ms 424 KB Output isn't correct
35 Incorrect 1 ms 424 KB Output isn't correct
36 Incorrect 2 ms 424 KB Output isn't correct
37 Incorrect 1 ms 424 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 1 ms 380 KB Output is correct
3 Correct 1 ms 416 KB Output is correct
4 Correct 1 ms 420 KB Output is correct
5 Correct 1 ms 420 KB Output is correct
6 Correct 1 ms 420 KB Output is correct
7 Correct 1 ms 420 KB Output is correct
8 Correct 1 ms 424 KB Output is correct
9 Correct 1 ms 424 KB Output is correct
10 Correct 1 ms 424 KB Output is correct
11 Correct 1 ms 424 KB Output is correct
12 Correct 2 ms 424 KB Output is correct
13 Correct 1 ms 424 KB Output is correct
14 Correct 1 ms 424 KB Output is correct
15 Correct 1 ms 424 KB Output is correct
16 Correct 1 ms 424 KB Output is correct
17 Correct 1 ms 424 KB Output is correct
18 Incorrect 1 ms 424 KB Output isn't correct
19 Incorrect 1 ms 424 KB Output isn't correct
20 Incorrect 1 ms 424 KB Output isn't correct
21 Incorrect 2 ms 424 KB Output isn't correct
22 Correct 1 ms 424 KB Output is correct
23 Incorrect 1 ms 424 KB Output isn't correct
24 Correct 1 ms 424 KB Output is correct
25 Incorrect 1 ms 424 KB Output isn't correct
26 Incorrect 1 ms 424 KB Output isn't correct
27 Correct 1 ms 424 KB Output is correct
28 Correct 1 ms 424 KB Output is correct
29 Incorrect 1 ms 424 KB Output isn't correct
30 Incorrect 2 ms 424 KB Output isn't correct
31 Correct 1 ms 424 KB Output is correct
32 Incorrect 1 ms 424 KB Output isn't correct
33 Incorrect 1 ms 424 KB Output isn't correct
34 Incorrect 1 ms 424 KB Output isn't correct
35 Incorrect 1 ms 424 KB Output isn't correct
36 Incorrect 2 ms 424 KB Output isn't correct
37 Incorrect 1 ms 424 KB Output isn't correct
38 Incorrect 1 ms 424 KB Output isn't correct
39 Incorrect 2 ms 424 KB Output isn't correct
40 Correct 1 ms 424 KB Output is correct
41 Incorrect 2 ms 424 KB Output isn't correct
42 Incorrect 2 ms 424 KB Output isn't correct
43 Incorrect 2 ms 424 KB Output isn't correct
44 Correct 1 ms 424 KB Output is correct
45 Incorrect 2 ms 424 KB Output isn't correct
46 Incorrect 1 ms 424 KB Output isn't correct
47 Correct 2 ms 424 KB Output is correct
48 Incorrect 2 ms 432 KB Output isn't correct
49 Incorrect 2 ms 432 KB Output isn't correct
50 Incorrect 2 ms 432 KB Output isn't correct
51 Incorrect 2 ms 432 KB Output isn't correct
52 Incorrect 2 ms 432 KB Output isn't correct
53 Incorrect 2 ms 432 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 1 ms 380 KB Output is correct
3 Correct 1 ms 416 KB Output is correct
4 Correct 1 ms 420 KB Output is correct
5 Correct 1 ms 420 KB Output is correct
6 Correct 1 ms 420 KB Output is correct
7 Correct 1 ms 420 KB Output is correct
8 Correct 1 ms 424 KB Output is correct
9 Correct 1 ms 424 KB Output is correct
10 Correct 1 ms 424 KB Output is correct
11 Correct 1 ms 424 KB Output is correct
12 Correct 2 ms 424 KB Output is correct
13 Correct 1 ms 424 KB Output is correct
14 Correct 1 ms 424 KB Output is correct
15 Correct 1 ms 424 KB Output is correct
16 Correct 1 ms 424 KB Output is correct
17 Correct 1 ms 424 KB Output is correct
18 Incorrect 1 ms 424 KB Output isn't correct
19 Incorrect 1 ms 424 KB Output isn't correct
20 Incorrect 1 ms 424 KB Output isn't correct
21 Incorrect 2 ms 424 KB Output isn't correct
22 Correct 1 ms 424 KB Output is correct
23 Incorrect 1 ms 424 KB Output isn't correct
24 Correct 1 ms 424 KB Output is correct
25 Incorrect 1 ms 424 KB Output isn't correct
26 Incorrect 1 ms 424 KB Output isn't correct
27 Correct 1 ms 424 KB Output is correct
28 Correct 1 ms 424 KB Output is correct
29 Incorrect 1 ms 424 KB Output isn't correct
30 Incorrect 2 ms 424 KB Output isn't correct
31 Correct 1 ms 424 KB Output is correct
32 Incorrect 1 ms 424 KB Output isn't correct
33 Incorrect 1 ms 424 KB Output isn't correct
34 Incorrect 1 ms 424 KB Output isn't correct
35 Incorrect 1 ms 424 KB Output isn't correct
36 Incorrect 2 ms 424 KB Output isn't correct
37 Incorrect 1 ms 424 KB Output isn't correct
38 Incorrect 1 ms 424 KB Output isn't correct
39 Incorrect 2 ms 424 KB Output isn't correct
40 Correct 1 ms 424 KB Output is correct
41 Incorrect 2 ms 424 KB Output isn't correct
42 Incorrect 2 ms 424 KB Output isn't correct
43 Incorrect 2 ms 424 KB Output isn't correct
44 Correct 1 ms 424 KB Output is correct
45 Incorrect 2 ms 424 KB Output isn't correct
46 Incorrect 1 ms 424 KB Output isn't correct
47 Correct 2 ms 424 KB Output is correct
48 Incorrect 2 ms 432 KB Output isn't correct
49 Incorrect 2 ms 432 KB Output isn't correct
50 Incorrect 2 ms 432 KB Output isn't correct
51 Incorrect 2 ms 432 KB Output isn't correct
52 Incorrect 2 ms 432 KB Output isn't correct
53 Incorrect 2 ms 432 KB Output isn't correct
54 Incorrect 2 ms 432 KB Output isn't correct
55 Incorrect 16 ms 2200 KB Output isn't correct
56 Correct 16 ms 2216 KB Output is correct
57 Incorrect 16 ms 2216 KB Output isn't correct
58 Incorrect 17 ms 2224 KB Output isn't correct
59 Incorrect 18 ms 2224 KB Output isn't correct
60 Incorrect 22 ms 2232 KB Output isn't correct
61 Correct 21 ms 2232 KB Output is correct
62 Incorrect 12 ms 2232 KB Output isn't correct
63 Incorrect 4 ms 2232 KB Output isn't correct
64 Incorrect 16 ms 2232 KB Output isn't correct
65 Correct 14 ms 2232 KB Output is correct
66 Incorrect 18 ms 2232 KB Output isn't correct
67 Incorrect 8 ms 2232 KB Output isn't correct
68 Incorrect 6 ms 2232 KB Output isn't correct
69 Incorrect 16 ms 2232 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 1 ms 380 KB Output is correct
3 Correct 1 ms 416 KB Output is correct
4 Correct 1 ms 420 KB Output is correct
5 Correct 1 ms 420 KB Output is correct
6 Correct 1 ms 420 KB Output is correct
7 Correct 1 ms 420 KB Output is correct
8 Correct 1 ms 424 KB Output is correct
9 Correct 1 ms 424 KB Output is correct
10 Correct 1 ms 424 KB Output is correct
11 Correct 1 ms 424 KB Output is correct
12 Correct 2 ms 424 KB Output is correct
13 Correct 1 ms 424 KB Output is correct
14 Correct 1 ms 424 KB Output is correct
15 Correct 1 ms 424 KB Output is correct
16 Correct 1 ms 424 KB Output is correct
17 Correct 1 ms 424 KB Output is correct
18 Incorrect 1 ms 424 KB Output isn't correct
19 Incorrect 1 ms 424 KB Output isn't correct
20 Incorrect 1 ms 424 KB Output isn't correct
21 Incorrect 2 ms 424 KB Output isn't correct
22 Correct 1 ms 424 KB Output is correct
23 Incorrect 1 ms 424 KB Output isn't correct
24 Correct 1 ms 424 KB Output is correct
25 Incorrect 1 ms 424 KB Output isn't correct
26 Incorrect 1 ms 424 KB Output isn't correct
27 Correct 1 ms 424 KB Output is correct
28 Correct 1 ms 424 KB Output is correct
29 Incorrect 1 ms 424 KB Output isn't correct
30 Incorrect 2 ms 424 KB Output isn't correct
31 Correct 1 ms 424 KB Output is correct
32 Incorrect 1 ms 424 KB Output isn't correct
33 Incorrect 1 ms 424 KB Output isn't correct
34 Incorrect 1 ms 424 KB Output isn't correct
35 Incorrect 1 ms 424 KB Output isn't correct
36 Incorrect 2 ms 424 KB Output isn't correct
37 Incorrect 1 ms 424 KB Output isn't correct
38 Incorrect 1 ms 424 KB Output isn't correct
39 Incorrect 2 ms 424 KB Output isn't correct
40 Correct 1 ms 424 KB Output is correct
41 Incorrect 2 ms 424 KB Output isn't correct
42 Incorrect 2 ms 424 KB Output isn't correct
43 Incorrect 2 ms 424 KB Output isn't correct
44 Correct 1 ms 424 KB Output is correct
45 Incorrect 2 ms 424 KB Output isn't correct
46 Incorrect 1 ms 424 KB Output isn't correct
47 Correct 2 ms 424 KB Output is correct
48 Incorrect 2 ms 432 KB Output isn't correct
49 Incorrect 2 ms 432 KB Output isn't correct
50 Incorrect 2 ms 432 KB Output isn't correct
51 Incorrect 2 ms 432 KB Output isn't correct
52 Incorrect 2 ms 432 KB Output isn't correct
53 Incorrect 2 ms 432 KB Output isn't correct
54 Incorrect 2 ms 432 KB Output isn't correct
55 Incorrect 16 ms 2200 KB Output isn't correct
56 Correct 16 ms 2216 KB Output is correct
57 Incorrect 16 ms 2216 KB Output isn't correct
58 Incorrect 17 ms 2224 KB Output isn't correct
59 Incorrect 18 ms 2224 KB Output isn't correct
60 Incorrect 22 ms 2232 KB Output isn't correct
61 Correct 21 ms 2232 KB Output is correct
62 Incorrect 12 ms 2232 KB Output isn't correct
63 Incorrect 4 ms 2232 KB Output isn't correct
64 Incorrect 16 ms 2232 KB Output isn't correct
65 Correct 14 ms 2232 KB Output is correct
66 Incorrect 18 ms 2232 KB Output isn't correct
67 Incorrect 8 ms 2232 KB Output isn't correct
68 Incorrect 6 ms 2232 KB Output isn't correct
69 Incorrect 16 ms 2232 KB Output isn't correct
70 Incorrect 16 ms 2236 KB Output isn't correct
71 Incorrect 326 ms 32188 KB Output isn't correct
72 Correct 227 ms 33072 KB Output is correct
73 Incorrect 364 ms 33072 KB Output isn't correct
74 Correct 314 ms 33072 KB Output is correct
75 Incorrect 325 ms 33072 KB Output isn't correct
76 Incorrect 337 ms 33072 KB Output isn't correct
77 Incorrect 144 ms 33072 KB Output isn't correct
78 Incorrect 339 ms 33072 KB Output isn't correct
79 Incorrect 349 ms 33072 KB Output isn't correct
80 Correct 338 ms 33072 KB Output is correct
81 Incorrect 62 ms 33072 KB Output isn't correct
82 Incorrect 219 ms 33072 KB Output isn't correct
83 Incorrect 347 ms 33072 KB Output isn't correct
84 Incorrect 347 ms 33072 KB Output isn't correct
85 Incorrect 326 ms 33072 KB Output isn't correct
86 Incorrect 330 ms 33072 KB Output isn't correct
87 Incorrect 327 ms 33072 KB Output isn't correct
88 Incorrect 49 ms 33072 KB Output isn't correct
89 Incorrect 203 ms 33072 KB Output isn't correct
90 Incorrect 155 ms 33072 KB Output isn't correct
91 Correct 15 ms 33072 KB Output is correct
92 Incorrect 150 ms 33072 KB Output isn't correct