Dima and Salad(完全背包)】的更多相关文章

Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to ha…
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to…
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的背包里放,可以取得的…
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to…
题意: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]\)的最大值就是答案,注意不…
题目链接:http://codeforces.com/problemset/problem/366/C 题意: 有n个物品,每个物品有两个属性a[i]和b[i]. 给定k,让你选出一些物品,使得 ∑ a[i] / ∑ b[i] = k. 问你选出物品的 ∑ a[i]最大是多少. 题解: 将原式变形: ∑ a[i] - k * ∑ b[i] = 0 可以看做有一个容积为0的箱子,每个物品的价值为a[i],体积为a[i] - k*b[i],问你最大总价值. 这就变成了01背包. 但是体积a[i] -…
C. Dima and Salad   Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something. Dima and Seryozha have n fruits in the fridge. Each fruit has…
codeforces-214(Div. 2)-C. Dima and Salad 题意:有不同的沙拉,对应不同的颜值和卡路里,现在要求取出总颜值尽可能高的沙拉,同时要满足 解法:首先要把除法变成乘法,就是每次把读进来的b[ i ] 乘以 K; 因为对于a [ i ] - b[ i ] * k有两种不同的可能(大于0和小于0),可以放在一个以25000为中心的,大小为50000的dp数组里: 比如对于样例1: input 3 2 10 8 1 2 7 1 output 18这里我们缩小一下数据 我…
Dima and Salad 题意:一共有n种水果,每种水果都有一个ai, bi,现求一个最大的ai总和,使得ai之和/对应的bi之和的值等于K. 题解:将bi转换成偏移量,只要偏移到起点位置,就代表左右偏移抵消了,就满足题意了,注意一点的是这个跑法和01背包的恰好消耗是一样的初始化方法,将起点设为0,其他位置设为-INF,这样状态只能从起点转移出去,然后再从外面存在的点转移出去. 代码: #include<iostream> #include<cstring> using nam…
<题目链接> 题目大意: 在一个水果篮里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出来一些做一个水果沙拉, 并且要求他的水果沙拉的美味度是卡路里的k倍,问小明是否可以做出这么一个水果沙拉,若不能输出-1,否则输出复合要求的最大的美味值. 解题思路: 题目的限制条件为物品的价值总和与卡路里的比值要为K,这个控制,于是我们将卡路里总和乘到的右边,然后移项,可得(a1-k*b1)+(a2-k*b2)+……+(an-k*bn)=0.因此就将 (ai-k*bi…