当前位置:网站首页>C-Eighty seven(背包+bitset)
C-Eighty seven(背包+bitset)
2022-08-05 06:54:00 【咸蛋_dd】
浅学了一下bitset,感觉挺妙的,可以进行背包问题的优化
Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach children how to add numbers up. Before the class, he will prepare NN cards with numbers. The number on the ii-th card is a_iai. In class, each turn he will remove no more than 33 cards and let students choose any ten cards, the sum of the numbers on which is 8787. After each turn the removed cards will be put back to their position. Now, he wants to know if there is at least one solution of each turn. Can you help him?
Input
The first line of input contains an integer t~(t \le 5)t (t≤5), the number of test cases. tt test cases follow.
For each test case, the first line consists an integer N(N \leq 50)N(N≤50).
The second line contains NN non-negative integers a_1, a_2, ... , a_Na1,a2,...,aN. The ii-th number represents the number on the ii-th card. The third line consists an integer Q(Q \leq 100000)Q(Q≤100000). Each line of the next QQ lines contains three integers i,j,ki,j,k, representing Mr.Fib will remove the ii-th, jj-th, and kk-th cards in this turn. A question may degenerate while i=ji=j, i=ki=k or j=kj=k.
Output
For each turn of each case, output 'Yes' if there exists at least one solution, otherwise output 'No'.
题意:给你n个数字,每次拿走里面1到3个数,问你剩下的数里面是否能选出10个数之和为87
思路:因为n只有50,所以可以进行n^3复杂度(约等于,可以优化,不优化会t掉)的预处理,然后01背包的思路,bitset优化,因为是二进制位所以可以很有效的减少时间复杂度
#include <bits/stdc++.h>
#include <bitset>
using namespace std;
const int N=55;
int a[N];
bitset<90> s[11];
int ff[N][N][N];
int n,m;
int solve(int x,int y ,int z)
{
for(int i=0;i<=10;i++) s[i].reset();//将s[i]每一位全部变成0
s[0][0]=1;
for(int i=1;i<=n;i++)
{
if(i==x||i==y||i==z||a[i]>87) continue;
for(int j=10;j>0;j--)
{
s[j]|=(s[j-1]<<a[i]);//异或相当于加上a[i]
}
}
if(s[10][87]!=0)
return 1;
else
return 0;
}
int main()
{
int t;
int x,y,z;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int x=1;x<=n;x++)
for(int y=x;y<=n;y++)
for(int z=y;z<=n;z++)
{
int f=solve(x,y,z);
if(f==1) ff[x][y][z]=1;
else ff[x][y][z]=0;
}
scanf("%d",&m);
while(m--)
{
int b[10];
scanf("%d %d %d",&b[1],&b[2],&b[3]);
sort(b+1,b+4);//必须排序
if(ff[b[1]][b[2]][b[3]]==1)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
边栏推荐
- 对数据类型而言运算符无效。运算符为 add,类型为 text。
- 17-VMware Horizon 2203 虚拟桌面-Win10 手动桌面池浮动(十七)
- Day9 of Hegong Daqiong team vision team training - camera calibration
- 原来使Maya Arnold也能渲染出高质量作品!超赞小技巧
- C# FileSystemWatcher
- cmake 学习使用笔记(三)
- How to avoid online memory leaks
- [Shanghai] Hiring .Net Senior Software Engineer & BI Data Warehouse Engineer (Urgent)
- In the anaconda Promat interface, import torch is passed, and the error is reported in the jupyter notebook (only provide ideas and understanding!)
- 在STM32中使用printf函数
猜你喜欢
TRACE32——C源码关联1
一天学会从抓包到接口测试,通过智慧物业项目深度解析
FPGA parsing B code----serial 4
本地能ping通虚拟机,虚拟机ping不通本地
字节面试流程及面试题无私奉献,吐血整理
Shiny04---Application of DT and progress bar in shiny
真实字节跳动测试开发面试题,拿下年薪50万offer。
Shared memory + inotify mechanism to achieve multi-process low-latency data sharing
Shiny02---Shiny异常解决
RNote108---Display the running progress of the R program
随机推荐
typescript63-索引签名类型
The NDK compiler so libraries
2022熔化焊接与热切割操作证考试题及模拟考试
Flink Learning 10: Use idea to write WordCount and package and run
Tencent Internship Summary
Cannot compare or sort text, ntext, and image data types
Flink Learning 11: Flink Program Parallelism
re正则表达式
风控特征的优化分箱,看看这样教科书的操作
MAYA船的建模
给网站套上Cloudflare(以腾讯云为例)
【instancetype类型 Objective-C】
In the anaconda Promat interface, import torch is passed, and the error is reported in the jupyter notebook (only provide ideas and understanding!)
Technical Analysis Mode (7) Playing the Gap
typescript64-映射类型
After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career
专用机终端安装软件后报IP冲突
IO process thread -> communication between processes -> day7
It turns out that Maya Arnold can also render high-quality works!Awesome Tips
Takeda Fiscal 2022 First Quarter Results Strong; On Track to Achieve Full-Year Management Guidance