B. Hoofball(dfs)

时间:2019-02-19
本文章向大家介绍B. Hoofball(dfs),主要包括B. Hoofball(dfs)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

B. Hoofball

time limit per test

5 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

In preparation for the upcoming hoofball tournament, Farmer John is drilling his NN cows (conveniently numbered 1…N1…N, where 1≤N≤1001≤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 xixi units away from the barn (1≤xi≤10001≤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 ii 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 NN. The second line contains NN space-separated integers, where the iith integer is xixi.

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.

Example

input

Copy

5
7 1 3 11 4

output

Copy

2

Note

In the above example, Farmer John should pass a ball to the cow at x=1x=1 and pass a ball to the cow at x=11x=11. The cow at x=1x=1 will pass her ball to the cow at x=3x=3, after which this ball will oscillate between the cow at x=3x=3 and the cow at x=4x=4. The cow at x=11x=11will pass her ball to the cow at x=7x=7, who will pass the ball to the cow at x=4x=4, after which this ball will also cycle between the cow at x=3x=3 and the cow at x=4x=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个人站在一条直线上,给出距原点的距离。每个人向距离他最近的人传球,若距离相等则向左,每人至少摸一次球,问最少需要给几个球。

题目分析:

dfs 。分情况,球在第一个人,第n个人,2-n-1人之间。

dfs +标记同一颜色。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int n,a[N],vis[N],ans[N];int cnt=1;
void dfs(int pos)
{
	if(vis[pos]==cnt)return;
	vis[pos]=cnt;
	if(pos==1)dfs(2);
	else if(pos==n)dfs(n-1);
	else if(pos!=1&&pos!=n){
		if(a[pos]-a[pos-1]<=a[pos+1]-a[pos])dfs(pos-1);
		else dfs(pos+1);
	}
}
int main()
{
	cin>>n;for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++){
		if(!vis[i])
			dfs(i);
		cnt++;
	}
	cnt=0;
	for(int i=1;i<=n;i++)ans[vis[i]]=1;
	for(int i=1;i<=n;i++)if(ans[i])cnt++;
	cout<<cnt;
}