cf 827 div 4 G

题意

给定一个数列,确定选数顺序,假设每次取出为x,要使得ans|x 最大,并且令ans|=x,求选数顺序。

题解

我们可以建一个大根堆,设堆顶为z,判断一下z&ans是否等于0,如果是那就是要选的数,否则就将z弹出,将z-=z&ans放入堆中。

code

#include<cstdio>
#include<algorithm>
#include<queue>
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
using namespace std;
const int N=2e5+5;
typedef long long ll;
int a[N],n,tot;
ll b[40],ans,xx,yy;
struct node{
	int x,y;
};
node k;
priority_queue<node> q;
bool operator < (const node &a, const node &b){
	return a.x<b.x;
}
int main(){
//	freopen("data.in","r",stdin);
	
	int T;
	scanf("%d",&T);
	b[0]=1;
	fo(i,1,30) b[i]=b[i-1]*2;
	
	while (T--){

		scanf("%d",&n);
		fo(i,1,n) scanf("%d",&a[i]);
		
		fo(i,1,n) q.push((node){a[i],a[i]});
		
		ans=0; 
		tot=0;
		
		while (tot<n) { 
			while (1) {
				k=q.top();
				q.pop();
				xx=k.x;
				yy=k.y;
				if ((ans&xx)) {
					xx-=ans&xx;
					q.push((node){xx,yy});
				}
				else{
					ans|=xx;
					printf("%d ",yy);
					tot++;
					break;
				}
			}
		}
		printf("\n");
	}
	return 0;
}
posted @ 2022-10-15 12:01  gan_coder  阅读(51)  评论(0)    收藏  举报