题目
-
Problem Description
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 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 N. The second line contains N space-separated integers, where the i-th 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
题意
牛站成一列,John给牛发球,每头牛都会传给离自己最近的牛,要求每头牛至少要踢一次球
求最少要发多少球
思路
先处理每头牛的距离,转化为左传还是右传,如果一头牛的右边是向右传球的牛 or 左边是向左传球的牛 则不用给这头牛发球,
不过有一种特殊情况:…左传 右传 左传 右传… 虽然最中间的两头牛符合上面的条件但是没人给他们传球,需要发一个球
代码
#include <bits/stdc++.h>
using namespace std;
int dis[105],vis[105];
int main() {
int N;
scanf("%d",&N);
for(int n=1; n<=N; n++)
scanf("%d",dis+n);
sort(dis+1,dis+1+N);
memset(vis,0,sizeof(vis));
vis[1]=2;//边界的两头牛
vis[N]=1;
int l,r;
for(int i=2; i<N; ++i) {
l=dis[i]-dis[i-1];
r=dis[i+1]-dis[i];
if( l<=r ) {
vis[i]=1;//1=左传
}
else vis[i]=2;//2=右传
}
int sum=0;
int i,t;
for(int i=1; i<=N;i++) {
if(vis[i-1]!=2 and vis[i]==2 and vis[i+1]==1 and vis[i+2]!=1){
sum++;//特殊情况
}
else if(vis[i-1]==2 or vis[i+1]==1){//不需要发球
}
else{
sum++;
}
}
printf("%d\n",sum);
return 0;
}