中石油 Hoofball 5934

5934: Hoofball

时间限制: 1 Sec   内存限制: 128 MB
提交: 174   解决: 30
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

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.

来源

USACO 2018 February Contest, Bronze 


题意:

一群牛在一起踢球,他只踢给离他们最近的一头牛,这样就有可能一直在两头牛之间踢着玩,比如1 3 4 6,就在3-4之间 来回踢,贼六

分析:我们模拟出来这个踢球的过程,看看每个牛往哪儿踢,大约有三种情况吧

case1

1 2 3 4    这种情况下,假设从右往左传(因为距离一样往左传)4->3->2->1,很明显,这个时候给4一个球,他就能全部传完。开个vis数组,能收到球的vis[i]++,这样就得到vis[4]=0,vis[3]=1,vis[2]=1,vis[1]=1。

明显,vis[i]=0,我们的ans就要+1。

case 2

1 2 4 5 和以一种情况类似,存在两处vis=0,ans=2,没问题

case 3

1 3 4 10 11 20 24 40 100 ,仔细观察,你会发现10<->11之间,是一个小循环,所以再加一个nxt数组,如果nxt[ nxt[i] ]=i,那么可能就是小循环,再加点小条件,就可以完全判断出来

My ugly code

#include <bits/stdc++.h>

using namespace std;
int n,a[128],vis[128],nxt[128];

/*
http://exam.upc.edu.cn/problem.php?id=5934
9
1 3 4 10 11 20 24 40 100
3
*/

int main(){

    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);

        sort(a+1,a+1+n);
        a[n+1]=0x3f3f3f3f;
        a[0]=-0x3f3f3f3f;
        for(int i=1;i<=n;i++){
            if( a[i]-a[i-1] > a[i+1] - a[i] ){
                vis[i+1]++;
                nxt[i]=i+1;
            }
            else {
                vis[i-1]++;
                nxt[i]=i-1;
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            if(vis[i]==0) ans++;
            else if(vis[i]==1 && vis[ nxt[i] ] == 1 && nxt[ nxt[i] ]==i && i < nxt[i] ){

                ans++;
            }
        }
        printf("%d\n",ans);

    }


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值