CodeForces 687C The Values You Can Make

$dp$,背包。

$f[i][j][s]$表示前$i$个物品,凑出$j$价格的情况下,能否凑出$s$价格,$f[i][j][s]=1$表示能,否则不能。

转移很简单:如果$f[i][j][s]=1$,那么$f[i+1][j][s]=1$,$f[i+1][j+c[i]][s]=1$,$f[i+1][j+c[i]][s+c[i]]=1$。最后将$f[n][k][s]=1$的$s$都输出就可以了。

$f[500][500][500]$内存会炸,第一维可以用滚动数组优化。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar();  }
}

const int maxn=550;
bool f[2][maxn][maxn];
int n,k,c[maxn];

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&c[i]);
    int now=0; f[now][0][0]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=k;j++)
        {
            for(int s=0;s<=j;s++)
            {
                if(f[now][j][s]==0) continue;
                f[now^1][j][s]=1;
                if(j+c[i]<=k) f[now^1][j+c[i]][s]=1;
                if(j+c[i]<=k&&s+c[i]<=k) f[now^1][j+c[i]][s+c[i]]=1;
            }
        }
        now=now^1;
    }
    int sum=0;
    for(int i=0;i<=k;i++) if(f[now][k][i]) sum++;
    printf("%d\n",sum);
    for(int i=0;i<=k;i++) if(f[now][k][i]) printf("%d ",i);
    printf("\n");
    return 0;
}
时间: 2024-08-09 02:21:09

CodeForces 687C The Values You Can Make的相关文章

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(

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 688E - The Values You Can Make 简单dp

题意:一个数组a[i],你可以挑出若干个数(只能挑一次)加起来等于k, 针对每一种方案,你可以选出这若干个数的子集来组合新数 最后所有的方案能组合出多少种数 分析:一看数据范围n,k<=500 那就是显而易见就是母函数那套了 从1到n,dp[i][j],代表通过前i个元素和为i,能否组合出j #include <cstdio> #include <iostream> #include <ctime> #include <vector> #include

mysql-省市区县-中华人民共和国统计局-最新行政区域划分-有需要其他的格式的,留下联系方式

insert into t_province (ProvinceCode,ProvinceName) values ('410000','河南省'); insert into t_province (ProvinceCode,ProvinceName) values ('420000','湖北省'); insert into t_province (ProvinceCode,ProvinceName) values ('422800','恩施土家族苗族自治州'); insert into t_p

Codeforces 538C. Tourist&#39;s Notes

A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that t

Codeforces Gym 100269 Dwarf Tower (最短路)

题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game named "Dwarf Tower". In this game there are n different items,which you can put on your dwarf character. Items are numbered from 1 to n. Vasya want

Codeforces 788A Functions again - 贪心

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about

codeforces 359 C - Robbers&#39; watch

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their ow

树状数组模拟3个元素的排序 Codeforces 12D Ball

http://codeforces.com/problemset/problem/12/d Ball time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output N ladies attend the ball in the King's palace. Every lady can be described with three val