LuoguP7912 [CSP-J 2021] 小熊的果篮

时隔一年,再次做这道题,感觉好很多了233。当时暴力70分,如今新做法终AC。

做法来源于这个题解! 大佬 @StarBird 的做法

但是优化了码量,源自于对0和1的小技巧。代码如下

#include<bits/stdc++.h>
#define INF 200010
using namespace std;
int n,nw,p;
set<int>s[2];
signed main(){
	scanf("%d",&n);
	for(int i=1,a;i<=n;i++){
		scanf("%d",&a);
		s[a].insert(i);
	}s[0].insert(INF),s[1].insert(INF);
	p=*s[0].begin()>*s[1].begin();
	while(s[p].size()>1){
		nw=*s[p].upper_bound(nw);
		if(nw==INF){
			nw=0;
			p=*s[0].begin()>*s[1].begin();
			puts("");
			continue;
		}printf("%d ",nw);
		s[p].erase(nw);p=!p;
	}puts("");
	p=s[0].size()<s[1].size();
	while(s[p].size()>1)
		printf("%d\n",*s[p].begin()),s[p].erase(*s[p].begin());
	return 0;
}

具体的做法,就去看那篇博客吧

posted @ 2022-11-12 10:45  robinyqc  阅读(211)  评论(0)    收藏  举报