01_ Ant Cold (fifth session Blue Bridge preliminary undergraduate B group 8th question Nyoj 990)

Source: Internet
Author: User

Source: Fifth session of the Blue Bridge preliminary Undergraduate B group 8th question

problem Description: there are N (1<n<50) ants on a straight pole (100cm) on a fixed length (each ant's starting point is different), they all crawl to the left or right at the same speed (1cm/s), and when two ants meet, They will turn around at the same time and crawl in the opposite direction. Of these ants, 1 ants have a cold. And when they meet with other ants, they will infect the ant with the cold. Please calculate how many ants have a cold when all the ants are climbing away from the pole.

Problem Analysis: 1. When two ants meet, they crawl in the opposite direction, which is equivalent to crawling after the two ants meet (except for the ants).

       2. If the cold ant (STA) climbs to the left, he will infect all the ants on his left to the right (the quantity is recorded as the Ieft), and if left!=0, the ants will infect the left side of the STA, so the ants (quantity is right)

       3. If the cold ant (STA) climbs to the right, he will infect all the ants on his right to the left (the number is in right!=0), in which case the ants will infect the left side of the STA and crawl to the right.

Example Links:http://acm.nyist.net/JudgeOnline/problem.php?pid=990

Examples:

Ant Flu time limit:MS | Memory limit:65535 KB Difficulty:2

Describe
there are n ants on a slender straight pole 100 centimeters long. They have their heads on the left, some facing to the right. Each ant can only climb along the pole, at a speed of 1 centimeters per second. When two ants meet, they will turn around at the same time and crawl in the opposite direction. Of these ants, 1 ants have a cold. And when they meet with other ants, they will infect the ant with the cold. Please calculate how many ants have a cold when all the ants are climbing away from the pole.
Input
The first line enters an integer n (1 < n < 50), which represents the total number of ants.
The next line is n a space-separated integer XI ( -100 < Xi <), the absolute value of Xi, indicating the distance the ant leaves the left end of the pole. A positive value indicates a head to the right, a negative value indicates a head to the left, no 0 values appear in the data, and no two ants occupy the same position. Among them, the first data represents the ants catching a cold.
Output
Requires an output of 1 integers, indicating the number of last cold ants.
Sample input
35-2 85-10 8-20) 12 25
Sample output
13

Code implementation:

1#include"stdio.h"2#include"string.h"3#include"stdlib.h"4 5 #defineN 556 intA[n];7 8 structnode9 {Ten     intx; One     intDi; A } Point[n],sta; -  - intcmpConst void*a,Const void*b) the { -     structNode *c = (Node *) A; -     structNode *d = (Node *) b; -     returnC->x-d->x; + } -  +  A intMain () at { -     intI,n; -     intans; -     intLeft,right; -      while(SCANF ("%d", &n)! =EOF) -     { inAns =1;//own cold!  -          for(i=0; i<n; i++) to         { +scanf"%d",&a[i]); -             if(a[i]<0) thepoint[i].x =-a[i],point[i]. Di =-1; *             Else $point[i].x = A[i], point[i]. Di =1;Panax Notoginseng         } -sta.x = point[0].x; theSta. Di = point[0]. Di; +Qsort (Point,n,sizeof(point[0]), CMP); Aleft = right =0; the          for(i=0; point[i].x<sta.x; i++)//left: Statistics on the number of ants crawling to the right of the cold ant sta +         { -             if(Point[i]. di==1) $left++; $         } -          for(i++; i<n; i++)//Right: Count the number of ants crawling to the left of the cold ant sta. -         { the             if(Point[i]. di==-1) -right++;Wuyi         } the         if(Sta.) di==-1&& left!=0)//Cold ant sta climbs to the left -Ans + = left +Right ; Wu         Else if(Sta.) di==1&& right!=0)//Cold ant sta climbs right -Ans + = left +Right ; Aboutprintf"%d\n", ans); $     } -     return 0; -}



01_ Ant Cold (fifth session Blue Bridge preliminary undergraduate B group 8th question Nyoj 990)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.