Codeforces Round #618 (Div. 2) C.Anu Has a Function

鏈接:https://codeforces.com/contest/1300/problem/C
思路
f(x,y)=(x|y)−y 其實就是 y的二進制位上所有爲1的位置都清零,即所有運算結束後這個位置一定是零,所以影響最後答案的就只有那些,該位上只有一個1的位置,而要使答案最大,那麼就應該把只有一個1的最高位對應的數放在第一個,其餘的任意放都可以。
例如樣例:
04:   0100
00:   0000
11:   1011
06:   0110
所以只有一個1的最高位對應的數就是11,放在第一個,其餘任意放即可

/*****************************
*author:ccf
*source:CF_Round_D\C 
*topic:
*******************************/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <unordered_map>
#define ll __int64
using namespace std;

const int N = 1e5+7;
vector<int> vv[N];
int n,a[N];
unordered_map<int,int> mapp;
bool cmp(int x,int y){
	return x > y;
}

int main(){
	//freopen("data.in","r",stdin);
	scanf("%d",&n);
	for(int i = 1;i <= n; i++)
		scanf("%d",a+i);
	for(int i = 30; i >= 0; --i){
		for(int j = 1; j <= n; j++)
			if(a[j] & (ll)(1 << i)) vv[i].push_back(a[j]);
	}
	for(int i = 30; i >= 0; i--)
		if(vv[i].size() == 1 && !mapp[vv[i][0]]){
			printf("%d ",vv[i][0]),mapp[vv[i][0]] = 1;
		} 
	for(int i = 1; i <= n; ++i){
		if(!mapp[a[i]])
			printf("%d ",a[i]);
	}
	return 0;
}

發佈了132 篇原創文章 · 獲贊 54 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章