当前位置:网站首页>Compression state DP bit operation
Compression state DP bit operation
2022-06-30 11:13:00 【Dongdongyu】
I brushed a little video while writing this blog , Today, all provinces and cities have basically given out scores , Then I think this year's question should be very difficult , There is a sentence in the comment area that makes me feel good ,“ At least we should give ordinary people a way to live ”, alas … It's tragic , Okay , Gossip and less narration , Direct to the point !
Today I wrote a Luogu , The current state is that there is no idea without looking at the problem solution , I have to write for more than an hour ,( I'm too fond of vegetables.
subject :
https://www.luogu.com.cn/problem/P1441
But today I have a train of thought without looking at the solution , I did a similar problem yesterday , Result timeout + The answer is partly correct , Send , In desperation, I turned to the problem solving area , I want to post one first Code , When I saw this , I was wondering why 100 Code to line , You can finish it in ten lines , Even more , alas
#include <bitset>
#include <cstdio>
int w[25];
int table[16] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
int popcount(unsigned int x) {
// return x In binary 1 The number of
int ret = 0;
for(int i = 0; i < 8; i++) ret += table[x & 15], x >>= 4;
return ret;
}
int main() {
int n, m, ans = 0;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) scanf("%d", w + i);
for(int i = 0, li = 1 << n; i < li; i++) {
if(popcount(i) == n - m) {
std::bitset<2010> S;
S[0] = 1;
for(int j = 0; j < n; j++) if(i & (1 << j)) S |= S << w[j];
int siz = S.count();
ans = ans > siz ? ans : siz;
}
}
printf("%d\n", ans - 1);
return 0;
}
The source of the above code :https://www.luogu.com.cn/problem/solution/P1441,pantw For the author
He calculated the number of digits skillfully , But it should be the regular operation of the boss .
The focus is still on the cycle
The most important thing about him is this sentence :
if(i & (1 << j)) S |= S << w[j];
What do you mean , We can think about the complete knapsack problem , The following is the weight of the backpack , This is also , He takes and as the subscript , Then use the bit operation to mark , For example, the following example :
3 1
1 2 2
Obvious , I'm taking out 2 after 1 and 2 It can make up 1,2,3 So the output is 3
At this moment , If the original bit is S[0]=1,S[1]=1( Only 1 a number ) Then add in one 2, Use the above operation , speak S Move two bits to the left and match the original S Phase or , In this way, it becomes S[0]=1,S[1]=1,S[2]=1,S[3]=1, May use questions , What if the added number coincides with the original number ? Very simple, for example , The original state is S[0]=1,S[1]=1,S[3]=1 Add in one 2 after S and S[2]=1,S[3]=1,S[5]=1 Phase or , In this way, it is 0,1,2,3,5 yes 1, Two 3 Or it becomes a
An inappropriate method : It's my way , The reason why I have ideas , Because I have done a very similar problem before this problem
https://www.luogu.com.cn/problem/P3694
It is used here dp It's done , That is, the previous state accumulation , Then the back state comes from the front .( But they all use bit operations , The shell is the same
But this method has one drawback , When you count again , It is necessary to know which numbers are added before and have appeared , These should be deleted , So we need to use vectors to record ,( My array , If you want to use bit operations, you have to 2000 Many bits , stay int Only 32 In the case of bit , Let's pull it down .
So the big guy's solution is not bad , Not pure dp, But the method is ingenious .
边栏推荐
- 数学(快速幂)
- The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)
- The reasoning delay on iphone12 is only 1.6 MS! Snap et al. Analyzed the transformer structure latency in detail, and used NAS to find out the efficient network structure of mobile devices
- LVGL 8.2 Image
- The jetpack compose dropdownmenu is displayed following the finger click position
- Deep dive kotlin synergy (18): hot and cold data flow
- 深潜Kotlin协程(十六):Channel
- SQL必需掌握的100个重要知识点:插入数据
- 【leetcode 239】滑动窗口
- LeetCode Algorithm 86. 分隔鏈錶
猜你喜欢
Dickinson's soul chooses its companion
[STL source code analysis] container (to be supplemented)
Dell et Apple, deux entreprises de PC établies, se sont effondrées rapidement
第一届中国数字藏品大会即将召开
The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)
Deep dive kotlin synergy (16): Channel
Every time I look at my colleagues' interface documents, I get confused and have a lot of problems...
MySQL export SQL script file
200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration
The intelligent DNA molecular nano robot model is coming
随机推荐
中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场
The intelligent DNA molecular nano robot model is coming
LVGL 8.2 Simple Colorwheel
经典面试题:负责的模块,针对这些功能点你是怎么设计测试用例的?【杭州多测师】【杭州多测师_王sir】...
Memory escape analysis
datax json说明
【IC5000教程】-01-使用daqIDEA图形化debug调试C代码
LVGL 8.2 Image
iptables目标TPROXY
LVGL 8.2 re-coloring
ESP32-C3入门教程 IoT篇⑤——阿里云 物联网平台 EspAliYun RGB LED 实战之批量生产的解决方案
LVGL 8.2 Image
深潜Kotlin协程(十六):Channel
[proteus simulation] Arduino uno led simulated traffic light
The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)
When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]
[STL source code analysis] iterator
SQL必需掌握的100个重要知识点:汇总数据
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
单片机 MCU 固件打包脚本软件