zoukankan      html  css  js  c++  java
  • [简单思维题]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头牛站在x轴上不同位置,现挑出一些牛给它们每牛一只球,得到球的牛将会将球传递给别的牛,传递的规则是:传给离自己最近的牛,若多头牛同时离自己最近,传给最左边那头牛。问至少需要多少球去分配给这些牛,使得所有牛都能被传到球。
    思路:首先要知道,被传递的球最终总是会落入某一个cycle中。那么所有这些农夫分配下去的球,最终都会分别落入属于自己的cycle中。那我们就要看,在解最优的情况下(用最少数量的球使所有牛都能被传到球),每一个cycle分别“吸收”了几个球,其总和就是答案。
    对于一个cycle:若邻近它的左边那头牛的投球目标是这个cycle中的左牛,且邻近它的右边那头牛的投球目标是这个cycle中的右牛,那这个cycle将总共“吸收”2只球;
                              否则,这个cycle将总共“吸收”1只球;
    AC代码:
    #include<cstdio>
    #include<algorithm>
    #define ll long long
    using namespace std;
    
    ll a[110];
    ll nxt[110];
    bool flag[110];
    int vis[110];
    
    int main(){
      ll n;
      scanf("%lld",&n);
      for(ll i=1;i<=n;i++) scanf("%lld",&a[i]);
      if(n==1||n==2) {printf("1
    "); return 0;}
      sort(a+1,a+1+n);
      for(ll i=1;i<=n;i++){
        if(i==1) nxt[i]=2;
        else if(i==n) nxt[i]=n-1;
        else {
            if(a[i]-a[i-1]<=a[i+1]-a[i]) nxt[i]=i-1;
            else if(a[i+1]-a[i]<a[i]-a[i-1]) nxt[i]=i+1;
        }
      }
      for(ll i=1;i<=n;i++) if(nxt[nxt[i]]==i) flag[i]=1;//一个cycle中的左右牛flag标记为1
      ll ans=0;
      for(ll i=1;i<=n-1;i++){
        if(flag[i]&&flag[i+1]&&!vis[i]&&!vis[i+1]){//若i与i+1构成一个cycle
            if(i>=2&&nxt[i-1]==i&&i+1<=n-1&&nxt[i+2]==i+1) ans+=2;
            else ans+=1;
            vis[i]=vis[i+1]=1;
        }
      }
      printf("%lld
    ",ans);
      return 0;
    }
     
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    CR开发笔记-1工作前的准备
    CR开发笔记-2基础飞机的搭建以及测试代码
    c++还有一个小时考试
    c# winform 打印笔记
    aspcms部署
    c#复习笔记 继承
    [转]IoC框架
    Cinder-2 窗口的创建过程
    Cinder-1 TinderBox
    admin模板
  • 原文地址:https://www.cnblogs.com/lllxq/p/9020749.html
Copyright © 2011-2022 走看看