二分查找

思路很简单,将所待查找的数与中间的数进行比较,小了在左区间差,大了在右区间差,主要是要如何判断是否使用二分,如何使用
要求:排序;范围;
题目:
A - River Hopscotch
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance before he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.

    Input


  Line 1: Three space-separated integers: 
   L, 
   N, and 
   M 

Lines 2…
N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.

    Output
    
  
   Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing 
   M rocks
  

    Sample Input
    25 5 2
    2
    14
    11
    21
    17

    Sample Output
    4

    Hint
    
  
   Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2.    After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
//题意可知最短距离有范围【0,l];可采用二分法,去试距离,用sum记录剩下的石头
//若sum>(n-m)则说明距离取小了;
// 若sum<(n-m)则说明距离取大了;
//若sum=(n-m),因为所求的为最大短距离。所以可以进一步加大距离尝试
//总的需满足两个条件
//1:求在当且仅当剩下的石头为n-m的情况下的距离di
//2:求最大的di
#include<iostream>
#include<algorithm>
using namespace std;
int a[60000];
int l,n,m;
bool judge(int mid)
{
 int sum=0;//记录剩下的石头
 int s=0;
 for(int i=1;i<=n;i++)
 {
  	if((a[i]-s)<mid)
  	 continue;
  	else
  	{
  	 sum++;s=a[i];
	 if((l-a[i])<=mid) break;//最后一块石头 直接上岸  	
	}
 }
 return sum>=(n-m);	
}
int main()
{
 cin>>l>>n>>m;
 for(int i=1;i<=n;i++)
  cin>>a[i];
 a[0]=0;a[n+1]=l;
 sort(a,a+n+2);//第一步,先排序
 int start=0,last=l;
 int di;
 while(start<=last)//第二步,开始二分 =?  14 15 14  ;15 15 15
 {
  int mid=(start+last)/2;	
  if(judge(mid))//>=成立,往大的数目寻找 ,并记录di 
  {
  	di=mid;
	start=mid+1; 
  }	
  else
  {
  	last=mid-1;
  }
 }
 cout<<di;
 return 0; 
} 

该题为常见的求最值,此时查找判断需加上=的情况
B - Drying
It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.
Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.
There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.
Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).
The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.

    Input
    
  
   The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).
  

    Output
    
  
   Output a single integer — the minimal possible number of minutes required to dry all clothes.
  

    Sample Input
    sample input #1
    3
    2 3 9
    5

    sample input #2
    3
    2 3 6
    5

    Sample Output
    sample output #1
    3

    sample output #2
    2
//最短时间即【0,ai(max)],可采用二分 
//两个条件:1:烘干所以衣服;2:最短时间 ti 
//用time[i]记录每件衣服所需时间,将其与 ti进行比对,将多出的时间ans所需时间与t作比较 
//若 t>ans,t太多,记录t,往少的寻找            
//    <   ,   少,往大的寻找 
//    =   ,  往少的寻找 
//-------------------x------------------------
//注意!!!!烘干的时候不在另外减少
//t要另外算 
//***********二分简单,最难的是时间的计算 X + Y = t  ;X * k + Y >= Ai; X>=(Ai-t)/(k-1)。
//***********注意数据范围!!long long int 
//***********注意 long long int time[110100]和 	scanf("%I64d",time+i);的区别;//不标准,编译器不支持 
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
long long int time[110100];
long long int n,k;
int judge(long int mid)
{
 long long int ans=0;//所需时间 
 for(int i=0;i<n;i++)
  if(time[i]>mid)
  {
  	ans+=((time[i]-mid)/(k-1));
	if(((time[i]-mid)%(k-1)))
	 ans++;
  }
 return ans<=mid;
} 
int main()
{
 long long int maxn=0;
 cin>>n;
 for(int i=0;i<n;i++)
 {
  	scanf("%I64d",time+i);/////**********
  	if(time[i]>maxn) maxn=time[i];
 }	
 cin>>k;
 if(k==1)
 {
 	cout<<maxn;
 	return 0;
 } 
 long long int star=0,late=maxn; //
 long long int ti;
 while(star<=late) 
 {
   long long int mid=(star+late)/2;
   if(judge(mid))//>= 
   {
   	//cout<<"*";
   	ti=mid;
   	late=mid-1;
   }
   else
   {
   	//cout<<"&";
   	star=mid+1;
   }
 }
 //cout<<sizeof(time)<<endl; 
 cout<<ti;
 return 0;
} 

该题说明了正确分析题意的重要性!?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值