http://codeforces.com/contest/366/problem/C

转化为背包问题,可以将a[i]-b[i]*k看成重量,a[i]为价值; 因为a[i]-b[i]*k可以为负数,所以应该分开讨论。结果就是dp[10000],如果等于0则输出-1,否则输出dp[10000];

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 500005
using namespace std; int a[maxn],b[maxn];
int w[maxn];
int dp[maxn];
int n,k; int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
for(int i=; i<=n; i++)
{
scanf("%d",&b[i]);
}
for(int i=; i<=n; i++)
{
w[i]=a[i]-b[i]*k;
}
memset(dp,-,sizeof(dp));
dp[]=;
for(int i=; i<=n; i++)
{
if(w[i]>=)
{
for(int j=; j>=w[i]; j--)
{
if(dp[j-w[i]]!=-)
dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
}
}
else
{
for(int j=; j<; j++)
{
if(dp[j-w[i]]!=-)
dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
}
}
}
if(dp[]==) printf("-1\n");
else printf("%d\n",dp[]);
}
return ;
}

cf C. Dima and Salad的更多相关文章

  1. CF Dima and Salad 01背包

    C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  3. Dima and Salad(完全背包)

    Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)

    C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. Codeforces Round #214 (Div. 2) C. Dima and Salad 背包

    C. Dima and Salad   Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to ...

  6. codeforces-214(Div. 2)-C. Dima and Salad+DP恰好背包花费

    codeforces-214(Div. 2)-C. Dima and Salad 题意:有不同的沙拉,对应不同的颜值和卡路里,现在要求取出总颜值尽可能高的沙拉,同时要满足 解法:首先要把除法变成乘法, ...

  7. Codefroces 366 C Dima and Salad(dp)

    Dima and Salad 题意:一共有n种水果,每种水果都有一个ai, bi,现求一个最大的ai总和,使得ai之和/对应的bi之和的值等于K. 题解:将bi转换成偏移量,只要偏移到起点位置,就代表 ...

  8. cf 366C C. Dima and Salad(01背包)

    http://codeforces.com/contest/366/problem/C 题意:给出n个水果的两种属性a属性和b属性,然后挑选苹果,选择的苹果必须要满足这样一个条件:,现在给出n,k,要 ...

  9. cf D. Dima and Trap Graph

    http://codeforces.com/contest/366/problem/D 遍历下界,然后用二分求上界,然后用dfs去判断是否可以. #include <cstdio> #in ...

随机推荐

  1. poj2063 Investment(多次完全背包)

    http://poj.org/problem?id=2063 多次完全背包~ #include <stdio.h> #include <string.h> #define MA ...

  2. HDU_2027——统计元音

    Problem Description 统计每个元音字母在字符串中出现的次数.   Input 输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串.   Output ...

  3. canvas生成遮罩图片

         首先我们知道css3中增加了不少好用.好玩的css3样式可以使用.今天我们要说到是遮罩.        它的使用方式也不复杂,和background使用方式差不多.使用mask-image就 ...

  4. CoreText学习(一)Base Objects of Core Text

    最近要做一个读入Word,PDF格式等的文件并且加以编辑的程序,本来以为使用Text Kit结合Text View来打开doc文件是完全没问题的,结果用了各种方法打开要么是数据是nil,要么打开的文字 ...

  5. CSS3:优雅地绘制不规则ICON

    早上在w3ctech上看到 中国第二届CSS Conf总结  的时候,真是开心极了: 自从去年在慕课网上看了第一届CSS conf 视频之后,整个人都震惊了,原来还有大会是专门用来讨论CSS的,而且分 ...

  6. [React] React Router: hashHistory vs browserHistory

    In this lesson we'll look at hashHistory which uses a hash hack to track our route changes vs browse ...

  7. Activity具体解释(生命周期、以各种方式启动Activity、状态保存,全然退出等)

    一.什么是Activity? 简单的说:Activity就是布满整个窗体或者悬浮于其它窗体上的交互界面.在一个应用程序中通常由多个Activity构成,都会在Manifest.xml中指定一个主的Ac ...

  8. 解决svn状态图标不显示的办法

    SVN是一款出色的代码版本控制工具,大部分开发者都在使用.由于前不久刚做了系统,所以要重装一下SVN,结果就出了问题,问题就是,不管是文件处于什么状态他的提示图标都不显示,这就太不给力了吧.通过搜寻, ...

  9. mysql主从监控

    要求:检测myslq从库状态,跳过固定的错误号,每隔30秒检测一次,如果符合条件自动跳过或者是重启从库 1)取出mysql从库的关键字 [root@localhost scripts]# mysql ...

  10. Android/Linux boot time优化

    基于analyze_boot.py分析Android/Linux的kernel boot时间 1.修改HiKey的BoardConfig.mk文件,使能initcall_debug,增加dmesg b ...