cf 827div 4 G

题意

给定一个数列a,然后定义 b i = a 1 ⨁ a 2 ⨁ a 3 . . . ⨁ a i b_i=a_1 \bigoplus a_2 \bigoplus a_3... \bigoplus a_i bi=a1a2a3...ai
调整a的顺序,使得b的字典序最大

题解

我们可以建一个大根堆,设堆顶为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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值