poj 3258 还是二分

265 篇文章 1 订阅
15 篇文章 0 订阅

River Hopscotch
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5637 Accepted: 2450

Description

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 distanceDi 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 toM rocks (0 ≤ MN).

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 ofM 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).

Source



这个题的主要问题是计算m的方法不够直接,当==m时的处理不再单调,贴一个错误的程序:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int l,n,m;
vector<int> rock;
int dis[50005]={0};

int main(){
	int i,j;
	int maxn,minn,ll,rr,mid,cnt,sum,tmp,all,res;
	scanf("%d%d%d",&l,&n,&m);
	
	rock.push_back(0);
	for(i=1;i<=n;i++){
		scanf("%d",&tmp);
		rock.push_back(tmp);
	}
	rock.push_back(l);


	sort(rock.begin(),rock.end());
	for(i=1;i<=n+1;i++)
		dis[i]=rock[i]-rock[i-1];

	
	
	rock[n+1]=l;
	dis[n+1]=rock[n+1]-rock[n];
	maxn=dis[1];
	minn=dis[1];
	all=0;
	for(i=1;i<=n+1;i++){
		if(maxn<dis[i])
			maxn=dis[i];
		if(minn>dis[i])
			minn=dis[i];
		all+=dis[i];
	}
	ll=minn;
	rr=all;
	while(ll<=rr){
		mid=(ll+rr)>>1;
		cnt=0;
		sum=0;
		for( i=1;i<=n+1;i++ ){
			sum+=dis[i];
			if(sum>=mid){   //>=mid可以被分为一组,mid最大可以取多少
				cnt++;
				sum=0;
			}
		}
		if(sum>0)
			cnt++;
		
		if( cnt>n+1-m )
			ll=mid+1;
		else if( cnt<n+1-m )
			rr=mid-1;
		else if( cnt==n+1-m ){

			sum=0;
			res=0x7fffffff;
			for( i=1;i<=n+1;i++ ){
				sum+=dis[i];
				if(sum>=mid){   //>=mid可以被分为一组,mid最大可以取多少
					if(res>sum)
						res=sum;
					sum=0;
				}
			}
			
			if(sum>0)
				res=sum;
			
			printf("%d\n",res);
			return 0;
		}
	}

	
	return 0;
}

使用数据:

25 5 3
2
14
11
21
17
0 2 11 14 17 21 25
   2   9   3   3   4   4
0 11 17 25
    11  6   8
就知道为什么wa了

AC的程序:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> 
#include<vector>
#include<algorithm>
using namespace std;
int l,n,m;
vector<int> rock;
int dis[50005]={0};

int main(){
	int i,j;
	int maxn,minn,ll,rr,mid,cnt,sum,tmp,all,res;
	scanf("%d%d%d",&l,&n,&m);
	
	rock.push_back(0);
	for(i=1;i<=n;i++){
		scanf("%d",&tmp);
		rock.push_back(tmp);
	}
	rock.push_back(l);


	sort(rock.begin(),rock.end());
	for(i=1;i<=n+1;i++)
		dis[i]=rock[i]-rock[i-1];

	
	
	rock[n+1]=l;
	dis[n+1]=rock[n+1]-rock[n];
	maxn=dis[1];
	minn=dis[1];
	all=0;
	for(i=1;i<=n+1;i++){
		if(maxn<dis[i])
			maxn=dis[i];
		if(minn>dis[i])
			minn=dis[i];
		all+=dis[i];
	}
	ll=minn;
	rr=l;
	while(ll<=rr){
		mid=(ll+rr)>>1;
		cnt=0;
		sum=0;
		for( i=1;i<=n+1;i++ ){
			sum+=dis[i];
			if( sum<=mid ){   
				cnt++;//移除石头的个数				
			}
			else
				sum=0;
		}

		if( cnt<=m )
			ll=mid+1;
		else
			rr=mid-1;
	}
	printf("%d\n",ll);

//	system("pause");
	return 0;
}

按照另一种理解始终没有办法AC,不知道该怎么处理,希望有大牛指教:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int l,n,m;
vector<int> rock;
int dis[50005]={0};

int main(){
	int i,j;
	int maxn,minn,ll,rr,mid,cnt,sum,tmp,all,res;
	scanf("%d%d%d",&l,&n,&m);
	
	rock.push_back(0);
	for(i=1;i<=n;i++){
		scanf("%d",&tmp);
		rock.push_back(tmp);
	}
	rock.push_back(l);


	sort(rock.begin(),rock.end());
	for(i=1;i<=n+1;i++)
		dis[i]=rock[i]-rock[i-1];

	
	
	rock[n+1]=l;
	dis[n+1]=rock[n+1]-rock[n];
	maxn=dis[1];
	minn=dis[1];
	all=0;
	for(i=1;i<=n+1;i++){
		if(maxn<dis[i])
			maxn=dis[i];
		if(minn>dis[i])
			minn=dis[i];
		all+=dis[i];
	}
	ll=minn;
	rr=all;
	while(ll<rr){
		mid=(ll+rr)>>1;
		cnt=0;
		sum=0;
		for( i=1;i<=n+1;i++ ){
			sum+=dis[i];
			if(sum>=mid){   //>=mid可以被分为一组,mid最大可以取多少
				cnt++;
				sum=0;
			}
		}
		//if(sum>0)
		//	cnt++;
		//一共分了cnt组,mid越小,cnt越大	
		
		if( cnt>n+1-m )
			ll=mid+1;
		else
			rr=mid-1;
		
	}
	
	printf("%d\n",mid);
	
	return 0;
}


