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

Eighty seven

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)

Problem Description

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 Ncards with numbers. The number on the i-th card is ai. In class, each turn he will remove no more than 3 cards and let students choose any ten cards, the sum of the numbers on which is 87. 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), the number of test cases. t test cases follow.
For each test case, the first line consists an integer N(N≤50).
The second line contains N non-negative integers a1,a2,...,aN. The i-th number represents the number on the i-th card. The third line consists an integer Q(Q≤100000). Each line of the next Q lines contains three integers i,j,k, representing Mr.Fib will remove the i-th, j-th, and k-th cards in this turn. A question may degenerate while i=j, i=k or j=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

Source

2016 ACM/ICPC Asia Regional Qingdao Online

bitset把87*n优化成10*n;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=1e5+10,M=1e6+10,inf=1e9+10,mod=1e9+7;
const ll INF=1e18+10;
bitset<90>dp[11];
int ans[60][60][60];
int a[100],n,m;
int q[5];
int check(int x,int y,int z)
{
    for(int i=0;i<=n;i++)dp[i].reset();
    dp[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        if(i!=x&&i!=y&&i!=z)
            for(int t=10;t>=1;t--)
                dp[t]|=dp[t-1]<<a[i];
    }
    if(dp[10][87]==1)
        return 1;
        return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(ans,0,sizeof(ans));
        scanf("%d",&n);
        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++)
                if(check(i,j,k))ans[i][j][k]=1;
        scanf("%d",&m);
        while(m--)
        {
            for(int i=0;i<3;i++)
                scanf("%d",&q[i]);
            sort(q,q+3);
            if(ans[q[0]][q[1]][q[2]])
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}
时间: 2025-01-11 19:42:50

hdu 5890 Eighty seven 暴力+bitset优化背包的相关文章

HDU5890: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 wi

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

题意:T组样例,给次给出一个N节点的点权树,以及M,问连通块的点权和sum的情况,输出sum=1到M,用0或者1表示. 思路:背包,N^2,由于是无向的连通块,所以可以用分治优化到NlgN. 然后背包可以用bitset优化.注意不要想着背包合并背包,背包只能合并单点. #include<bits/stdc++.h> #define pb push_back #define rep(i,a,b) for(int i=a;i<=b;i++) #define Gv G[u][i] #defin

HDU 5890 Eighty seven(DP+bitset优化)

题目链接 Eighty seven 背包(用bitset预处理)然后对于每个询问O(1)回答即可. 预处理的时候背包. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i, a, b) for(int i(a); i <= (b); ++i) 6 #define dec(i, a, b) for(int i(a); i >= (b); --i) 7 8 const int N = 52; 9 1

Hdu 5890 Eighty seven

给些数字,可能去掉其中一些,检验能否算到87 用bitsets做背包,细节处之处理i<=j<=k的情况 查询的时候排序一下就好了. #include<bits/stdc++.h> using namespace std; bitset<90>dp[11]; int ans[60][60][60]; int a[100],n,m; int q[5]; int check(int x,int y,int z) { for(int i=0;i<=n;i++)dp[i].

POJ1014 Diving bitset优化背包

dp小水题,结果我还WA了一遍qwq 1 #include <cstdio> 2 #include <bitset> 3 #define maxn 120010 4 using namespace std; 5 bitset<maxn> bit; 6 int a[20],tot,T; 7 void work(){ 8 bit.reset(); 9 bit[0]=1,tot/=2; 10 for(int i=1;i<=6;i++){ 11 int k=1; 12 w

hdu 5036 Explosion bitset优化floyd

http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么1能联通的顶点就可以直接走过去,其他不和1连通的,就需要炸坏.问需要炸弹的期望. 比如一副图是1-->2-->3的.那么期望是11 / 6 假如从1号点开始,1/3概率选中1号点开始,那么需要炸弹数是1(炸开一号),贡献是1/3 假如从2号点开始,1/3概率选中2号点开始,那么需要炸开2号点,然后

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]] 然后按这样再退三层,最后看g3[10][87]的方案数是否非0即可,这样复杂度是O(50*50*50*10*87) 如果直接枚举删掉的数,然后用可行性二维01背包做 复杂度是O(50*50*50)*O(n*10*87) 再加上bitset优化第二维 复杂度/32 ,由于n比较小,所以也差不多 /* 在n个数里找到

hdu 5036 Explosion (bitset优化的传递闭包求解概率)

Explosion Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 142    Accepted Submission(s): 25 Problem Description Everyone knows Matt enjoys playing games very much. Now, he is playing such a g

hdu_5890_Eighty seven(bitset优化DP)

题目链接:hdu_5890_Eighty seven 题意: 50个数,10W个询问,每次问删掉第i,j,k个数后,是否存在一种选10个数和为87的方案,只需要输出 ’Yes’ 或者 ’No’ 题解: 暴力:不同的询问大概2W个,每个暴力bitset DP,抠一抠能卡着过.优化1:先求出一组解,如果询问和解没交就是’Yes’,否则暴力,不同的询问大概1W个:优化2:先预处理出所有询问的答案,能方便的复用之前的DP数组,不用每次从头开始重新求. 以上是camp的题解 这里学习了一下bitset,把