题面
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
5
7 1 3 11 4
output
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.
题目大意
有 头牛,他们分布在一条直线上,每头牛都有一个坐标 ,然后给它们几个球,它们会往举例它们最短的牛传,若是距离一样,则往左边传。问至少要几个球,才能使得它们每头牛都传过一次球。
题目分析
距离是一定的,所以传球的方向也是一定的。而且最左边的牛一定往右传,最右边的牛往左传。我们不妨先预处理出它们传球的方向,观察它们的规律。
我们不难发现,当有这样右左
的结构的时候,球会陷进死循环。这时我们就需要一个球。
我们以此为起点,继续观察:当右左左……左
或者右右……右左
这样的结构的时候,也还是需要一颗球。
可是,当出现右右……右左左……左
的时候我们就需要两颗球了。因为肯定要有一个起点从端点开始,而当遇到右左
结构时陷入死循环,另一端就无法到达了。
而所有的牛的序列肯定由上面两个序列组合起来,我们只需要数一下有多少这样的结构,就可以知道至少需要几个球了。
代码
#include <cstdio>
#include <algorithm>
using namespace std;
int arr[105];
const bool l = 1, r = 0;
bool fx[105];
int main(int argc, char const *argv[]) {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);
sort(arr, arr + n);
fx[0] = r; fx[n - 1] = l;
for(int i = 1; i < n - 1; i++){
if(arr[i] - arr[i - 1] <= arr[i + 1] - arr[i])
fx[i] = l;
else
fx[i] = r;
}
int ans = 0;
bool flag = false;
int L = 0, R = 0;
// for(int i = 0; i < n; i++)
// printf("%c\n", fx[i] == l? 'l': 'r');
for(int i = 0; i < n; i++){
if(fx[i] == l){
L++;
flag = true;
}else{
if(flag){
if(R == 1 || L == 1)
ans++;
else
ans += 2;
L = 0; R = 0;
}
flag = false;
R++;
}
}
if(flag){
if(R == 1 || L == 1)
ans++;
else
ans += 2;
L = 0; R = 0;
}
printf("%d\n", ans);
return 0;
}