终于找到哪有问题了,思路太别,把自己绕进入了,看wa的代码:


/*
终于想明白了,这种以mid为组线的分组方法本身的单调性就存在问题

因为最后始终可能存在 处在(0,mid)之间的一组,这和下限为mid是矛盾的

就算把处在(0,mid)之间的一组归并到倒数第二组,这样所有组的下限并不一定是mid,目标是找所有组的下限而不是找mid

或者干脆克服困难找到下界,但是这样相当于绕了很大的一圈!!!!!

但是找到下界了也不对

试一下
28 5 4
8
11
14
17
21
数据

只能说以后这样逻辑崩溃了一定得跳出来换个思路,不要钻牛角尖


这样多直接
int move(int a)
{ 
     int sum=0,i,pre;
     pre=0;
     for(i=1;i<=n+1;i++)
     {
         if(dis[i]-dis[pre]<a)
             sum++;
         else
             pre=i;
     }
     return sum;
}


思维定势真可怕,这绝对是受3273影响造成的
 
*/
 
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int l,n,m;
vector<int> rock;
int dis[50005]={0};
int g[50005]={0};


int main(){
	int i,j;
	int maxn,minn,ll,rr,mid,cnt,sum,tmp,all,res,gi;
	scanf("%d%d%d",&l,&n,&m);
	
	rock.push_back(0);
	for(i=1;i<=n;i++){
		scanf("%d",&tmp);
		rock.push_back(tmp);
	}
	rock.push_back(l);


	sort(rock.begin(),rock.end());
	for(i=1;i<=n+1;i++)
		dis[i]=rock[i]-rock[i-1];

	
	
	rock[n+1]=l;
	dis[n+1]=rock[n+1]-rock[n];
	maxn=dis[1];
	minn=dis[1];
	all=0;
	for(i=1;i<=n+1;i++){
		if(maxn<dis[i])
			maxn=dis[i];
		if(minn>dis[i])
			minn=dis[i];
		all+=dis[i];
	}
	ll=minn;
	rr=all;
	while(ll<rr){
		mid=(ll+rr)>>1;
		cnt=0;
		sum=0;
		for( i=1;i<=n+1;i++ ){
			sum+=dis[i];
			if(sum>=mid){   //>=mid可以被分为一组,mid最大可以取多少
				cnt++;
				sum=0;
			}
		}
		
		
		if( cnt>n+1-m )
			ll=mid+1;
		else if( cnt<n+1-m )
			rr=mid-1;
		else if( cnt==n+1-m ){
		//按照当前的mid分组,找下限

			sum=0;
			gi=0;
			res=0x7fffffff;						

			for( i=1;i<=n+1;i++ ){
				sum+=dis[i];
				if(sum>=mid){   //>=mid可以被分为一组,mid最大可以取多少
					g[gi++]=sum;
					sum=0;
				}
			}
			--gi;

			if(sum>0)
				g[gi]+=sum;

			for(i=0;i<=gi;i++)
				if(res>g[i])
					res=g[i];
			printf("%d\n",res);		
			return 0;
		}
		
	}
	
//	printf("%d\n",mid);
//	system("pause"); 
	return 0;
}

另外注意计算石头那一步:

			if( sum<=mid ){   
会导致计算石头的时候多一个,而

			if( sum<mid ){   
石头正好


<=的时候需要输出ll,而<需要输出rr,因此此时rr是一个死边界,这样容易理解一些:


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> 
#include<vector>
#include<algorithm>
using namespace std;
int l,n,m;
vector<int> rock;
int dis[50005]={0};

int main(){
	int i,j;
	int maxn,minn,ll,rr,mid,cnt,sum,tmp,all,res;
	scanf("%d%d%d",&l,&n,&m);
	
	rock.push_back(0);
	for(i=1;i<=n;i++){
		scanf("%d",&tmp);
		rock.push_back(tmp);
	}
	rock.push_back(l);


	sort(rock.begin(),rock.end());
	for(i=1;i<=n+1;i++)
		dis[i]=rock[i]-rock[i-1];

	
	
	rock[n+1]=l;
	dis[n+1]=rock[n+1]-rock[n];
	maxn=dis[1];
	minn=dis[1];
	all=0;
	for(i=1;i<=n+1;i++){
		if(maxn<dis[i])
			maxn=dis[i];
		if(minn>dis[i])
			minn=dis[i];
		all+=dis[i];
	}
	ll=minn;
	rr=l;
	while(ll<=rr){
		mid=(ll+rr)>>1;
		cnt=0;
		sum=0;
		for( i=1;i<=n+1;i++ ){
			sum+=dis[i];
			if( sum<mid ){   
				cnt++;//移除石头的个数				
			}
			else
				sum=0;
		}

		if( cnt<=m )
			ll=mid+1;
		else
			rr=mid-1;
	}
	printf("%d\n",rr);

//	system("pause");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值