题意:50个数,10W个询问,每次问删掉第i,j,k个数后,是否存在一种选10个数和为87的方案,只需要输出 ’Yes’ 或者 ’No’
题解:暴力:不同的询问大概2W个,每个暴力bitset DP,抠一抠能卡着过。优化1:先求出一组解,如果询问和解没交就是’Yes’,否则暴力,不同的询问大概1W个;优化2:先预处理出所有询问的答案,能方便的复用之前的DP数组,不用每次从头开始重新求。
思路:学习了一波bitset...
#include<bits/stdc++.h>
using namespace std;
#define ll long long
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;
}
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 (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 .
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