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(美味度,卡路里量). 要保证 ...
随机推荐
- mysql中根据一个字段相同记录写递增序号,如序号结果,如何实现?
mysql中根据一个字段相同记录写递增序号,如序号结果,如何实现? mysql中实现方式如下: select merchantId, NameCn, send_date, deliver_name ...
- linux命令之scp
两个主机之间copy数据经常用到命令 1.copy文件命令 scp /home/test/1.mp3 root@192.168.1.20:/home/test/music 2.copy文件目录命令 s ...
- docker学习-docker仓库
docker仓库中心:https://hub.docker.com/ 网易蜂巢仓库中心:https://c.163.com/hub#/m/home/
- PyQt4日历部件QXalendarWidget
QCalendarWidget类提供了以月为单位地日历部件.该部件允许用户以一种简单而直接的方式选择日期. #!/usr/bin/python # -*- coding: utf-8 -*- impo ...
- 当新增页面和编辑页面使用同一jsp时
<c:if test="${type eq '1'}"><title>新增页面</title></c:if> <c:if te ...
- 浅谈MVC和MVVM模式
MVC I’m dating with a model… and a view, and a controller. 众所周知,MVC 是开发客户端最经典的设计模式,iOS 开发也不例外,但是 MVC ...
- linux配置免密登录
例如: $ ssh -i ~/ec2.pem ubuntu@12.34.56.78 首先确定你可以以密码的形式连接远程服务器,也可以创建一个非超级管理员用户,并增加 sudo 权限. $ sudo s ...
- HTTP2.0简明笔记
版权声明:本文由史燕飞原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/82 来源:腾云阁https://www.qcloud ...
- php中关于时间的用法
一.时间戳相关: 当前时间戳:time(); 把时间戳转换为时间显示:date("Y-m-d H:i:s", $a); 把日期时间转换 ...
- javascript关于链接的一些用法
(1)javascript:openURL() http://www.kpdown.com/search?name=Ben Nadel 此URL后边有一个name参数,只不过参数的值竟然带了空格,这样 ...