中石油 5934 Problem I Hoofball

Hoofball

题目描述

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.

题意:在一条直线上有n头牛每个牛对应一个位置,现在扔给某一头牛一个球,这头牛会向距离它最近的另一头牛传球,如果左右两边距离相等,那么它会向左边的牛传球。问最少需要传给这n头牛多少个球,可以让所有的牛都能传至少一次球。

我想的是先找到所有的会来回传的两头牛,记下左边牛的位置,然后只需要看一下这两头牛左右两边是否都可以有球传进来,如果是那么至少需要两个球才能让这一部分的牛都传一次;否则只需要一个球;


就只有上面的这几种情况。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
int a[105],vis[105];
int main()
{
    memset(vis,0,sizeof(vis));
    int n,k=0,ans=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        a[i]=10000;
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    if(n==1||n==2){
        printf("1\n");
        return 0;
    }
    sort(a,a+n);
    for(int i=0;i<n-1;i++){
        if(i==0){
            if(a[i+1]-a[i]<=a[i+2]-a[i+1])
                vis[k++]=i;
        }
        else if(i==n-2){
            if(a[i+1]-a[i]<a[i]-a[i-1])
                vis[k++]=i;
        }
        else{
            if(a[i+1]-a[i]<=a[i+2]-a[i+1]&&a[i+1]-a[i]<a[i]-a[i-1])
                vis[k++]=i;
        }
    }
    for(int i=0;i<k;i++){
		int t=vis[i];
        if(t>0&&t<n-2){
            if((t-1==0||a[t]-a[t-1]<a[t-1]-a[t-2])&&(t+2==n-1||a[t+2]-a[t+1]<=a[t+3]-a[t+2]))
                ans+=2;
            else
                ans++;
        }
        else
            ans++;
    }
    printf("%d\n",ans);
    return 0;
}



猜你喜欢

转载自blog.csdn.net/sxh759151483/article/details/79767073