USACO 2018 February BronzeP0024

Hoofball
题目描述:
,为了准备即将到来的蹄球锦标赛,Farmer John正在训练他的N头奶牛(方便起见,编号为1…N,其中1 \le N \le 1001≤N≤100)进行传球。这些奶牛在牛棚一侧沿直线排列,第i号奶牛位于距离牛棚x_ix
​i
​​ 的地方(1 \le x_i \le10001≤x
​i
​​ ≤1000)。每头奶牛都在不同的位置上。
在训练开始的时候,Farmer John会将若干个球传给不同的奶牛。当第i号奶牛接到球时,无论是从Farmer John或是从另一头奶牛传来的,她会将球传给最近的奶牛(如果有多头奶牛与她距离相同,她会传给其中距左边最远的那头奶牛)。为了使所有奶牛都有机会练习到传球,Farmer John想要确保每头奶牛都持球至少一次。帮助他求出为了达到这一目的他开始时至少要传出的球的数量。假设他在开始的时候能将球传给最适当的一组奶牛。
输入格式:
输入的第一行包含N。第二行包含N个用空格分隔的整数,其中第i个整数为x_ix
​i。
输出格式:
输出Farmer John开始的时候最少需要传出的球的数量,使得所有奶牛至少持球一次。
样例:
输入样例1:
5
7 1 3 11 4
输出样例1:
2

数据范围与提示:
在上面的样例中,Farmer John应该将球传给位于x=1的奶牛和位于x=11的奶牛。位于x=1的奶牛会将她的球传给位于x=3的奶牛,在此之后这个球会在位于x=3的奶牛和位于x=4的奶牛之间来回传递。位于x=11的奶牛会将她的球传给位于x=7的奶牛,然后球会被传给位于x=4的奶牛,在此之后这个球也会在位于x=3的奶牛和位于x=4的奶牛之间来回传递。这样的话,所有的奶牛都会至少一次接到球(可能从Farmer John,也可能从另一头奶牛)。

可以看出,不存在这样一头奶牛,Farmer John可以将球传给她之后所有奶牛最终都能被传到球。
题意:
牛站成一列,John给牛发球,每头牛都会传给离自己最近的牛,要求每头牛至少要踢一次球
求最少要发多少球
思路:
先要算出距离,转化为左传还是右传,如果一头牛的右边是向右传球的牛 or 左边是向左传球的牛 则不用给这头牛发球,特殊情况:…左传 右传 左传 右传… 虽然最中间的两头牛符合上面的条件但是没人给他们传球,需要发一个球。

#include<bits/stdc++.h>
using namespace std;
int a[105];
int main()
{
    int i,n,mi=0,ma=0;
    cin>>n;
    for(i=1;i<=n;i++)
    cin>>a[i];
    for(i=n;i>=2;i--){
	    if(a[i]!=-1&&a[i]!=0) {
	    if(a[i-1]==-1) {a[i-1]=a[i]-1;}
	    else {
		    if(a[i-1]!=a[i]-1)
			    {cout<<-1<<endl;return 0;}

	}
}
}
    if(a[1]==-1) a[1]=0;
    if(a[1]!=0){
	    cout<<-1<<endl;return 0;
}
    for(i=1;i<=n;i++){
	    if(a[i]==0) {mi++;ma++;}
	    else{
		    if(a[i]==-1) ma++;
	}
}
    cout<<mi<<' '<<ma<<endl;
    return 0;
}

题目原文:
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值