Submission #58204

# Submission time Handle Problem Language Result Execution time Memory
58204 2018-07-17T06:47:25 Z 정원준(#1691) Candies (JOI18_candies) C++11
100 / 100
643 ms 21292 KB
#include <bits/stdc++.h>
#define L long long

using namespace std;

L n,ans;
L a[200010],nex[200010],pre[200010],cd[200010];

struct S{
	L val,loc,candy;
};

bool operator<(S a,S b){
	if(a.val==b.val) return a.loc<b.loc;
	return a.val>b.val;
}

bool operator==(S a,S b){
	return a.val==b.val&&a.loc==b.loc;
}

set<S>st;

int main()
{
	scanf("%lld",&n);
	L i,j;
	for(i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		nex[i]=i+1;
		pre[i]=i-1;
		cd[i]=1;
		S temp=(S){a[i],i,1};
		st.insert(temp);
	}
	nex[0]=1;
	pre[0]=0;
	nex[n+1]=n+1;
	pre[n+1]=n;
	
	/*for(set<S>::iterator ii=st.begin();ii!=st.end();ii++)
		{
			printf("%lld %lld\n",ii->loc,ii->val);
		}
		for(j=1;j<=n;j++)
		{
			printf("%lld ",nex[j]);
		}
		puts("");
		for(j=1;j<=n;j++)
		{
			printf("%lld ",pre[j]);
		}
		puts("");*/
	
	for(i=1;i<=(n+1)/2;i++)
	{
		set<S>::iterator now=st.begin();
		ans+=now->val;
		if(pre[now->loc]==0&&nex[now->loc]==n+1)
		{
			st.erase(now);
		}
		else if(pre[now->loc]==0)
		{	
			//puts("1");
			S nextemp=(S){a[nex[now->loc]],nex[now->loc],cd[nex[now->loc]]};
			S inptemp=(S){nextemp.val-a[now->loc],now->loc,nextemp.candy-now->candy};
			a[now->loc]=nextemp.val-a[now->loc];
			cd[now->loc]=nextemp.candy-now->candy;
			
			pre[nex[nex[now->loc]]]=pre[now->loc];
			nex[now->loc]=nex[nex[now->loc]];
			
			st.erase(now);
			st.erase(nextemp);
			//st.insert(inptemp);
		}
		else if(nex[now->loc]==n+1)
		{
			//puts("2");
			S pretemp=(S){a[pre[now->loc]],pre[now->loc],cd[pre[now->loc]]};
			S inptemp=(S){pretemp.val-a[now->loc],now->loc,pretemp.candy-now->candy};
			a[now->loc]=pretemp.val-a[now->loc];
			cd[now->loc]=pretemp.candy-now->candy;
			
			nex[pre[pre[now->loc]]]=nex[now->loc];
			pre[now->loc]=pre[pre[now->loc]];
			
			st.erase(now);
			st.erase(pretemp);
			//st.insert(inptemp);	
		}
		else
		{
			//puts("3");
			S nextemp=(S){a[nex[now->loc]],nex[now->loc],cd[nex[now->loc]]};
			S pretemp=(S){a[pre[now->loc]],pre[now->loc],cd[pre[now->loc]]};
			S inptemp=(S){nextemp.val+pretemp.val-a[now->loc],now->loc,nextemp.candy+pretemp.candy-now->candy};
			a[now->loc]=nextemp.val+pretemp.val-a[now->loc];
			cd[now->loc]=nextemp.candy+pretemp.candy-now->candy;
			
			/*printf("%lld %lld\n",nextemp.loc,nextemp.val);
			printf("%lld %lld\n",pretemp.loc,pretemp.val);
			printf("%lld %lld\n",inptemp.loc,inptemp.val);*/
			
			pre[nex[nex[now->loc]]]=now->loc;
			nex[now->loc]=nex[nex[now->loc]];
			
			nex[pre[pre[now->loc]]]=now->loc;
			pre[now->loc]=pre[pre[now->loc]];
			
			st.erase(now);
			st.erase(nextemp);
			st.erase(pretemp);
			st.insert(inptemp);
		}
		printf("%lld\n",ans);
		//printf("**%lld**\n",ans);
		/*for(set<S>::iterator ii=st.begin();ii!=st.end();ii++)
		{
			printf("%lld %lld\n",ii->loc,ii->val);
		}
		for(j=1;j<=n;j++)
		{
			printf("%lld ",nex[j]);
		}
		puts("");
		for(j=1;j<=n;j++)
		{
			printf("%lld ",pre[j]);
		}
		puts("");
		for(j=1;j<=n;j++)
		{
			printf("%lld ",cd[j]);
		}
		puts("");*/
	}
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:69:6: warning: variable 'inptemp' set but not used [-Wunused-but-set-variable]
    S inptemp=(S){nextemp.val-a[now->loc],now->loc,nextemp.candy-now->candy};
      ^~~~~~~
candies.cpp:84:6: warning: variable 'inptemp' set but not used [-Wunused-but-set-variable]
    S inptemp=(S){pretemp.val-a[now->loc],now->loc,pretemp.candy-now->candy};
      ^~~~~~~
candies.cpp:27:6: warning: unused variable 'j' [-Wunused-variable]
  L i,j;
      ^
candies.cpp:26:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld",&n);
  ~~~~~^~~~~~~~~~~
candies.cpp:30:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld",&a[i]);
   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 744 KB Output is correct
3 Correct 4 ms 816 KB Output is correct
4 Correct 4 ms 816 KB Output is correct
5 Correct 4 ms 816 KB Output is correct
6 Correct 5 ms 816 KB Output is correct
7 Correct 4 ms 816 KB Output is correct
8 Correct 5 ms 816 KB Output is correct
9 Correct 4 ms 816 KB Output is correct
10 Correct 4 ms 816 KB Output is correct
11 Correct 4 ms 816 KB Output is correct
12 Correct 5 ms 816 KB Output is correct
13 Correct 4 ms 816 KB Output is correct
14 Correct 3 ms 816 KB Output is correct
15 Correct 4 ms 816 KB Output is correct
16 Correct 4 ms 816 KB Output is correct
17 Correct 4 ms 880 KB Output is correct
18 Correct 5 ms 880 KB Output is correct
19 Correct 5 ms 880 KB Output is correct
20 Correct 4 ms 880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 744 KB Output is correct
3 Correct 4 ms 816 KB Output is correct
4 Correct 4 ms 816 KB Output is correct
5 Correct 4 ms 816 KB Output is correct
6 Correct 5 ms 816 KB Output is correct
7 Correct 4 ms 816 KB Output is correct
8 Correct 5 ms 816 KB Output is correct
9 Correct 4 ms 816 KB Output is correct
10 Correct 4 ms 816 KB Output is correct
11 Correct 4 ms 816 KB Output is correct
12 Correct 5 ms 816 KB Output is correct
13 Correct 4 ms 816 KB Output is correct
14 Correct 3 ms 816 KB Output is correct
15 Correct 4 ms 816 KB Output is correct
16 Correct 4 ms 816 KB Output is correct
17 Correct 4 ms 880 KB Output is correct
18 Correct 5 ms 880 KB Output is correct
19 Correct 5 ms 880 KB Output is correct
20 Correct 4 ms 880 KB Output is correct
21 Correct 610 ms 20912 KB Output is correct
22 Correct 643 ms 21228 KB Output is correct
23 Correct 617 ms 21284 KB Output is correct
24 Correct 187 ms 21284 KB Output is correct
25 Correct 219 ms 21284 KB Output is correct
26 Correct 232 ms 21284 KB Output is correct
27 Correct 212 ms 21284 KB Output is correct
28 Correct 238 ms 21284 KB Output is correct
29 Correct 200 ms 21284 KB Output is correct
30 Correct 240 ms 21284 KB Output is correct
31 Correct 212 ms 21284 KB Output is correct
32 Correct 219 ms 21284 KB Output is correct
33 Correct 348 ms 21284 KB Output is correct
34 Correct 388 ms 21284 KB Output is correct
35 Correct 331 ms 21284 KB Output is correct
36 Correct 518 ms 21284 KB Output is correct
37 Correct 501 ms 21292 KB Output is correct
38 Correct 552 ms 21292 KB Output is correct
39 Correct 270 ms 21292 KB Output is correct
40 Correct 310 ms 21292 KB Output is correct
41 Correct 261 ms 21292 KB Output is correct
42 Correct 200 ms 21292 KB Output is correct
43 Correct 263 ms 21292 KB Output is correct
44 Correct 193 ms 21292 KB Output is correct
45 Correct 296 ms 21292 KB Output is correct
46 Correct 248 ms 21292 KB Output is correct
47 Correct 253 ms 21292 KB Output is correct
48 Correct 360 ms 21292 KB Output is correct
49 Correct 315 ms 21292 KB Output is correct
50 Correct 432 ms 21292 KB Output is correct