【hdu5890】【bitset+dp】Eighty seven【每次从序列中删掉第i,j,k个数后,是否存在一种选10个数和为87的方案】

传送门:HDU 5890 Eighty seven

描述:

Eighty seven

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 819    Accepted Submission(s): 277


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  N cards 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 (t5) , the number of test cases.  t  test cases follow.
For each test case, the first line consists an integer  N(N50) .
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(Q100000) . 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
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5906  5905  5904  5903  5902 
题意:

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

思路:

暴力:不同的询问大概2W个,每个暴力bitset DP,抠一抠能卡着过。

优化先预处理出所有询问的答案,能方便的复用之前的DP数组,不用每次从头开始重新求。

再加个读入挂,速度快了点


学习一下bitset。

可以当作一个bool型数组考虑,bitset<N> bs;  可以考虑成一个数组bool bs[N]。

相关操作:

bs.set(); 全部置1,bs.reset()全部置0;

bs.set(pos);等价于bs[pos]=1,bs.reset(pos)等价于bs[pos]=0;

最重点的来了,bitset<N> a, b;

a和b可以直接进行操作,

!a //按位取反
a^b //按位异或
a|b //按位或
a&b //按位与
a=b<<3 //整体移位
a.count(); //a中1的个数

bitset有什么用呢(也许还有其他的用处,这里讲的是本题用到的)

如果有一个bool数组 a[N] 和b[N] 把每一个位异或的话,一定是

for (int i = 0; i < N; ++i) c[i] = a[i] ^ b[i];

但是如果用bitset直接a^b的话,只需要O(N/机器字节数)

这样可以实现常数优化。

---


代码:

#include<bits/stdc++.h>
#define ll __int64
using namespace std;
bitset<90>dp[11];
int ans[60][60][60];
int a[100],n,m;
int q[5];

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}

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=1; t<=10; t++)
        dp[t]|=dp[t-1]<<a[i];
  }
  if(dp[10][87]==1)return 1;
  return 0;
}

int main(){
  #ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
  #endif
  int t;
  read(t);
  while(t--){
    memset(ans, 0, sizeof(ans));
    read(n);
    for(int i=1; i<=n; i++)
      read(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;
    read(m);
    while(m--){
      for(int i=0; i<3; i++)
        read(q[i]);
      sort(q, q+3);
      if(ans[q[0]][q[1]][q[2]])printf("Yes\n");
      else printf("No\n");
    }
  }
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值