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. FastMM使用详解

    FastMM使用详解 一.引言      FastMM 是适用于delphi的第三方内存管理器,在国外已经是大名鼎鼎,在国内也有许多人在使用或者希望使用,就连 Borland 也在delphi2007 ...

  2. Java并发编程实战 读书笔记(二)

    关于发布和逸出 并发编程实践中,this引用逃逸("this"escape)是指对象还没有构造完成,它的this引用就被发布出去了.这是危及到线程安全的,因为其他线程有可能通过这个 ...

  3. 解决本地调用office组件成功,但是发布到IIS中出现的错误(检索COM类工厂中CLSID为{00024500-0000-0000-C000-000000000046}的组件时失败)

    在C#操作word或者Excel,我们可能会用到微软内置的COM组件,会出现很多问题. 如:在本地调试导出Excel没有问题,发布到IIS就有问题了,检测到的异常: 我们会发现在iis上运行的程序,没 ...

  4. jquery 禁用/启用滚动条

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

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

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

  6. [Javascript] Replicate JavaScript Constructor Inheritance with Simple Objects (OLOO)

    Do you get lost when working with functions and the new keyword? Prototypal inheritance can be compl ...

  7. Error: cannot call methods on draggable prior to initialization; attempted to call

    cannot call methods on draggable prior to initialization; attempted to call  报这个问题的根本原因是由于你的引用文件有问题 ...

  8. 天津政府应急系统之GIS一张图(arcgis api for flex)解说(二)鹰眼模块

    解说GIS功能模块实现之前,先大概说一下flexviewer的核心配置文件config.xml,系统额GIS功能widget菜单布局.系统的样式.地图资源等等都是在这里配置的,这里对flexviewe ...

  9. Js两种post方式(转)

    第一种提交post的方式是传统方式,判断浏览器进行post请求. <SCRIPT stype=text/javascript> var xmlobj; //定义XMLHttpRequest ...

  10. 简单的dp hdu 数塔(水题)

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...