codeforces Hoofball

版权声明:博客作者为blue bear,严禁他人在未经作者同意下进行商用转载 https://blog.csdn.net/weixin_44354699/article/details/88378549

模拟 dfs

题面

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 i standing xi units away from the barn (1≤xi≤1000). Each cow is standing at a distinct location.

At the beginning 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.

Input
The first line of input contains N. The second line contains N space-separated integers, where the ith integer is xi.

Output
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.

input
5
7 1 3 11 4
output
2

Note
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.

题意

有N头牛在N个位置,每头牛的传球规则:

  1. 两边只有一边有牛,即端点处时,传给旁边的牛
  2. 两边牛距离不相等,传给距离近的那个
  3. 两边牛距离相等,传给左边那个也就是位置小的那个(比如3 4 5 ,4会把球传给3)
    问最少需要多少个球才能让每头牛都踢到球

分析

在比赛的时候想的是先给每头牛一个球,如果有球传给他,那么收了他的球,最后统计还有多少个球。于是想到用dfs来模拟。

代码

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
int vis[105],a[105],b[105],n;
void dfs(int pos,int flag){
	if(vis[pos] || (b[pos]==0 && flag==0) ) return;
	//if(a[pos]==7) printf("666\n");
	vis[pos]=1;
	if(flag) b[pos]=0;
	if(pos==0){
		dfs(1,1);
		return;
	}
	if(pos==n-1){
		dfs(n-2,1);
		return;
	}
	if(a[pos]-a[pos-1]<=a[pos+1]-a[pos]){
		dfs(pos-1,1);
	}else{
		dfs(pos+1,1);
	}
}
int main(){
	int i;
	scanf("%d",&n);
	for(i=0;i<n;i++) scanf("%d",&a[i]),b[i]=1;
	sort(a,a+n);
	for(i=n-1;i>=0;i--){
		memset(vis,0,sizeof(vis));
		dfs(i,0);
	}
	int ans=0;
	for(i=0;i<n;i++){
		//printf("%d ",b[i]);
		if(b[i]) ans++;
	}
	printf("%d\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44354699/article/details/88378549