ACM集训之排位赛二:思维/深搜题 (USACO 2018 February Contest, Bronze Hoofball)

版权声明:本文为博主原创文章,转载请注明出自CSDN Authur_gyc https://blog.csdn.net/WHY995987477/article/details/88583672

USACO 2018 February Contest, Bronze Hoofball
http://exam.upc.edu.cn/problemset.php?search=2018
5934: Hoofball
时间限制: 1 Sec 内存限制: 128 MB
题目描述
In preparation for the upcoming hoofball tournament, Farmer John is drilling his N cows (conveniently numbered 1…N, where 1≤N≤100) in passing the ball. The cows are all standing along a very long line on one side of the barn, with cow ii standing xi units away from the barn (1≤xi≤1000). Each cow is standing at a distinct location.
At the begiNing of the drill, Farmer John will pass several balls to different cows. When cow i receives a ball, either from Farmer John or from another cow, she will pass the ball to the cow nearest her (and if multiple cows are the same distance from her, she will pass the ball to the cow farthest to the left among these). So that all cows get at least a little bit of practice passing, Farmer John wants to make sure that every cow will hold a ball at least once. Help him figure out the minimum number of balls he needs to distribute initially to ensure this can happen, assuming he hands the balls to an appropriate initial set of cows.
输入
The first line of input contains N. The second line contains N space-separated integers, where the ith integer is xi.
输出
Please output the minimum number of balls Farmer John must initially pass to the cows, so that every cow can hold a ball at least once.
样例输入
5
7 1 3 11 4
样例输出
2
提示
In the above example, Farmer John should pass a ball to the cow at x=1 and pass a ball to the cow at x=11. The cow at x=1 will pass her ball to the cow at x=3, after which this ball will oscillate between the cow at x=3 and the cow at x=4. The cow at x=11 will pass her ball to the cow at x=7, who will pass the ball to the cow at x=4, after which this ball will also cycle between the cow at x=3 and the cow at x=4. In this way, all cows will be passed a ball at least once (possibly by Farmer John, possibly by another cow).

It can be seen that there is no single cow to whom Farmer John could initially pass a ball so that every cow would eventually be passed a ball.

题意

有很多个人排在一条水平直线上,他们在踢球,拿到球的会踢给离他最近的那个,如果旁边的两个人距离一样,那么他会踢给这两个人中左边的那个,问至少要多少个球,才能让每个人都踢上一遍。

思路

对每个人都进行深搜,对路径进行标记(后面搜的人能够覆盖前面搜过留下的路径),所有人搜完后,看一下一共有多少个不同的标记,标记的个数就是所需要的球的个数。
搜索时要注意端点的判断,不要走出界了。
(我的很多队友用了区间划分做 ,通过对距离的判断,能够确定球只能在哪个区间踢动,看有多少个区间就能确定需要多少个球了)

反思

这道题不难,写它不是不会,而是做题的时候把题意理解得不一样,题目中写到
(and if multiple cows are the same distance from her, she will pass the ball to the cow farthest to the left among these).
以为这个是指踢球踢给所有的牛里面最左边的那只,其实是踢给距离相等的牛中,左边的那只。
题意理解错特别伤,英语有待提高。

代码

#include<cstdio>
#include<cstdlib>
#include<string> 
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
int a[105];//位置 
int dis[105][5];//1是左,2是右 
int vis[105];//标记 
int ans[105];
int cnt=1; 
int dfs(int i)
{
	if(vis[i]==cnt)
		return 0;
	vis[i]=cnt;
	if(dis[i][1]==0) //如果是端点,走对的那条 
		dfs(i+1);
	else if(dis[i][2]==0)
		dfs(i-1);
	else if(dis[i][1]!=0&&dis[i][2]!=0)//如果不是端点,继续搜 
	{
		if(dis[i][1]<dis[i][2])
			dfs(i-1);
		else if(dis[i][1]>dis[i][2])
			dfs(i+1);
		else//两边一样近 ,给左边 
			dfs(i-1);
	}
	return 0;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+n+1);//从左到右排
	dis[1][2]=a[2]-a[1];
	dis[n][1]=a[n]-a[n-1];
	for(int i=2;i<n;i++){
		dis[i][1]=a[i]-a[i-1];
		dis[i][2]=a[i+1]-a[i];
	}
	for(int i=1;i<=n;i++){
		if(vis[i]==0)
			dfs(i);
		cnt++; 
	}	
	for(int i=1;i<=n;i++)
		ans[vis[i]]=1;
	cnt=0;
	for(int i=1;i<=105;i++)
		if(ans[i]==1)
			cnt++;
	printf("%d\n",cnt);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/WHY995987477/article/details/88583672
今日推荐