CodeForces - 165E Compatible Numbers(SOSdp)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 165E Compatible Numbers(SOSdp)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接:點(diǎn)擊查看
題目大意:給出 nnn 個(gè)數(shù),問能否在數(shù)列中找到一個(gè)數(shù),滿足 ai&aj=0a_i\& a_j=0ai?&aj?=0
題目分析:題目中的式子可以轉(zhuǎn)換為,aia_iai? 是 aja_jaj? 補(bǔ)集的子集,然后就是 SOSdpSOSdpSOSdp 的模板題了,因?yàn)檩敵龅拇鸢干婕暗搅??1-1?1,所以可以將 dpdpdp 初始化為 ?1-1?1,然后直接維護(hù)每個(gè)超集的最大可行答案就好啦
轉(zhuǎn)移的話就是子集向超集轉(zhuǎn)移
代碼:
// Problem: E. Compatible Numbers // Contest: Codeforces - Codeforces Round #112 (Div. 2) // URL: https://codeforces.com/contest/165/problem/E // Memory Limit: 256 MB // Time Limit: 4000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) x&-x using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1<<22; int a[N],dp[N]; int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);memset(dp,-1,sizeof(dp));int n;read(n);for(int i=1;i<=n;i++) {read(a[i]);dp[a[i]]=a[i];}for(int j=0;j<22;j++) {for(int i=0;i<1<<22;i++) {//子集向超集轉(zhuǎn)移if((i>>j)&1) {dp[i]=max(dp[i],dp[i^(1<<j)]);}}}for(int i=1;i<=n;i++) {printf("%d ",dp[(N-1)^a[i]]);}return 0; }總結(jié)
以上是生活随笔為你收集整理的CodeForces - 165E Compatible Numbers(SOSdp)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AtCoder - arc100_c O
- 下一篇: CodeForces - 1486B E