【HDU - 5890】Eighty seven(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 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?

Input

The 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.

Output

For 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

题目大意:

50个数,10W个询问,每次问删掉第i,j,k个数后,是否存在一种选10个数和为87的方案,只需要输出 ’Yes’ 或者 ’No’

解题报告:

   直接可行性背包,然后bitset优化一下就可以了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#include<bitset>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
bitset<111> bs[111];
int a[MAX],n;
bool dp[111][111][111];
bool ok(int x,int y,int z) {for(int i = 1; i<=n; i++) bs[i].reset();bs[0][0]=1;for(int i = 1; i<=n; i++) {if(i == x || i == y || i == z) continue;for(int j = 10; j>=1; j--) {bs[j] |= (bs[j-1] << a[i]);}}return bs[10][87];
}
int main()
{int t;cin>>t;while(t--) {scanf("%d",&n);memset(dp,0,sizeof dp);for(int i = 1; i<=n; i++) scanf("%d",a+i);for(int i = 1; i<=n; i++) {for(int j = i; j<=n; j++) {for(int k = j; k<=n; k++) {dp[i][j][k]=0;if(ok(i,j,k)) dp[i][j][k] = 1;}}}int q,x[4];scanf("%d",&q);while(q--) {for(int i = 1; i<=3; i++) scanf("%d",x+i);sort(x+1,x+4);if(dp[x[1]][x[2]][x[3]] == 1) puts("Yes");else puts("No");}}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439317.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 1937 】Finding Seats(二维前缀和+尺取法)

题干&#xff1a; A group of K friends is going to see a movie. However, they are too late to get good tickets, so they are looking for a good way to sit all nearby. Since they are all science students, they decided to come up with an optimization problem …

【POJ - 1459】Power Network(网络流最大流,建图)

题干&#xff1a; A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) > 0 of power, may produce an amount 0 < p(u) < p max(u) of power, may …

【转】React Vue MVC MVVM MVP

首先&#xff0c;在谈这个话题之前&#xff0c; 我们有必要了解一下库和框架的区别。 我们先来看react官网以及vue官网对他们的定位&#xff1a; react: vue: react我们不说了&#xff0c;官网上明明白白说了&#xff0c;人家是一个library&#xff0c;用于构建用户界面。 v…

**【POJ - 3122】 Pie(二分寻值)

题干&#xff1a; My birthday is coming up and traditionally Im serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should b…

【转】IsCallBack属性和IsPostBack属性有什么区别?

if (Page.IsCallback) return; 此句话在page的构造函数中使用&#xff0c;不让page反复生成。比如一个TEXTbox如果不组织页面刷新&#xff0c;其数据会丢失。 以postback方式进行客户端和服务器端的交互的&#xff0c; IsPostBack就是true。 以callback方式进行客户端和服务器…

【转】使用Feature导入WebPart

原文链接&#xff1a;http://www.cnblogs.com/glife/archive/2009/10/27/1590488.html 前些天在刚刚接触WebPart的时候&#xff0c;搜到了一篇《使用Feature导入WebPart》的文章&#xff0c;那个时候对Feature的了解还为零&#xff0c;所以看了是一知半解&#xff0c;等到今天…

【HDU - 5017】Ellipsoid(爬山算法,模拟退火,三分)

题干&#xff1a; Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x 1,y 1,z 1) and (x 2,y 2,z 2) is defined as Input There a…

【转】[SharePoint 开发详解] 一个Feature中使用SPGridView的几个Tips

根据上面一篇随笔所介绍的PC购买流程的项目&#xff0c;在项目中&#xff0c;需要有一个生成订单的功能&#xff0c;能够使得Admin很方便的在获得批准的申请中选取一些来生成订单&#xff0c;要求界面操作简单明了&#xff0c;大概的效果图如下&#xff1a; 点击checkbox&#…

【LeetCode - 131】分割回文串(dp,dfs)

题目链接&#xff1a;https://leetcode-cn.com/problems/palindrome-partitioning/ 题目&#xff1a; 给定一个字符串 s&#xff0c;将 s 分割成一些子串&#xff0c;使每个子串都是回文串。 返回 s 所有可能的分割方案。 示例: 输入: "aab" 输出: [ ["a…

【转】VSTS中版本控制系统Git与TFVC的区别

VSTS&#xff08;Visual Studio Team Services&#xff09; VSTS简单说就是微软TFS(Team Foundation Services)的升级云版&#xff0c;不用像TFS需要在企业内部服务器上部署&#xff0c;并且是免费提供给用户使用的。 每个有微软账号&#xff08;也是免费注册的&#xff09;的…

【LeetCode - 1254】统计封闭岛屿的数目(dfs,连通块)

题目链接&#xff1a;https://leetcode-cn.com/problems/number-of-closed-islands/ 有一个二维矩阵 grid &#xff0c;每个位置要么是陆地&#xff08;记号为 0 &#xff09;要么是水域&#xff08;记号为 1 &#xff09;。 我们从一块陆地出发&#xff0c;每次可以往上下左…

【转】0.SharePoint服务器端对象模型 之 序言

对于刚刚开始接触SharePoint的开发人员&#xff0c;即使之前有较为丰富的ASP.NET开发经验&#xff0c;在面对SharePoint时候可能也很难找到入手的方向。对于任何一种开发平台而言&#xff0c;学习开发的过程大致会包括&#xff1a;开发工具的使用、开发手段的选择和开发语言的编…

【LeetCode - 122】买卖股票的最佳时机 II(贪心 或 dp)

题目链接&#xff1a;https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ 给定一个数组&#xff0c;它的第 i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易&#xff08;多次买卖一支股票…

【转】1.SharePoint服务器端对象模型 之 对象模型概述(Part 1)

在一个传统的ASP.NET开发过程中&#xff0c;我们往往会把开发分为界面展现层、逻辑业务层和数据访问层这三个层面。作为一个应用开发平台&#xff0c;SharePoint是微软在直观的开发能力和自由的扩展能力之间&#xff0c;取到的一个平衡点&#xff0c;其对象模型的设计理念也反映…

【LeetCode - 123】买卖股票的最佳时机 III

题目链接&#xff1a; 给定一个数组&#xff0c;它的第 i 个元素是一支给定的股票在第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。 注意&#xff1a;你不能同时参与多笔交易&#xff08;你必须在再次购买前出售掉之前的股票&#xf…

【转】1.2SharePoint服务器端对象模型 之 对象模型概述(Part 2)

&#xff08;三&#xff09;Url 作为一个B/S体系&#xff0c;在SharePoint的属性、方法参数和返回值中&#xff0c;大量的涉及到了Url&#xff0c;总的来说&#xff0c;涉及到的Url可以分为如下四类&#xff1a; 绝对路径&#xff1a;完整的Url&#xff0c;包含了协议头&…

【LeetCode - 224】基本计算器(栈)

实现一个基本的计算器来计算一个简单的字符串表达式 s 的值。 题目链接&#xff1a;https://leetcode-cn.com/problems/basic-calculator/ 示例 1&#xff1a; 输入&#xff1a;s "1 1" 输出&#xff1a;2 示例 2&#xff1a; 输入&#xff1a;s " 2-1 …

【转】2.1 SharePoint服务器端对象模型 之 访问网站和列表数据(Part 1)

本节将会介绍SharePoint中最为常用的一些对象模型&#xff0c;以及如何使用这些对象模型来访问和操作网站中的数据。几乎所有的SharePoint服务器端开发都会涉及到这些内容&#xff0c;因此应着重掌握本节中所介绍的基本对象模型的使用方法。由于篇幅所限&#xff0c;在介绍每种…

【LeetCode - 1047】删除字符串中的所有相邻重复项(栈)

https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/ 给出由小写字母组成的字符串 S&#xff0c;重复项删除操作会选择两个相邻且相同的字母&#xff0c;并删除它们。 在 S 上反复执行重复项删除操作&#xff0c;直到无法继续删除。 在完成所有重复…

【转】2.2 SharePoint服务器端对象模型 之 访问网站和列表数据(Part 2)

&#xff08;二&#xff09;列表&#xff08;SPList&#xff09; 列表是SharePoint中最为重要的数据容器&#xff0c;我们一般保存在SharePoint中的所有数据&#xff0c;都是保存在列表中&#xff08;文档库也是一种列表&#xff09;&#xff0c;因此列表对象在SharePoint的开…