@2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017) K:Kayaking Trip(二分+贪心)

版权声明:岂曰无衣,与子同袍 https://blog.csdn.net/sizaif/article/details/82227195

Kayaking Trip

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

题目描述

You are leading a kayaking trip with a mixed group of participants in the Stockholm archipelago, but as you are about to begin your final stretch back to the mainland you notice a storm on the horizon. You had better paddle as fast as you can to make sure you do not get trapped on one of the islands. Of course, you cannot leave anyone behind, so your speed will be determined by the slowest kayak. 
Time to start thinking; How should you distribute the participants among the kayaks to maximize your chance of reaching the mainland safely?
The kayaks are of different types and have different amounts of packing, so some are more easily paddled than others. This is captured by a speed factor c that you have already figured out for each kayak. The final speed v of a kayak, however, is also determined by the strengths s1 and s2 of the two people in the kayak, by the relation v = c(s1 + s2 ). In your group you have some beginners with a kayaking strength of sb , a number of normal participants with strength s n and some quite experienced strong kayakers with strength se .

输入

The first line of input contains three non-negative integers b, n, and e, denoting the number of beginners, normal  participants, and experienced kayakers, respectively. The total number of participants, b + n + e, will be even, at least 2, and no more than 100 000. This is followed by a line with three integers sb , sn , and se , giving the strengths of the corresponding participants (1 ≤ sb < sn < se ≤ 1 000). The third and final line contains m = (b+n+e)/2 integers c1 , . . . , cm(1 ≤ ci ≤ 100 000 for each i), each giving the speed factor of one kayak.

输出

Output a single integer, the maximum speed that the slowest kayak can get by distributing the participants two in each kayak.

样例输入

3 1 0
40 60 90
18 20

样例输出

1600

[思路]

求最小中最大,   所以要用 二分.

V = C(s1+s2) 

已知C 组合s1,s2   二分答案 V  ,然后 贪心 组合 (s1,s2) 判断 是否满足  (注意存在个数限制)

[代码]

#include <bits/stdc++.h>
#include <stdio.h>
#define rep(i,a,n) for(int i=1;i<=n;i++)
/*
* 
*  Author : siz
 */
using namespace std;
typedef long long ll;
const int maxn =1e5+10;
const int mod =1e9+7;
const double esp =1e-6;
int b,n,e;
int sb,sn,se;
int c[maxn];
int m;
struct node
{
	int b,n,e,sum;
	node(){}
	node(int bb,int nn,int ee,int Sum)
	{
		b = bb,n = nn,e = ee,sum = Sum;
	}
	friend bool operator < (const node a,const node b)
	{
		return a.sum < b.sum;
	}
}Node[20];
bool judge(int mid)
{
	int bb = b,nn = n,ee = e;
	rep(i,1,m)
	{
		double Ss = ( (mid*1.0)/(1.0*c[i]));	
		int cot = 0;
		if( bb >= 2)
			Node[++cot] = node{2,0,0,sb+sb};
		if( nn >= 2)
			Node[++cot] = node{0,2,0,sn+sn};	
		if( ee >= 2)
			Node[++cot] = node{0,0,2,se+se};
		if( bb&&nn )
			Node[++cot] = node{1,1,0,sb+sn};	
		if( bb&&ee )
			Node[++cot] = node{1,0,1,sb+se};	
		if( nn&&ee )
			Node[++cot] = node{0,1,1,sn+se};	
		sort(Node+1,Node+cot+1);
		int flag = false;
		rep(j,1,cot)
		{
			if( (Ss - (double)Node[j].sum) < esp)
			{
				flag = true;
				bb -= Node[j].b;
				nn -= Node[j].n;
				ee -= Node[j].e;
				break;
			}
		}
		if(!flag)
			return false;
	}
	return true;
}
int main()
{
	scanf("%d %d %d",&b,&n,&e);

	m = (b+n+e)/2;
	
	scanf("%d %d %d",&sb,&sn,&se);

	rep(i,1,m) scanf("%d",&c[i]);
	
	sort(c+1,c+m+1);
	
	int l = 1 , r = 1000000000;
	
	int ans  = 0 ;
	
	while( l <= r)
	{
		int mid = (l+r)>>1;
		if(judge(mid))
		{
			ans = mid;
			l = mid+1;
		}
		else
			r = mid -1;
	}
	//cout<<ans<<" "<<l<<" "<<r<<endl;
	printf("%d\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sizaif/article/details/82227195