Codeforces 687C The Values You Can Make(DP)

题目大概说给n个各有价值的硬币,要从它们中选出若干个组合成面值k,而要求的是各个方案里这些选出的硬币能组合出来的面值有哪些。

有点绕。。

  • dp[i][j][k]表示前i个硬币中 能否 组合成面值j且选出的硬币能组合成面值k
  • 转移要考虑全面。。三个方向转移,第i个不选、第i个选但不参与选出硬币去组合成k、第i个选且参与选出硬币去组成成k
 1 #include<cstdio>
 2 using namespace std;
 3
 4 bool d[555][555][555];
 5 int main(){
 6     int n,K,a;
 7     scanf("%d%d",&n,&K);
 8     d[0][0][0]=1;
 9     for(int i=0; i<n; ++i){
10         scanf("%d",&a);
11         for(int j=0; j<=K; ++j){
12             for(int k=0; k<=K; ++k){
13                 if(d[i][j][k]==0) continue;
14                 d[i+1][j][k]=1;
15                 if(j+a<=K){
16                     d[i+1][j+a][k]=1;
17                     if(k+a<=K){
18                         d[i+1][j+a][k+a]=1;
19                     }
20                 }
21             }
22         }
23     }
24     int cnt=0;
25     for(int k=0; k<=K; ++k){
26         if(d[n][K][k]){
27             ++cnt;
28         }
29     }
30     printf("%d\n",cnt);
31     for(int k=0; k<=K; ++k){
32         if(d[n][K][k]){
33             printf("%d ",k);
34         }
35     }
36     return 0;
37 }
时间: 2024-10-13 06:58:33

Codeforces 687C The Values You Can Make(DP)的相关文章

CodeForces 687C The Values You Can Make(动态规划)

这个也可以说是一个01背包了,里面也有一些集合的思想在里面,首先dp方程,dp[i][j]代表着当前数值为i,j能否被构成,如果dp[i][j] = 1,那么dp[i+m][j] 和 dp[i+m][j+m] = 1,所以转移方程就写出来了,但是注意我们只能从后向前转移,也就是说我们一定要用选上一个数的状态,因为这里是01背包,每一个数只能选一次,如果正着选就是完全背包了. 代码如下: #include<iostream> #include<cstdio> #include<

Codeforces Round #260 (Div. 1) A. Boredom (DP)

题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alex doesn't like boredom. That's why whenever he gets bored, he comes up with

【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)

C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered

Codeforces Round #247 (Div. 2) C. k-Tree (dp)

题目链接 题意: 思路: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 using namespace std; 7 const int mo = 1000000000 + 7; 8 int dp[110][110][110]; 9 10 int main() 11 { 12

Codeforces 460 D Little Victor and Set (构造)

题目链接 构造的好题.表示是看了题解才会做的. 假如[l,r]长度不超过4,直接暴力就行了. 假如[l,r]长度大于等于5,那么如果k = 1,显然答案应该是l:如果k=2,可以找到a^(a+1)=1:如果k=3,首先只取两个就得到一个下界为1,但是可能出现为0的情况,下面再仔细讨论.如果k>=4,可以找到两组a^(a + 1) = 1,所以答案是0. 现在剩下的问题就是如何判断k=3的情况答案能否为0了.答案为0时我们只有可能取了3个数,设它们为x,y,z,并且不妨设有l <= z <

Codeforces Round #262 (Div. 2) 460C. Present(二分)

题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little beaver is a beginner programmer, so informatics is his favorite subjec

CodeForces 686B - Little Robber Girl&#39;s Zoo(暴力)

题意:将一个n(1 <= n <= 100)个元素的序列排成非递减序列,每次操作可以指定区间[ L,R ](区间内元素个数为偶数),将区间内第一项与第二项交换,第三项与第四项交换,第五项与第六项--在2W次内完成排序,输出每次操作. 瞎搞即可,不断检查相邻元素是否满足 前者>=后者,不满足即交换,直到序列满足条件位置(n最大为100绝对不会超过2W步) #include<cstdio> #include<cstring> #include<cctype>

Codeforces118D Caesar&#39;s Legions(DP)

题目 Source http://codeforces.com/problemset/problem/118/D Description Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had n1 footmen and n2 horsemen. Caesar thought that an arrangement is not beautiful if somewhe

ZOJ 1642 Match for Bonus(dp)

Match for Bonus Time Limit: 2 Seconds      Memory Limit: 65536 KB Roy played a game with his roommates the other day. His roommates wrote 2 strings of characters, and gave each character a bonus value. When Roy pinned the positions of one common char