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 aiai. 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?

InputThe first line of input contains an integer t (t≤5)t (t≤5), the number of test cases. tttest cases follow. 
For each test case, the first line consists an integer N(N≤50)N(N≤50). 
The second line contains NN non-negative integers a1,a2,...,aNa1,a2,...,aN. The ii-th number represents the number on the ii-th card. The third line consists an integer Q(Q≤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.OutputFor each turn of each case, output 'Yes' if there exists at least one solution, otherwise output 'No'.Sample Input

1
12
1 2 3 4 5 6 7 8 9 42 21 22
10
1 2 3
3 4 5
2 3 2
10 10 10
10 11 11
10 1 1
1 2 10
1 11 12
1 10 10
11 11 12

Sample Output

No
No
No
Yes
No
Yes
No
No
Yes
Yes

题意:N个物品,分别有自己的值,Q个问题,每次问拿走第X个,第Y个和第Z个物品后,是否能在里面找10个物品,使其和为87。

思路:需要用Bitset优化背包。

感受:对青岛的题目早有耳闻,这次提交了很多次都来TLE,加了输入优化后还是TLE。说明问题不在输入上。

最后Debug了很久,才发现,改了下面一点点就AC了,居然卡“逻辑运算”吗。

      if(ans[x[]][x[]][x[]]==true) puts("Yes");
改为 if(ans[x[]][x[]][x[]]) puts("Yes");

上面的代码改为下面的代码就AC了,而且依然是967ms,而时限猜1000ms。

(如果有幸去青岛赛区,一定要输入输出优化,一定要检查很多遍再提交)

#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
bool ans[][][];
bitset<>s[];
int a[],N;
int read()
{
char c=getchar(); int res;
while(c>''||c<'') c=getchar();
for(res=;c>=''&&c<='';c=getchar()) res=(res<<)+(res<<)+c-'';
return res;
}
void solve(int x,int y,int z)
{
for(int i=;i<=;i++) s[i].reset();
s[][]=;
for(int i=;i<=N;i++){
if(i==x||i==y||i==z||a[i]>) continue;
for(int j=;j>=;j--) s[j]|=s[j-]<<a[i];
}
if(s[][]==) ans[x][y][z]=true;
else ans[x][y][z]=false;
}
int main()
{
int T,Q;
T=read();
while(T--){
scanf("%d",&N);
for(int i=;i<=N;i++)
a[i]=read(); for(int i=;i<=N;i++)
for(int j=i;j<=N;j++)
for(int k=j;k<=N;k++)
solve(i,j,k); Q=read();
while(Q--){
int x[];
x[]=read(); x[]=read(); x[]=read();
sort(x,x+);
if(ans[x[]][x[]][x[]]) puts("Yes");
else puts("No");
}
}
return ;
}

HDU5890:Eighty seven(Bitset优化背包)的更多相关文章

  1. 洛谷 P5527 - [Ynoi2012] NOIP2016 人生巅峰(抽屉原理+bitset 优化背包)

    洛谷题面传送门 一道挺有意思的题,想到了某一步就很简单,想不到就很毒瘤( 首先看到这样的设问我们显然可以想到背包,具体来说题目等价于对于每个满足 \(i\in[l,r]\) 的 \(a_i\) 赋上一 ...

  2. HDU - 6268: Master of Subgraph (分治+bitset优化背包)

    题意:T组样例,给次给出一个N节点的点权树,以及M,问连通块的点权和sum的情况,输出sum=1到M,用0或者1表示. 思路:背包,N^2,由于是无向的连通块,所以可以用分治优化到NlgN. 然后背包 ...

  3. hdu 5890 Eighty seven 暴力+bitset优化背包

    Eighty seven Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) P ...

  4. bitset优化背包

    题目:https://agc020.contest.atcoder.jp/tasks/agc020_c 回忆下一题,是零一背包,主要的做法就是凑出最接近sum/2的价值,然后发现现在的背包的容量是20 ...

  5. HDU 5313 bitset优化背包

    题目大意: 添加尽可能少的边,最后使图形成二分图 一开始将图区分成一个个联通分量,根据二分图染色,计算出每个联通分量的黑色点和白色点的个数 希望添加的边最少,那么合并的时候,希望黑白块尽可能平均,这无 ...

  6. AtCoder3857:Median Sum (Bitset优化背包&&对称性求中位数)

    Median Sum You are given N integers A1, A2, ..., AN. Consider the sums of all non-empty subsequences ...

  7. CCPC 2016 杭州 E. Master of Subgraph(点分治+bitset优化DP)

    题目链接:http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf 题意:给定一棵有 n 个结点的树和一个数 m,对于 i ∈ ...

  8. 01二维背包+bitset优化——hdu5890

    口胡一种别的解法: 三重退背包,g1[j]k]表示不选x的选了j件物品,体积为k的方案数,g[0][0] = 1 , g1[j][k]=dp[j][k]-g1[j-1][k-a[x]] 然后按这样再退 ...

  9. poj 1742 Coins(二进制拆分+bitset优化多重背包)

    \(Coins\) \(solution:\) 这道题很短,开门见山,很明显的告诉了读者这是一道多重背包.但是这道题的数据范围很不友好,它不允许我们直接将这一题当做01背包去做.于是我们得想一想优化. ...

随机推荐

  1. qu de hanzi 首字母

    Function hztopy(hzpy As String) As StringDim hzstring As String, pystring As StringDim hzpysum As In ...

  2. Struts2 与SpringMVC之比较

    1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...

  3. perf stat 输出解读

    http://zhengheng.me/2015/11/12/perf-stat/ http://zhengheng.me/2015/08/30/perf-tools/

  4. JS那些事儿——Gulp的入门使用

    前言 新人使用gulp的一个记录. 首先对于第一个新事物,我会问gulp这是什么? 答:gulp是一个自动化构建工具,它可以做一些自动化的任务,比如: 检查Javascript 编译Sass(或Les ...

  5. 发布ios应用程序

    详见文档 AppDistributionGuide Submit and Release Your App 首先,需要登入itunes connect并且输入必要的信息更改app状态为 waiting ...

  6. 【Kotlin】spring boot项目中,在Idea下启动,报错@Configuration class 'BugsnagClient' may not be final.

    报错如下: Exception encountered during context initialization - cancelling refresh attempt: org.springfr ...

  7. linux命令stat,查看文件详细信息

    可以查看文件的各类具体信息:文件权限的数字形式0664:uid.gid的权限的数字形式等 更多用法参考stat --help lsattr test.sh 查看文件的其他属性:只读属性.只可以追加写属 ...

  8. flask的debug模式下,网页输入pin码进行调试

    网站后端Python+Flask .FLASK调试模式之开启DEBUG与PIN使用? 自动加载: # 方式一 1 2 if __name__ == '__main__':     app.run(ho ...

  9. 走入asp.net mvc不归路:[4]说说Action有哪些常见成员

    一个控制器中,功能最终会落实到一个个Action中实现,最常见的是增删查改操作.这些Action是一个个的方法,一般返回值是ActionResult,并且是public 方法,可以带参数,可以添加元标 ...

  10. Hdu 4274 Spy&#39;s Work

    Spy's Work Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...