【贪心-二分】Atcoder——ABC144——Gluttony

前言

明明是一道很裸的“最大值最小”的二分题,考试时硬是没想到OTZ...

题目

Time Limit: 2 sec / Memory Limit: 1024 MB

Score: 500 points

Problem Statement

Takahashi will take part in an eating contest. Teams of N

members will compete in this contest, and Takahashi's team consists of N players numbered 1 through N from youngest to oldest. The consumption coefficient of Member i is Ai.

In the contest, N foods numbered 1 through N will be presented, and the difficulty of Food i is Fi. The details of the contest are as follows:

  • A team should assign one member to each food, and should not assign the same member to multiple foods.
  • It will take x×y seconds for a member to finish the food, where x is the consumption coefficient of the member and y is the difficulty of the dish.
  • The score of a team is the longest time it takes for an individual member to finish the food.

Before the contest, Takahashi's team decided to do some training. In one set of training, a member can reduce his/her consumption coefficient by 1, as long as it does not go below 0. However, for financial reasons, the N members can do at most Ksets of training in total.

What is the minimum possible score of the team, achieved by choosing the amounts of members' training and allocating the dishes optimally?

Constraints

  • All values in input are integers.
  • 1≤N≤2×10^5
  • 0≤K≤10^18
  • 1≤Ai≤10^6 (1≤i≤N)
  • 1≤Fi≤10^6 (1≤i≤N)

Input

Input is given from Standard Input in the following format:

 

Output

Print the minimum possible score of the team.


Sample Input 1

3 5
4 2 1
2 3 1

Sample Output 1

2

They can achieve the score of 2, as follows:

  • Member 1 does 4 sets of training and eats Food 2 in (4−4)×3=0seconds.
  • Member 2 does 1 set of training and eats Food 3 in (2−1)×1=1second.
  • Member 3 does 0 sets of training and eats Food 1 in (1−0)×2=2seconds.
  • They cannot achieve a score of less than 2, so the answer is 2

  • Sample Input 2

    3 8
    4 2 1
    2 3 1
    

    Sample Output 2

    0
    

    They can choose not to do exactly K sets of training.


    Sample Input 3

    11 14
    3 1 4 1 5 9 2 6 5 3 5
    8 9 7 9 3 2 3 8 4 6 2
    

    Sample Output 3

    12

题目大意

有n个人和n份食物,每个人吃东西的系数为ai,食物被吃的系数为fi,一个人吃一份食物,所花时间为 ai * fi,求所有人各吃一份食物,所有人中吃食物所花的最大时间最小

分析

首先【贪心】地想:人的系数小的去吃食物系数大的,反之亦然,所以可以先把a[  ]按从小到大排序,f[  ]从大到小排序

【最大值最小】,典型的二分的题型

从0到a范围的最大值二分所花的最大的时间x,检查能否使每个ai*fi都小于等于x

分析完感觉还是挺水的


自己有另一种想法,只是还未实现:

直接在a数组上二分

代码

#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN=2e5;
ll a[MAXN+5],b[MAXN+5],f[MAXN+5];
ll n,k,tot,ans;
bool cmp(ll a,ll b)
{
	return a>b;
}
bool Check(ll x)
{
	for(int i=1;i<=n;i++)
		b[i]=x/f[i];//使a[i]小于等于x达到的上界 
	ll res=0;
	for(int i=1;i<=n;i++)
		res+=max(0ll,a[i]-b[i]);
	return res<=k;
}
ll BS()
{
	ll l=0,r=(ll)1e12;
	while(l+1<=r)
	{
		ll mid=(l+r)>>1; 
		if(Check(mid))
			r=mid;
		else
			l=mid+1;
	}
	return r;
}
int main()
{
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		tot+=a[i];
	}
	for(int i=1;i<=n;i++)
		scanf("%lld",&f[i]);
	if(tot<=k)//个人值可被减完 
	{
		printf("0\n");
		return 0;
	}
	sort(a+1,a+n+1);
	sort(f+1,f+n+1,cmp);
	printf("%lld\n",BS());
	return 0;
}

 

你可能感兴趣的:(二分,贪心)