cf C. Dima and Salad
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的更多相关文章
- CF Dima and Salad 01背包
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- CF#214 C. Dima and Salad 01背包变形
C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...
- Dima and Salad(完全背包)
Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 ...
- 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 ...
- codeforces-214(Div. 2)-C. Dima and Salad+DP恰好背包花费
codeforces-214(Div. 2)-C. Dima and Salad 题意:有不同的沙拉,对应不同的颜值和卡路里,现在要求取出总颜值尽可能高的沙拉,同时要满足 解法:首先要把除法变成乘法, ...
- Codefroces 366 C Dima and Salad(dp)
Dima and Salad 题意:一共有n种水果,每种水果都有一个ai, bi,现求一个最大的ai总和,使得ai之和/对应的bi之和的值等于K. 题解:将bi转换成偏移量,只要偏移到起点位置,就代表 ...
- cf 366C C. Dima and Salad(01背包)
http://codeforces.com/contest/366/problem/C 题意:给出n个水果的两种属性a属性和b属性,然后挑选苹果,选择的苹果必须要满足这样一个条件:,现在给出n,k,要 ...
- cf D. Dima and Trap Graph
http://codeforces.com/contest/366/problem/D 遍历下界,然后用二分求上界,然后用dfs去判断是否可以. #include <cstdio> #in ...
随机推荐
- 【HDOJ】1406 Ferry Loading III
模拟,注意需要比较队头与当前时间的大小关系. #include <cstdio> #include <cstring> #include <cstdlib> #de ...
- BZOJ3399: [Usaco2009 Mar]Sand Castle城堡
3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 22 Solved: 17[Sub ...
- HDOJ 1266 Reverse Number(数字反向输出题)
Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give my bes ...
- Subarray Sum Closest
Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the ...
- MYSQL存储过程和函数学习笔记
学至Tarena金牌讲师,金色晨曦科技公司技术总监沙利穆课程笔记的综合. 1. 什么是存储过程和函数 将SQL语句放入一个集合里,然后直接调用存储过程和函数来执行已经定义好的SQL语句,通过存储过程和 ...
- JS中undefined与null的区别
1.概述: 在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? ...
- [Falcor] Building Paths Programmatically
model.setValue('genreList[0].titles[0].rating', 5) .then(function (value) { model.get('genreList[0.. ...
- UIView层次管理bringSubviewToFront,sendSubviewToBack
将一个UIView显示在最前面只需要调用其父视图的 bringSubviewToFront()方法. 将一个UIView层推送到背后只需要调用其父视图的 sendSubviewToBack()方法. ...
- Mac打造python2 python3开发环境
最新版的MacOS Sierra默认带的python环境仍然为2.7.10,本文将介绍使用Brew安装python3.5并做简单的配置,打造python2,python3共存的开发环境 直接尝试bre ...
- 重学《C#高级编程》(泛型与数组)
前段时间工作比较忙,就没有写随笔了,现在继续. 前两天重新看了泛型和数组两章,简单说下我自己的收获吧 泛型 我们知道数组是一种批量的数据格式,而泛型其实就是一种自定义的批量数据格式,当数组和C#现有的 ...