2014 nyoj990)

Source: Internet
Author: User


There are n ants on a 100 cm long straight rod. Their headers are left and right. Each ant can only climb forward along the pole at a speed of 1 cm/second. When the two ants meet, they both turn and crawl in the opposite direction. One of these ants caught a cold. In addition, when we meet other ant financial, we will send a cold to the ant Financial. Please calculate how many ants caught a cold when all the ants fell off the pole.



[Data format]

Enter an integer n (1 <n <50) in the first line, indicating the total number of ants.


The next row is n integers (-100 <Xi <100) separated by spaces. The absolute value of Xi indicates the distance from the left endpoint of the pole. A positive value indicates that the header is directed to the right, and a negative value indicates that the header is directed to the left. The data does not contain 0 values or the two ants occupy the same position. Among them, the first data indicates that ant caught a cold.


An integer is required to indicate the number of ants caught a cold.


For example, enter:

3

5-2 8


Program output:

1


For example, enter:

5

-10 8-20 12 25


Program output:

3


Resource conventions:

Peak memory consumption <256 M

CPU consumption <1000 ms


Solutions

First of all, it is the same to turn around after the collision of the two ants. You can think of it as turning around after the collision, and then the two ants exchange, but the ant does not affect the result.

So, if the first ant catches a cold and walks to the right, all the people who want to leave will be infected, and the infected ant will go to the left, then he will infect the left and right sides.

This is also the case when you go left.

So ans = + 1 (itself) on the left to the right to the left ).

Of course, there are special cases where the first infected person is walking to the right and the right is walking to the right. If the speed is the same, it will not infect others, so ans = 1. The opposite is true.

This is my personal idea. please correct me.

Nyoj question: http://acm.nyist.net/JudgeOnline/problem.php? Pid = 1, 990

My code:

# Include <cstdio >#include <algorithm> using namespace std; struct Node {int x; int dis; int num ;}; Node a [100]; int comp (Node a1, node a2) {if (a1.x! = A2.x) return a1.x <a2.x;} int main () {int n, x; while (~ Scanf ("% d", & n) {for (int I = 0; I <n; I ++) {scanf ("% d", & x ); a [I]. dis = x <0? -1:1; a [I]. x = abs (x); a [I]. num = I + 1;} sort (a, a + n, comp); // sort int tmp = 0, left = 0, right = 0, ans = 0; for (int I = 0; I <n; I ++) // {if (a [I] on the left to the right. num = 1) {tmp = I; break;} if (a [I]. dis = 1) left ++;} for (int I = tmp + 1; I <n; I ++) // {if (a [I]. dis =-1) right ++;} if (a [tmp]. dis = 1 & right = 0 | a [tmp]. dis =-1 & left = 0) ans = 1; else ans = left + right + 1; printf ("% d \ n", ans );} return 0 ;}


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.