题意: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背包)的更多相关文章

  1. Codeforces 366C Dima and Salad:背包dp

    题目链接:http://codeforces.com/problemset/problem/366/C 题意: 有n个物品,每个物品有两个属性a[i]和b[i]. 给定k,让你选出一些物品,使得 ∑ ...

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

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

  3. CF Dima and Salad 01背包

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

  4. codeforces 366C Dima and Salad 【限制性01背包】

    <题目链接> 题目大意: 在一个水果篮里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出来一些做一个水果沙拉, 并且要求他的水果沙拉的美味度是卡路里 ...

  5. Codeforces 366C Dima and Salad

    http://codeforces.com/problemset/problem/366/C 题意:在一个冰箱里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出 ...

  6. 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 ...

  7. Dima and Salad(完全背包)

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

  8. 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 ...

  9. CodeForces-366C Dima and Salad 对01背包的理解 多个背包问题

    题目链接:https://cn.vjudge.net/problem/CodeForces-366C 题意 给出n个水果和一个常数k,其中每个水果都有两种性质ai, bi(美味度,卡路里量). 要保证 ...

随机推荐

  1. oracle扩展dblink数。

    [标记]在进行数据迁移时:出现 Compilation errors for PROCEDURE ZDGAME.GFF_FETCH_MZR_LOG Error: ORA-04052: error oc ...

  2. Python pymysql 模块

    pymysql 是 Python3 连接 MySQL 的一个模块,常见用法如下: [root@localhost ~]$ pip3 install pymysql # 安装 pymysql 模块 In ...

  3. 使用React写的一个小小的登录验证密码组件

    哎,算了.直接上代码吧,不懂得私聊我把 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  4. DOS cmd - how to ping a remote host with specified port

    You can use ping to test whether you can connect to a remote host: ping baidu.com ping 125.6.45.88 ( ...

  5. java基础---->多线程之synchronized(六)

    这里学习一下java多线程中的关于synchronized的用法.我来不及认真地年轻,待明白过来时,只能选择认真地老去. synchronized的简单实例 一. synchronized在方法上的使 ...

  6. 【BZOJ2668】[cqoi2012]交换棋子 费用流

    [BZOJ2668][cqoi2012]交换棋子 Description 有一个n行m列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态.要求第i行第j列 ...

  7. c# 执行 CreateHandle() 时无法调用值 Dispose()

    在多线程C#开发中,遇到错误 执行 CreateHandle() 时无法调用值 Dispose().,这个错误是在关闭窗体的时候出来的. 原因是因为窗体还存在CreateHandle()事件,所以还不 ...

  8. jvm原理之内存机制

    转自:https://www.cnblogs.com/dreamowneryong/p/6381633.html JVM栈由堆.方法区,栈.本地方法栈.程序计数器等部分组成,结构图如下所示: 还有一张 ...

  9. 170705、springboot编程之自定义properties

    spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,可以这样用,如下! 在application.properties文件中增加信息 1 ...

  10. Intellij IDEA常用配置详解

    1. IDEA内存优化 先看看你机器本身的配置而配置. \IntelliJ IDEA 8\bin\idea.exe.vmoptions -------------------------------- ...