C. Dima and Salad

题意

有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{\sum_{i=1}^mb_i}==k\),问沙拉最大的美味度是多少?

思路

01背包变形。

对于给出的公式,我们化简一下:

\(\sum_{i=1}^ma_i-k*\sum_{i=1}^mb_i==0\)

就变成了把a[i]-k*b[i]作为体积,a[i]作为价值,向容量为0的背包里放,可以取得的最大价值。

因为a[i]-k*b[i]有正负,所以我们可以分别对正体积和负体积DP,体积为0直接算到答案中,在正负的DP值之中选择体积绝对值相等的最大和。

代码

//#include<bits/stdc++.h>
#include<vector>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<map>
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f; struct note
{
int a,b;
int wei;
} arr[N];
int dp1[N],dp2[N];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1; i<=n; i++)
scanf("%d",&arr[i].a);
int spos=0,sneg=0,ans=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&arr[i].b);
int tmp=arr[i].a-k*arr[i].b;
arr[i].wei=tmp;
if(tmp>0)
spos+=tmp;//正体积的和
else if(tmp<0)
sneg-=tmp;//负体积的和
else
ans+=arr[i].a;//体积为0直接算贡献
}
memset(dp1,0x8f,sizeof(dp1));
memset(dp2,0x8f,sizeof(dp2));
dp1[0]=dp2[0]=0;
for(int i=1; i<=n; i++)//正
{
if(arr[i].wei<=0)
continue;
for(int j=spos; j>=arr[i].wei; j--)
dp1[j]=max(dp1[j],dp1[j-arr[i].wei]+arr[i].a);
}
for(int i=1; i<=n; i++)//负
{
if(arr[i].wei>=0)
continue;
for(int j=sneg; j>=-arr[i].wei; j--)
dp2[j]=max(dp2[j],dp2[j+arr[i].wei]+arr[i].a);
}
int rel=0;
for(int i=1; i<=min(sneg,spos); i++)//选择两个体积绝对值相等的最大值
{
if(dp1[i]>0&&dp2[i]>0)
rel=max(rel,dp1[i]+dp2[i]);
}
ans+=rel;
if(ans==0)
printf("-1\n");
else
printf("%d\n",ans);
return 0;
}

CF#214 C. Dima and Salad 01背包变形的更多相关文章

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

  3. CodeForces - 366C Dima and Salad (01背包)

    题意:n件东西,有属性a和属性b.要选取若干件东西,使得\(\frac{\sum a_j}{\sum b_j} = k\).在这个条件下,问\(\sum a_j\)最大是多少. 分析:可以将其转化为0 ...

  4. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  5. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  6. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  8. Dima and Salad(完全背包)

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

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

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

随机推荐

  1. 使用GML的八方向自动寻路

    使用GML的八方向自动寻路 本教程适合无基础人员使用. 提示 本教程中仅使用了最简单的方法,并且有一些错误和不规范之处.请谅解一下,在评论区提出,我会修改.古人曰"教学相长",希望 ...

  2. PHP函数:fwrite

    fwrite()  - 写入文件(可安全用于二进制文件) 说明: fwrite ( resource $handle , string $string [, int $length ] ) : int ...

  3. HBase BucketAllocatorException 异常剖析

    近日,观察到HBase集群出现如下WARN日志: 2020-04-18 16:17:03,081 WARN [regionserver/xxx-BucketCacheWriter-1] bucket. ...

  4. mac上搭建mysql环境配置和Navicat连接mysql

    mac上搭建mysql环境配置 1.下载mysql for mac: https://downloads.mysql.com/archives/community/ 注意:mysql版本要和你的MAC ...

  5. JS:document.documentElement对象的

    document.documentElement.clientWidth 获取浏览器窗口文档显示区域的宽度,不包括滚动条. document.documentElement.clientHeight ...

  6. Vulnhub-dpwwn-01靶机过关记录

    靶机地址:172.16.1.192 Kali 目录扫描 查看info.php 端口扫描 开放3306,尝试弱密码或爆破mysql. 账户为root,密码为空,成功登陆. 查看数据库:再查看ssh表 查 ...

  7. HDFS一些基本操作方法

    启动hadoop cd /usr/local/hadoop ./sbin/start-dfs.sh 在浏览器中打开localhost:50070 找到 进入  操作 1)新建文件夹      在根目录 ...

  8. Linux 字符串处理函数

    1 strchr 函数原型:extern char *strchr(char *str,char character) 参数说明:str为一个字符串的指针,character为一个待查找字符.     ...

  9. [Qt]执行cmd命令

    要加 /c 参数 QProcess p; p.start("cmd", QStringList()<<"/c"<<"ping ...

  10. 透彻理解C++11新特性:右值引用、std::move、std::forward

    目录 浅拷贝.深拷贝 左值.右值 右值引用类型 强转右值 std::move 重新审视右值引用 右值引用类型和右值的关系 函数参数传递 函数返还值传递 万能引用 引用折叠 完美转发 std::forw ...