codeforces B. Hoofball

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 N cows (conveniently numbered 1…N, where ≤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.

Example

input
5
7 1 3 11 4
output
4

Note
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头牛在x轴上不同位置,选一些牛给一只球,得到球的牛将会将球传给离自己最近的牛,若两头牛同时离自己最近,传给左边那头牛。至少需要多少球才能使所有牛都能摸过球。
没看出来是什么题型;
感觉上左右模拟扫一下就能过,把每头牛都给球扫一下,在一个区块就用相同标记;
不知道这种做法有没有问题;

代码

写得太丑,感觉多余部分太多

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define MS(X) memset(X,0,sizeof(X))
#define MSC(X) memset(X,-1,sizeof(X))
typedef long long LL;
using namespace std;
const int INF=1e6;
const int MINF=-INF;
int n,st[105],nxt[105],lnk[105];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&st[i]);
        lnk[i]=st[i];
    }
    sort(st+1,st+n+1);
    st[0]=MINF;st[n+1]=INF;
    lnk[2]=lnk[1];
    lnk[n-1]=lnk[n];
    for(int i=n;i>=1;i--){
        while(st[i]-st[i-1]<=st[i+1]-st[i]&&lnk[i-1]!=lnk[i]){
            lnk[i-1]=lnk[i];
            i--;
        }
        while(st[i]-st[i-1]>st[i+1]-st[i]&&lnk[i+1]!=lnk[i]){
            lnk[i+1]=lnk[i];
            i++;
        }
    }
    int cnt=1;
    for(int i=1;i<n;i++){
        if(lnk[i]!=lnk[i+1]) cnt++;
    }
    printf("%d\n",cnt);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值