CodeForces - 366C Dima and Salad (01背包)
题意:n件东西,有属性a和属性b。要选取若干件东西,使得\(\frac{\sum a_j}{\sum b_j} = k\)。在这个条件下,问\(\sum a_j\)最大是多少。
分析:可以将其转化为0-1背包,令\(c[i] = a[i] - k*b[i]\) 等价于物品的重量,\(a_i\)为物品的价值。因为\(c[i]\)可能小于0,所以用\(dp1[i]\)表示重量为正i时的最大收益,\(dp2[i]\)表示负i时的最大收益。最后求\(dp1[i]+dp2[i]\)的最大值就是答案,注意不存在答案的情况。
#include<bits/stdc++.h>
using namespace std;
#define eps 1e-7
const int maxn = 1e5+5;
typedef long long LL;
int dp1[maxn], dp2[maxn];
int a[maxn], b[maxn];
const int INF = 0x3f3f3f3f;
int c[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int n,k;
scanf("%d %d",&n , &k);
memset(dp1,-INF,sizeof(dp1));
memset(dp2,-INF,sizeof(dp2));
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
dp1[0] = dp2[0] = 0;
for(int i=1;i<=n;++i){
scanf("%d",&b[i]);
c[i] = a[i] - k* b[i];
}
for(int i=1;i<=n;++i){
if(c[i]>=0){
for(int j=10000;j>=c[i];--j){
dp1[j] = max(dp1[j], dp1[j-c[i]]+a[i]);
}
}
else{
c[i] = -c[i];
for(int j=10000;j>=c[i];--j){
dp2[j] = max(dp2[j],dp2[j-c[i]]+a[i]);
}
}
}
int ans=-1;
for(int i=0;i<=10000;++i){
if(dp1[i]==0 && dp2[i]==0 ) continue;
ans = max(ans,dp1[i]+dp2[i]);
}
printf("%d\n",ans);
return 0;
}
CodeForces - 366C Dima and Salad (01背包)的更多相关文章
- Codeforces 366C Dima and Salad:背包dp
题目链接:http://codeforces.com/problemset/problem/366/C 题意: 有n个物品,每个物品有两个属性a[i]和b[i]. 给定k,让你选出一些物品,使得 ∑ ...
- CF#214 C. Dima and Salad 01背包变形
C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...
- CF Dima and Salad 01背包
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- codeforces 366C Dima and Salad 【限制性01背包】
<题目链接> 题目大意: 在一个水果篮里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出来一些做一个水果沙拉, 并且要求他的水果沙拉的美味度是卡路里 ...
- Codeforces 366C Dima and Salad
http://codeforces.com/problemset/problem/366/C 题意:在一个冰箱里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出 ...
- 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 ...
- 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 Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to ...
- CodeForces-366C Dima and Salad 对01背包的理解 多个背包问题
题目链接:https://cn.vjudge.net/problem/CodeForces-366C 题意 给出n个水果和一个常数k,其中每个水果都有两种性质ai, bi(美味度,卡路里量). 要保证 ...
随机推荐
- python2.0_s12_day12_css样式详解
CSScss是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. CSS 存放方式有三种: 一种写法:在<body></body>内部 ...
- 当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决
在Spring里面,当一个singleton bean依赖一个prototype bean,因为singleton bean是单例的,因此prototype bean在singleton bean里面 ...
- Linux curl 命令
curl is a tool to transfer data from or to a server, using one of the supported protocols ( http ,ht ...
- memcache和memcached的区别
用了段时间的memcache和memcached总结下认识,看很多人在用cache的时候,刚刚都没有搞清楚memcache和 memcached的区别,还有就是使用的时候基本都是 get/set 用 ...
- DiscuzX的目录权限设置1
经常有朋友遇到Discuz目录权限设置出错的问题,网上千奇百怪的教程非常多,所谓的终极安全的教程更是满天飞,各种所谓的安全加强软件也随处可见,可实际过程中发现,老手用不上,新手则只会因为这些东西徒增麻 ...
- MyEclipse 2016 CI修改web项目context-root
右击项目properties——>搜索Deployment Assembly,修改如红框所示的Web Context Root
- 图的建立(邻接矩阵)+深度优先遍历+广度优先遍历+Prim算法构造最小生成树(Java语言描述)
主要参考资料:数据结构(C语言版)严蔚敏 ,http://blog.chinaunix.net/uid-25324849-id-2182922.html 代码测试通过. package 图的建 ...
- Android Activity与Fragment生命周期
- 如何在office2010中的EXCEL表格使用求和公式
EXCEL做表格非常方便,有时我们需要对表格中的很多数字进行求和计算,如果用计算器算会非常麻烦,别担心,用求和公式计算,非常简单的 工具/原料 电脑一台 offic2010软件一套 方法/步骤 ...
- 破谣言——iPhone砍价
微信朋友圈和QQ空间很多朋友在传一个iPhone砍价免费送的活动.好吧,砍页面下面的那邪恶广告,第一感觉就是假的.但我要给出证明,所以就有了下面的代码.[只需把UID换成自己的就行],当你砍到5分钱的 ...