zoukankan      html  css  js  c++  java
  • 01_蚂蚁感冒(第五届蓝桥预赛本科B组第8题 nyoj 990)

    问题来源:第五届蓝桥预赛本科B组第8题 

    问题描述:有在一条定长(100cm)的直杆上有n(1<n<50)只蚂蚁(每只蚂蚁的起点都不一样),他们都以相同的速度(1cm/s)向左或者向右爬, 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。 这些蚂蚁中,有1只蚂蚁感冒了。并且在和其它蚂蚁碰面时,会把感冒传染给碰到的蚂蚁。 请你计算,当所有蚂蚁都爬离杆子时,有多少只蚂蚁患上了感冒。

    问题分析:1.两只蚂蚁碰面时,往相反方向爬行,等价于两只蚂蚁相遇后继续爬行(只不过换了蚂蚁)。

           2.如果感冒蚂蚁(sta)向左爬,他会感染他左边向右爬的所有蚂蚁(数量记为left),其中如果left!=0,这些蚂蚁又会感染sta右边的向左爬的所以蚂蚁(数量记为right)

           3.如果感冒蚂蚁(sta)向右爬,他会感染他右边向左爬的所有蚂蚁(数量记为right),其中如果right!=0,这些蚂蚁又会感染sta左边的向右爬的所以蚂蚁(数量记为left)

    例题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=990

    例题:

    蚂蚁感冒

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:2
    描述
      长100厘米的细长直杆子上有n只蚂蚁。它们的头有的朝左,有的朝右。 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒。 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。 这些蚂蚁中,有1只蚂蚁感冒了。并且在和其它蚂蚁碰面时,会把感冒传染给碰到的蚂蚁。 请你计算,当所有蚂蚁都爬离杆子时,有多少只蚂蚁患上了感冒。
    输入
      第一行输入一个整数n (1 < n < 50), 表示蚂蚁的总数。
      接着的一行是n个用空格分开的整数 Xi (-100 < Xi < 100), Xi的绝对值,表示蚂蚁离开杆子左边端点的距离。正值表示头朝右,负值表示头朝左,数据中不会出现0值,也不会出现两只蚂蚁占用同一位置。其中,第一个数据代表的蚂蚁感冒了。
    输出
      要求输出1个整数,表示最后感冒蚂蚁的数目。
    样例输入
    3
    5 -2 8
    5
    -10 8 -20 12 25
    样例输出
    1
    3

    代码实现:

     1 #include "stdio.h"
     2 #include "string.h"
     3 #include "stdlib.h"
     4 
     5 #define N 55
     6 int a[N];
     7 
     8 struct node
     9 {
    10     int x;
    11     int Di;
    12 } Point[N],sta;
    13 
    14 int cmp(const void *a,const void *b)
    15 {
    16     struct node *c = (node *)a;
    17     struct node *d = (node *)b;
    18     return c->x - d->x;
    19 }
    20 
    21 
    22 int main()
    23 {
    24     int i,n;
    25     int ans;
    26     int left,right;
    27     while(scanf("%d",&n)!=EOF)
    28     {
    29         ans = 1;  //自身感冒了!
    30         for(i=0; i<n; i++)
    31         {
    32             scanf("%d",&a[i]);
    33             if(a[i]<0)
    34                 Point[i].x = -a[i],Point[i].Di = -1;
    35             else
    36                 Point[i].x = a[i], Point[i].Di = 1;
    37         }
    38         sta.x = Point[0].x;
    39         sta.Di = Point[0].Di;
    40         qsort(Point,n,sizeof(Point[0]),cmp);
    41         left = right = 0;
    42         for(i=0; Point[i].x<sta.x; i++) //left:统计在感冒蚂蚁sta左边的向右爬的蚂蚁数
    43         {
    44             if(Point[i].Di==1)
    45                 left++;
    46         }
    47         for(i++; i<n; i++) //right:统计在感冒蚂蚁sta右边的向左爬的蚂蚁数
    48         {
    49             if(Point[i].Di==-1)
    50                 right++;
    51         }
    52         if(sta.Di==-1 && left!=0) //感冒蚂蚁sta向左爬
    53             ans += left + right;
    54         else if(sta.Di==1 && right!=0) //感冒蚂蚁sta向右爬
    55             ans += left + right;
    56         printf("%d
    ",ans);
    57     }
    58     return 0;
    59 }


  • 相关阅读:
    2013.4.15 Particle Swarm Optimization with Skyline Operator for Fast Cloudbased Web Service Composition
    Adaptive service composition in flexible processes
    2013.4.13 DomainSpecific Service Selection for Composite Services
    2013.4.14 Modeling and Algorithms for QoSAware Service Composition in VirtualizationBased Cloud Computing
    2013.5.29 Towards Networkaware Service Composition in the Cloud
    Efficient algorithms for Web services selection with endtoend QoS constraints
    SQL Server中常用的SQL语句
    接口限流自定义注解
    linux服务器生产环境搭建
    MVEL自定义函数重复掉用报错:duplicate function
  • 原文地址:https://www.cnblogs.com/ruo-yu/p/4393950.html
Copyright © 2011-2022 走看看