Talent Show
6349: Talent Show
时间限制: 1 Sec 内存限制: 128 MB
提交: 106 解决: 40
[提交] [状态] [讨论版] [命题人:admin]
题目描述
Upon arrival, Farmer John is quite surprised by the new rules for this year's talent show:
(i) A group of cows of total weight at least W must be entered into the show (in order to ensure strong teams of cows are competing, not just strong individuals), and
(ii) The group with the largest ratio of total talent to total weight shall win.
FJ observes that all of his cows together have weight at least W, so he should be able to enter a team satisfying (i). Help him determine the optimal ratio of talent to weight he can achieve for any such team.
输入
输出
样例输入
3 15
20 21
10 11
30 31
样例输出
1066
提示
In this example, the best talent-to-weight ratio overall would be to use just the single cow with talent 11 and weight 10, but since we need at least 15 units of weight, the optimal solution ends up being to use this cow plus the cow with talent 21 and weight 20. This gives a talent-to-weight ratio of (11+21)/(10+20) = 32/30 = 1.0666666..., which when multiplied by 1000 and floored gives 1066.
观察ti和wi和.设sum(ti)/sum(wi) = x.则有sum(ti) = sum(wi)*x. 进一步得出sum(ti)-sum(wi)*x = 0.展开后利用加法交换律得到sum(ti-wi*x) = 0. 此时发现ti,wi可以枚举,只留下x不确定,然后结果要求是0。考虑二分x,若加起来的和>0,说明x的值小了,若加起来的和<0,说明x大了,然后考虑到和的问题。
求和的问题明显可以看作是0-1背包问题,即dp[i]表示重量为i的情况下,可获得的最大值。然后发现此时的背包容量可以是特别大,而没有具体值,将重量大于W之后的值都当作W看待。对于dp[W],若他大于等于0,说明mid小了,否则说明mid大了。
再者0-1背包的过程中,由于背包的容量没有上限,所以背包的权值可以是在0到W+wi,所以二维dp维护的过程中可以将j-wi变成j+wi。,然后再进行背包。
最后得到结果,为了方便计算,可以在开始的时候就将ti*1000,即被除数扩大1000倍,这样结果自然扩大了1000倍,用long long计算,解决了向下取整的问题。
注:二分时,if成立,则需要向下取整,即ans=mid;否则,ans=mid-1.
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define N 300
#define ll long long
#define inf 1LL<<60
int n,m,t[N],w[N];
ll dp[1200];
bool jud(int mid)
{
for(int i=1; i<=m; ++i)
{
dp[i]=-inf;
}
for(int i=1; i<=n; ++i)
{
ll v=t[i]-(ll)w[i]*mid;
for(int j=m; j>=0; --j)
{
if(dp[j]==-inf)
{
continue;
}
int jj=j+w[i];
if(jj>m)
{
jj=m;
}
dp[jj]=max(dp[jj],dp[j]+v);
}
}
return dp[m]>=0;
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1; i<=n; ++i)
{
scanf("%d %d",&w[i],&t[i]);
t[i]*=1000;
}
int l=0,r=1000000;
while(l<=r)
{
int mid=(l+r)/2;
if(jud(mid))
{
l=mid+1;
}
else
{
r=mid-1;
}
}
printf("%d\n",r);
return 0;
}
Talent Show的更多相关文章
- Backbone,Marionette,Talent学习笔记
具体以源码为准 Talent继承自Marionette继承自BackBone Region: 继承自Backbone.Event,show(view)会调用view.render(),然后$el.ap ...
- 1062 Talent and Virtue (25)
/* L (>=60), the lower bound of the qualified grades -- that is, only the ones whose grades of ta ...
- pat 1062. Talent and Virtue (25)
难得的一次ac 题目意思直接,方法就是对virtue talent得分进行判断其归属类型,用0 1 2 3 4 表示 不合格 sage noblemen foolmen foolmen 再对序列进行排 ...
- PAT-B 1015. 德才论(同PAT 1062. Talent and Virtue)
1. 在排序的过程中,注意边界的处理(小于.小于等于) 2. 对于B-level,这题是比較麻烦一些了. 源代码: #include <cstdio> #include <vecto ...
- 分数规划模板(洛谷P4377 [USACO18OPEN]Talent Show)(分数规划,二分答案,背包)
分数规划是这样一个东西: 给定若干元素,每个元素有两个属性值\(a_i,b_i\),在满足题目要求的某些限制下选择若干元素并求出\(\frac{\sum a}{\sum b}\)的最大值. 如果没有限 ...
- 1062.Talent and Virtue
About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...
- Cocos2d-X开发教程-捕鱼达人 Cocos2-x development tutorial - fishing talent
Cocos2d-X开发教程-捕鱼达人 Cocos2-x development tutorial - fishing talent 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱 ...
- BZOJ 5281--[Usaco2018 Open]Talent Show(分数规划&单调队列&DP)
5281: [Usaco2018 Open]Talent Show Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 79 Solved: 58[Sub ...
- 【BZOJ5281】Talent Show(分数规划)
[BZOJ5281]Talent Show(分数规划) 题面 BZOJ 洛谷 题解 二分答案直接就是裸的分数规划,直接跑背包判断是否可行即可. #include<iostream> #in ...
- 1062 Talent and Virtue (25 分)
1062 Talent and Virtue (25 分) About 900 years ago, a Chinese philosopher Sima Guang wrote a history ...
随机推荐
- Manacher's Algorithm 马拉车算法(最长回文串)
这个马拉车算法Manacher‘s Algorithm是用来查找一个字符串的最长回文子串的线性方法,由一个叫Manacher的人在1975年发明的,这个方法的最大贡献是在于将时间复杂度提升到了线性,这 ...
- window.onerror 捕捉所有的前端error
//[捕捉所有前端error] window.onerror = function (errormessage, url, line, column, error) { console.log(&qu ...
- JS 识别生日、性别、年龄
<script> function IdCard(UUserCard,num){ if(num==1){ //获取出生日期 birth=UUserCard.substring(6, 10) ...
- uoj#351. 新年的叶子(概率期望)
传送门 数学还是太差了,想了半天都没想出来 首先有一个定理,如果直径(这里考虑经过的点数)为奇数,所有直径有同一个中点,如果直径为偶数,所有直径有同一条最中间的边.这个可以用反证法,假设不成立的话直径 ...
- Java基础--环境配置、简介
一.环境配置 1.傻瓜式安装JDK,若提示安装JRE,将其置于JDK同一安装目录即可. 2.配置JAVA_HOME, 指向JDK的安装目录.比如 JAVA_HOME = %JDK安装目录% 3.配置 ...
- 调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错
调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错 在需要解析的类型类上加上声明 eg:
- 安卓下点击a标签不跳转;点击a标签在手机真机上会调出手机键盘的解决办法
安卓下点击a标签不跳转的解决办法,是不用href做跳转的链接,改为用JS控制跳转 <a href="javascript:;" ontouchstart="wind ...
- jquery.jscrollpane.js滚动速度设置
首先找到插件里面的这个函数,改变成下面的样子: function initMousewheel() { container.unbind(mwEvent).bind( mwEvent, functio ...
- [LOJ 2190] 「SHOI2014」信号增幅仪
[LOJ 2190] 「SHOI2014」信号增幅仪 链接 链接 题解 坐标系直到 \(x\) 轴与椭圆长轴平行 点的坐标变换用旋转公式就可以了 因为是椭圆,所以所有点横坐标除以 \(p\) 然后最小 ...
- Educational Codeforces Round 66 (Rated for Div. 2) A. From Hero to Zero
链接:https://codeforces.com/contest/1175/problem/A 题意: You are given an integer nn and an integer kk. ...