题意是给你一个数组,让你求出他前缀或最大的排序。
思路:我一开始想的纯暴力,但是又知道肯定过不了。搜了一下,看到了大佬的解题思路。真牛啊。res是前缀或,每次排序获得能让他最大的数,因为一共也就30来位(int型),所以直接100*30*200000=6*10^8的复杂度。
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
string s;
#define maxn 03f3f3f3f
#define ll long long
//#define int long long
ll res;
int cmp(int a, int b)
{
return (res | a) > (res | b);
}
void solve()
{
res = 0;
int n;
cin >> n;
vector<int>a(n+1);
for (int i = 1; i <= n; i++)
cin >> a[i];
int cnt = min(30, n);//n是防止越界,最大也就30
for (int i = 1; i <= cnt; i++)
{
sort(a.begin() + i, a.end(), cmp);
res |= a[i];
}
for (int i = 1; i <= n; i++)
{
cout << a[i] << " ";
}
cout << endl;
return;
}
signed main()
{
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}