当前位置:网站首页>Cf. bits and pieces (subset pressing DP + pruning)
Cf. bits and pieces (subset pressing DP + pruning)
2022-07-24 18:22:00 【Alloy Bunny sauce】
Ideas :
Greedily consider from high to low , For arbitrary i, If a[i] Of the x Is it 1, Then don't worry ; If the first x Is it 0, Then I hope I can find it a[j] and a[k] Satisfy i<j<k, also a[j] & a[k] = 1.
Then we can traverse in reverse order i, For all the j
[i+1, n], We put a[j] The number of occurrences of the subset of is recorded in A In array , such as [i+1,n] Within the scope of a[j] by 10,11,100( All written in binary ), that A[10] = 2, Because two numbers have subsets 10.
that , When does it exist a[j] and a[k] Satisfy i<j<k, also a[j] & a[k] = 1 Well ? Namely A[mask] >= 2 When , among mask It contains a[i] All that need to be supplemented at present 1.
If you don't understand , Please look at the code. :
code1(tle):
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define debug cout<<111<<endl;
const int N = 1e6+5;
int n, a[N], f[(1<<20)+5], ans=0;
void add(int x,int p){
if(p==-1) {f[x]++; return;} // All bits are traversed , The number of schemes in this subset +1
add(x,p-1); // Keep the original sample , Down add
if(x&(1<<p)) add(x^(1<<p), p-1); // The first p Bit handle 1 Change to 0, Down add
}
inline void solve(){
cin>>n; FOR(i,1,n) cin>>a[i];
add(a[n],20); add(a[n-1],20);
for(int i=n-2; i>=1; i--){
int mask=0;
for(int j=20; j>=0; j--){
if(a[i] & (1<<j)) continue; // If a[i] This is originally 1, Then don't worry about
if(f[mask|(1<<j)] >= 2) mask |= (1<<j); // Greedy j position
}
ans = max(ans, a[i]|mask);
add(a[i],20); // hold a[i] Also join the right f In the contribution of
}
cout<<ans;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T=1; while(T--) solve();
}Ran goose , This code tle 了 .
Why? ? Because the parameters are exactly the same add It may be done many times , But the same add After more than two times, it will not affect A[mask] >= 2 了 , So it can be used f[i][j] Record add(i,j) The number of iterations , Traversed more than twice directly return fall , Finish pruning .
This is after pruning ac Code :
code2(ac):
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define debug cout<<111<<endl;
const int N = 1e6+5;
int n, a[N], A[N*2], ans=0;
int f[N*2][22];
void add(int x,int p){
if(p==-1) {A[x]++; return;} // All bits are traversed , The number of schemes in this subset +1
if(f[x][p]>=2) return; // This add It has been traversed twice , All corresponding A[y](y yes x Subset ) At least also 2 了 , So prune off
f[x][p]++; // I went through it once add(x,p), recorded
add(x,p-1); // Keep the original sample , Down add
if(x&(1<<p)) add(x^(1<<p), p-1); // The first p Bit handle 1 Change to 0, Down add
}
inline void solve(){
cin>>n; FOR(i,1,n) cin>>a[i];
add(a[n],20); add(a[n-1],20);
for(int i=n-2; i>=1; i--){
int mask=0;
for(int j=20; j>=0; j--){
if(a[i] & (1<<j)) continue; // If a[i] This is originally 1, Then don't worry about
if(A[mask|(1<<j)] >= 2) mask |= (1<<j); // Greedy j position
}
ans = max(ans, a[i]|mask);
add(a[i],20); // hold a[i] Also join the right f In the contribution of
}
cout<<ans;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T=1; while(T--) solve();
}边栏推荐
猜你喜欢

Typora 它依然是我心中的YYDS 最优美也是颜值最高的文档编辑神器 相信你永远不会抛弃它

【OpenCV】—阈值化

pycharm配置opencv库

CF. Bits And Pieces(子集状压dp + 剪枝)

Emerging potential of interactive virtual reality technology in drug discovery

第五届数字中国建设峰会在福建福州开幕

Laravel笔记-用户登录时密码进行RSA加密(提高系统安全性)

4. Basic type and reference type?

Icml2022 Best Paper Award: learning protein reverse folding from millions of predicted structures

jmeter --静默运行
随机推荐
Sword finger offer 21. adjust the array order so that odd numbers precede even numbers
JS to achieve progress steps (small exercise)
字符串常用方法(2)
数组常用方法(2)
Int8 & int8, have you ever stumbled like this?
Learn redisson from scratch ------- topics (subscription and distribution)
Ship new idea 2022.2 was officially released, and the new features are really fragrant!
Alibaba /1688 API instructions for searching products by map (pailitao)
Date function format conversion
13 essential methods of color!
数组扁平化.flat(Infinity)
【obs】依赖库: x264 vs 构建
New can also create objects. Why do you need factory mode?
【obs】视频、音频编码与rtmp发送的配合
undefined reference to H5PTopen
下拉列表组件使用 iScroll.js 实现滚动效果遇到的坑
2022 the latest short video de watermarking analysis API interface sharing
树链剖分板子
Section 9 cache penetration follow Daewoo redis ------- directory posts
odoo中的bom理解