Talent Show

时间限制: 1 Sec  内存限制: 128 MB
提交: 19  解决: 3
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Farmer John is bringing his N cows, conveniently numbered 1…N, to the county fair, to compete in the annual bovine talent show! His ith cow has a weight wiwi and talent level ti, both integers.
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.

输入

The first line of input contains N (1≤N≤250) and W (1≤W≤1000). The next N lines each describe a cow using two integers wi (1≤wi≤106) and ti (1≤ti≤103).

输出

Please determine the largest possible ratio of total talent over total weight Farmer John can achieve using a group of cows of total weight at least W. If your answer is A, please print out the floor of 1000A in order to keep the output integer-valued (the floor operation discards any fractional part by rounding down to an integer, if the number in question is not already an integer).

样例输入

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.

题意:有n件物品,重量分别是wi,价值是ti,要求在满足wi的和大于等于W的情况下,能得到的最大  ti和/wi 和的比,结果向下取整,然后乘1000倍。

观察ti和/wi和,此处用sum代替sigma.设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倍,用longlong计算,解决了向下取整的问题。

代码实现:

/*
Look at the star
Look at the shine for U
*/ #include<bits/stdc++.h>
#define sl(x) scanf("%lld",&x)
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
ll t[N],w[N];
ll n,W,dp[N];bool judge(int mid)
{int i,j,k;memset(dp,-INF,sizeof(dp));dp[0] = 0;bool flag = false;for(i = 1;i <= n;i++){for(j = W;j >= 0;j--){int tt = min(W,j+w[i]);          //容量无限大,只需关注W dp[tt] = max(dp[tt],dp[j]+t[i]-w[i]*mid);/*   if(j+w[i] >= W && dp[j]+t[i]-w[i]*mid >= 0 && dp[j+w[i]] >= 0)flag = true;*/}}if(dp[W] >= 0)return true;return false;
}int main()
{ll i,j,k;sl(n);sl(W);for(i = 1;i <= n;i++){sl(w[i]);sl(t[i]);t[i] = t[i]*1000;  //被除数*1000,直接取整 }ll l = 0,r = 100005,mid;while(l <= r){mid = (l+r)/2;if(judge(mid))l = mid+1;elser = mid-1;}printf("%lld\n",r);
}

Talent Show (二分+DP)相关推荐

  1. 【bzoj1044】[HAOI2008]木棍分割 二分+dp

    题目描述 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长度最小, 并且 ...

  2. hdu1025 Constructing Roads In JGShining#39;s Kingdom(二分+dp)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem ...

  3. bzoj 1863 二分+dp check

    思路:二分之后用dp去check就好啦. #include<bits/stdc++.h> #define LL long long #define fi first #define se ...

  4. hihocoder #1362 : 修补木桶(二分+dp)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 一只木桶能盛多少水,并不取决于桶壁上最高的那块木板,而恰恰取决于桶壁上最短的那块. 已知一个木桶的桶壁由N块木板组成,第i ...

  5. hdu 4495(hash+二分+dp)

    题意:求一个n*m的矩阵里面的最大的一个对称等腰直角三角形,三角形的腰必须平行于矩阵的边,n,m<=500. 解题思路:腰平行于矩阵的边,其实也就是做四个方向,首先找到每一个点的最长腰f[i][ ...

  6. poj 2397(二分+dp)

    题意:蜘蛛侠可以上下跳,问如何跳可以使得达到的最高高度尽可能小.并且最后的位置要在地面. 解题思路:首先记录跳的所有高度和sum,蜘蛛侠所能达到的最高高度肯定在1-sum之间(否则不可能做到),标准的 ...

  7. uvalive5983(二分+dp)

    题意: 给出一个n*m的图,从左上角出发,去右下角,只能向下或者向右走,每个格子有权值,问如何安排你在左上角时的权值,使得到达右下角时权值大于1. 思路: 题中给出10m时限,于是我就二分答案+dp验 ...

  8. NYOJ 229 工程 二分+dp检验

    工程 时间限制:1000 ms  |  内存限制:65535 KB 描述 有n个工人做两个工程A和B,每个工程都被分为相同的m份,给你第i个工人做A中的一份需要的时间Xi秒,和做B中的一份所需时间Yi ...

  9. BZOJ1044: [HAOI2008]木棍分割 (二分 + DP)

    题意:n根木棍依次连在一起 最多切m个端点 使得最长的一段最小 在保证最长的最小的情况下 有多少种不同的切法 题解:第一问傻子都知道二分 第二问想了一会不会做 但其实就是很简单的dp dp[i][j] ...

  10. [排序][二分][dp]JZOJ 2747 捡金子

    Description 从前有一个迷宫,迷宫的外形就像一棵带根树,每个结点(除了叶子结点外)恰好有K个儿子. 一开始你在根结点,根结点的K个儿子分别标记为'A', 'B', 'C'-.,而结点'A'的 ...

最新文章

  1. http各种中的各种timeout
  2. HTML、JSP、Servlet中的相对路径和绝对路径 页面跳转问题
  3. gazebo仿真环境加载多个机器人
  4. win10 远程桌面无法连接报错
  5. 信息系统项目管理师:第4章:项目整体管理与变更管理(2)
  6. Maven实战(二)——POM重构之增还是删
  7. CRC16循环冗余校验 RTU-MODBUS标准 Linux C
  8. Spring中都用到了哪些设计模式?
  9. 【conda】解决 An HTTP error occurred when trying to retrieve this URL.
  10. Pycharm连接Mysql问题: Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezon
  11. 【编译原理】理解BNF
  12. 如鹏网.Net三层架构 第四章代码生成器
  13. c++ vector基本函数、排序、查找用法
  14. 科学计算机感叹号,电脑网络黄三角感叹号怎么解决
  15. 机器学习入门 笔记(二) 机器学习基础概念
  16. python复利计算_如何让Python复利计算器给出正确的答案
  17. pymysql数据库的水果店销售系统之管理员端1.0
  18. 腿麻脚软警告,体虚者勿入!
  19. OPPO Pad 2 参数 OPPOPad 2评测怎么样
  20. 数说故事香氛品类分析及行业新趋势、消费者需求洞察

热门文章

  1. 雅虎正式任命硅谷投资人韦伯为公司董事会主席
  2. 出身名门的至尊旗舰,心系天下三星W2017图赏
  3. MJD时间的计算与UTC的转换
  4. 计算机相关专业是什么专业,计算机科学与技术专业属于什么大类 属于哪个学科...
  5. 直接拿来用!Facebook移动开源项目大合集
  6. ARMv8 AArch64异常处理机制概览
  7. Qt中实时调取摄像头(含代码)
  8. 用Ogre固定渲染管线实现火炬之光X-RAY效果 .
  9. soho一族快速赚钱发财的36个秘籍
  10. 游泳池知识库题_文学知识库(选择题)