BZOJ3523: [Poi2014]Bricks

来源:互联网 发布:淘宝上开网店要钱吗 编辑:程序博客网 时间:2024/06/05 23:56
题目大意:给你每种颜色的砖块数量,相同颜色的砖块不能放在一起,两头颜色已经确定,构造一种方案

很容易想到是贪心,哪种颜色剩下的多就优先放那种颜色,多个颜色数量相同优先放结尾的那种颜色,实在放不了了就无解


#include<iostream>#include<cstdio>#include<queue>#define N 1000010using namespace std;struct ppp{int w,c;};int st,en;bool operator <(ppp x,ppp y){if(x.w!=y.w) return x.w<y.w;if(x.c==en) return false;return true;}priority_queue<ppp>q;int ans[N];int main(){int n;scanf("%d%d%d",&n,&st,&en);int i,j,x,y,m=0;ppp t,tmp;for(i=1;i<=n;i++){scanf("%d",&t.w);m+=t.w;t.c=i;if(i==st) t.w--;if(i==en) t.w--;if(t.w<0) {puts("0");return 0;}q.push(t);}ans[1]=st;ans[m]=en;bool f;for(i=2;i<m;i++){t=q.top();f=false;q.pop();if(t.c==st){tmp=t;if(!q.empty()) t=q.top();else {puts("0");return 0;}q.pop();f=true;}st=ans[i]=t.c;if(t.w>1)q.push((ppp){t.w-1,t.c});if(f) q.push(tmp);}if(ans[m-1]==ans[m]) {puts("0");return 0;}for(i=1;i<=m;i++)printf("%d ",ans[i]);}

0 0