5934: Hoofball
时间限制: 1 Sec 内存限制: 128 MB提交: 186 解决: 34
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
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.
输入
输出
样例输入
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
N头牛进行传球,最后总会在两头牛之间进入循环,找到每个循环,如果此循环的左边和右边都有外来的球,说明会有两个球进入此循环,否则只有一个(包括第一个点和最后一个点)
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
long long n, a[110], flag[110], vis[110], next[110];
memset(flag, 0, sizeof(flag));
memset(vis, 0, sizeof(vis));
scanf("%lld", &n);
for(int i=1;i<=n;i++)
scanf("%lld", &a[i]);
if(n==1 || n==2)
{
printf("1\n");
return 0;
}
sort(a+1, a+n+1);
for(int i=1;i<=n;i++)
{
if(i==1)
next[i]=2;
else if(i==n)
next[i]=n-1;
else
{
if(a[i]-a[i-1]<=a[i+1]-a[i])
next[i]=i-1;
else
next[i]=i+1;
}
}
for(int i=1;i<=n;i++)
if(next[next[i]]==i)
flag[i]=1;
long long ans=0;
for(int i=1;i<=n;i++)
if(flag[i] && flag[i+1] && !vis[i] && !vis[i+1])
{
if(i>1 && i<n && next[i-1]==i && next[i+2]==i+1)
ans+=2;
else
ans+=1;
vis[i]=vis[i+1]=1;
}
printf("%lld\n", ans);
return 0;
}
/**************************************************************
Problem: 5934
User: ldu_reserver201701
Language: C++
Result: 正确
Time:0 ms
Memory:1096 kb
****************************************************************/