2014 Blue Bridge Cup--ants with a cold

Source: Internet
Author: User

Title: Ants Cold

There are n ants on the slender straight pole with a length of about two centimeters. 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.

"Data Format"

The first line enters an integer N (1 < n <), 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.

Requires an output of 1 integers, indicating the number of last cold ants.

For example, enter:

3

5-2 8

The program should output:

1

Again, for example, enter:

5

-10 8-20 12 25

The program should output:

3

Resource contract:

Peak memory consumption < 256M

CPU consumption < 1000ms

An ant collision can be seen through!! So this problem is not difficult, the analysis

1#include <iostream>2 usingStd::endl;3 usingstd::cout;4 usingstd::cin;5 intAbsintx)6 {7     if(x<0)8         return-x;9     returnx;Ten } One intMain () A { -     intN; -     intdata[ -]; the      while(Cin >>N) -     { -         //the number of cold ants -         intleft=0, right=0; +         //input Data -          for(intI=0; i<n; ++i) +         { ACIN >>Data[i]; at         } -          for(intI=1; i<n;++i) -         { -             //find the ant right and go left. -             if(data[i]<0&&abs (Data[i]) >abs (data[0])) -             { inleft++; -             } to             //find the ant left and go right. +             if(data[i]>0&&abs (Data[i]) <abs (data[0])) -             { theright++; *             } $         }Panax Notoginseng         //Judging whether it is a special case -         if((data[0]<0&&right==0)|| (data[0]>0&&left==0)) the{//See above Analysis +cout <<"1"<<Endl; A}Else the         { +cout << left+right+1<<Endl; -         } $     } $     return 0; -}

Original text reproduced in http://blog.csdn.net/computer_liuyun/article/details/23350077

2014 Blue Bridge Cup--ants with a cold

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.