Talent Show (二分+DP)

问题 D: Talent Show

时间限制: 1 Sec  内存限制: 128 MB
提交: 112  解决: 43
[提交] [状态] [讨论版] [命题人: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倍。

//S代表求和

式子可以写成S(Wi+Wk+Wl.......+Wp) / S(Ti+Tk+Tl.........+Tp)=x

要求是就是x尽可能大   然后等式化成 S(Wi+Wk+Wl.......+Wp) - S(Ti+Tk+Tl.........+Tp) * x = 0

然后 Wi和Ti是一一对应的  有可以简化成S(Wi - Ti*x) = 0  这样就可以考虑 二分枚举x  dp一个最大值就行了

为了方便计算,可以在开始的时候就将ti*1000 ,解决了向下取整的问题。

#include<bits/stdc++.h>
using namespace std;
long long n,w;
long long N[300];
long long W[300];
long long dp[10005];
int check(long long x)
{
	memset(dp,-0x3f3f3f,sizeof(dp));
	dp[0]=0;
	long long T = dp[w];
	for(int i=0;i<n;i++)
	{
		for(int j=w;j>=0;j--)
		{
			long long temp=j+N[i];
			temp=min(temp,w);
			dp[temp]=max(dp[temp],dp[j]+W[i]-N[i]*x);
		}
	}
	return dp[w]>=0;
}
void twofen()
{
	long long l=0;
	long long r=1000000;
	while(l<=r)
	{
		long long mid=(l+r)>>1;
		if(check(mid))
			l=mid+1;
		else
			r=mid-1;
	}
	printf("%lld\n",l-1);
}
int main()
{
	scanf("%lld%lld",&n,&w);
	for(int i=0;i<n;i++)
	{
		scanf("%lld%lld",&N[i],&W[i]);
		W[i]=W[i]*1000;
	}
	twofen();
	return 0; 
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值