Eighty seven (bitset优化) 在n个数中取m个数和为k
2016-09-30 12:51
411 查看
Eighty seven
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 802 Accepted Submission(s): 274
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.
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
Recommend
wange2014 | We have carefully selected several similar problems for you: 5906 5905 5904 5903 5902
题意:
给出n个数,q个询问i,j,k,每次要求去掉1~3个数,问剩下的数是否存在选出10个数的总和为87
思路:
由于n<=50 很小,且查询次数Q很大。所以我们可以先初始化所有的可能,然后直接查询结果即可。
bitset<100> dp[11]
dp[i][j]表示i个数和为j,0表示不存在,1表示存在。
如dp[1][2]==1 表示有个数为2 dp[2][5]==1表示有两个数的和为5
思考 dp[t] |= dp[t-1]< < a[i]
dp[t] |= dp[t-1]< < a[i] 表示dp[t-1]的数都右移a[i]位后和dp[t] 取或。
当前b[0][2]=1 b[0][5]=1
如:
b[0]<<2 bitset[0]里面的所有数出现的位置向右移两个位置
例:现在有3个数为1,2,3,4,问能否取出两个数和为5。dp[][]的变化过程如下:
开始时:dp[0][0]表示有0个数和为0满足,所以dp[0][0]=1
加入1后,1个数和为1满足,所以dp[1][1]=1;
加入2后,2个数的和为3满足,1个数的和为2满足,所以dp[2][3]=1,dp[1][2]=1;
加入3后,两个数的和为4,5满足,一个数的和为3满足,所以dp[2][4]=1,dp[2][5]=1,dp[1][3]=1;
…..
所以可以表示出所有的情况。
今后求在n个数中取m个数和为k,是否存在,则可用bitset b[m] 来快速完成 时间复杂度为O(NM)
代码:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 802 Accepted Submission(s): 274
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.
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
Recommend
wange2014 | We have carefully selected several similar problems for you: 5906 5905 5904 5903 5902
题意:
给出n个数,q个询问i,j,k,每次要求去掉1~3个数,问剩下的数是否存在选出10个数的总和为87
思路:
由于n<=50 很小,且查询次数Q很大。所以我们可以先初始化所有的可能,然后直接查询结果即可。
bitset<100> dp[11]
dp[i][j]表示i个数和为j,0表示不存在,1表示存在。
如dp[1][2]==1 表示有个数为2 dp[2][5]==1表示有两个数的和为5
思考 dp[t] |= dp[t-1]< < a[i]
dp[t] |= dp[t-1]< < a[i] 表示dp[t-1]的数都右移a[i]位后和dp[t] 取或。
当前b[0][2]=1 b[0][5]=1
如:
b[0]<<2 bitset[0]里面的所有数出现的位置向右移两个位置
0 1 2 3 4 5 6 7 8 9 10 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0
例:现在有3个数为1,2,3,4,问能否取出两个数和为5。dp[][]的变化过程如下:
开始时:dp[0][0]表示有0个数和为0满足,所以dp[0][0]=1
加入1后,1个数和为1满足,所以dp[1][1]=1;
加入2后,2个数的和为3满足,1个数的和为2满足,所以dp[2][3]=1,dp[1][2]=1;
加入3后,两个数的和为4,5满足,一个数的和为3满足,所以dp[2][4]=1,dp[2][5]=1,dp[1][3]=1;
…..
所以可以表示出所有的情况。
今后求在n个数中取m个数和为k,是否存在,则可用bitset b[m] 来快速完成 时间复杂度为O(NM)
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <bitset> 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; }
相关文章推荐
- HDU 5890 Eighty seven (DP + bitset) 优化 (2016 青岛网络赛)
- hdu_5890_Eighty seven(bitset优化DP)
- hdu 5890 Eighty seven 暴力+bitset优化背包
- HDU 5890 Eighty seven(bitset优化dp)
- hdu5890 Eighty seven(背包+bitset优化)
- HDU5890:Eighty seven(Bitset优化背包)
- SPOJ:Dandiya Night and Violence(Bitset优化)
- hdu 5506 bitset 优化
- hdu5890Eighty seven【bitset优化01背包】
- 2014 ACM/ICPC Asia Regional Beijing Online(hdu5036 概率+bitset优化)
- hdu5972 快速匹配bitset优化(shiftand算法)2016ACM/ICPC亚洲区大连站
- bitset优化背包...
- hdu5313Bipartite Graph(二分图染色+DP(bitset优化))
- Golang 优化之路——bitset
- LibreOJ β Round #2 B.贪心只能过样例【Dp+bitset优化】
- 【BZOJ】4643 卡常大水题【bitset优化bfs】
- hihoCoder : 题目1 : 小Hi的烦恼(bitset应用 TODO: 待优化 )
- 2017 四川省赛 D Dynamic Graph 思维+拓扑排序+bitset 优化
- HDU4460-Friend Chains-BFS+bitset优化
- “玲珑杯”ACM比赛 Round #24: C. この戦いが終わったら(BFS+bitset优化暴力)