CF#214 C. Dima and Salad 01背包变形
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背包变形的更多相关文章
- 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 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 - 366C Dima and Salad (01背包)
题意:n件东西,有属性a和属性b.要选取若干件东西,使得\(\frac{\sum a_j}{\sum b_j} = k\).在这个条件下,问\(\sum a_j\)最大是多少. 分析:可以将其转化为0 ...
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- HDU 2639 Bone Collector II(01背包变形【第K大最优解】)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 【01背包变形】Robberies HDU 2955
http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...
- Dima and Salad(完全背包)
Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 366C Dima and Salad:背包dp
题目链接:http://codeforces.com/problemset/problem/366/C 题意: 有n个物品,每个物品有两个属性a[i]和b[i]. 给定k,让你选出一些物品,使得 ∑ ...
随机推荐
- google protobuf c++ 反射
const Descriptor *desc = DescriptorPool::generated_pool()->FindMessageTypeByName(msg_name); asser ...
- [整理]svn常见问题汇总
1.’.’ is not a working copy.Can’t open file‘.svn/entries’: 系统找不到指定的路径.解答:原因是输入的访问路径不正确,如svn://192.16 ...
- python-Django收集主机信息json格式
Control: from django.conf.urls import patterns, include, url from django.contrib import admin admin. ...
- TensorFlow的图像NCHW与NHWC
import tensorflow as tf x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] with tf.Session() as sess: a = t ...
- Python自学从入门到就业之函数基础(小白必看)
函数介绍 <1>什么是函数 请看如下代码: print(" _ooOoo_ ") print(" o8888888o ") print(" ...
- SweetAlert - 演示6种不同的提示框效果
http://www.sucaihuo.com/js/190.html http://www.cnblogs.com/beiz/p/5238124.html
- 2019-2020-1 20199308《Linux内核原理与分析》第七周作业
<Linux内核分析> 第六章 进程的描述和进程的创建 6.1 进程的描述 操作系统内核实现操作系统的三大管理功能: 进程管理(进程)-核心 内存管理(虚拟内存) 文件系统(文件) 为了管 ...
- Python(4)
lst = [1,2,4,8,16,32,64,128,256,512,1024,32769,65536,4294967296] # 输出 { 1:[1,2,3,8], 2:[16,32,64], 3 ...
- Testing for the End of a File (Windows 的异步 IO)
The ReadFile function checks for the end-of-file condition (EOF) differently for synchronous and asy ...
- 尤雨溪的vue怎么学,应该从vue-cli开始,为什么?
带手机验证码登陆, 带全套购物车系统 带数据库 前后端分离开发 带定位用户功能 数据库代码为本地制作好了 带支付宝支付系统 带django开发服务器接口教程 地址: https://www.dua ...