Uva-10881-piotr ' s Ants

Source: Internet
Author: User
Tags array sort

Piotr ' s Ants
Time Limit:2 seconds

"One thing is to Certain:there is no stopping them;
The ants'll soon is here. And I, for one, welcome our
New insect overlords. "
Kent Brockman

piotr likes playing with ants. He has n of them on a horizontalpole L cm long. Each ant was facing either left or right and walksat a constant speed of 1 cm/s. When the ants bump into each other, the Theyboth turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it's Facingand wants to calculate where the ants would end u P T seconds from now.

input
The first line of input gives the Number of cases, N . N test Cases follow. Each one starts with a line containing 3 integers: L , T and n ( 0 <= N <= 10000) . The next n lines give the locations of the n ants (Measuredin cm from the left end of the Pole) and the direction they are facing (L or R).

output
For each test case, Output one Line containing ' case # x : ' followed by n lines describing the locations and directions of The n ants in the same format and order as in the input. If the or moreants is at the same location, print "Turning" instead of "L" or "R" fortheir direction. If the ant falls off the pole before T seconds,print "Fell off" for that Ant. Print an empty line after each test case.

Sample Input Sample Output
1 R5 R3 L10 R10 2 R5 L8 R
Case #1:2 Turning6 R2 Turningfell offcase #2:3 L6 R10 R


This question is really not simple ah, looks like the challenge also has this example, the first input order is not specific, so to numbering from left to right, the middle collision although can be regarded as cross, but the relative position is constant, the same as the initial order.

, and, in fact, understanding the function of the order array is very simple.

The first ant to start typing is from the left number of order[i], so we're behind the order array, which is in the relative order

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 10005;struct ant{int id;//input order int p;//position int d;//direction BOOL operator < (const Ant & A)    {return P < A.P;    }}before[n],after[n];int order[n];const char dir[][10] = {"L", "Turning", "R"};int main () {int TT;    CIN >> TT;        for (int ca = 1;ca <= tt;ca++) {int l,t,n;        scanf ("%d%d%d", &l,&t,&n);            for (int i = 0;i < n;i++) {int p,d;            char c;            scanf ("%d%c", &p,&c);            D = (c = = ' L '? -1:1);        Before[i] = (ant) {i,p,d};//learned after[i] = (ant) {0,p+t*d,d};        }//Calculate order array sort (before,before+n);        for (int i = 0;i < n;i++) Order[before[i].id] = i;        Calculates the final result sort (after,after+n); for (int i = 0;i <n-1;i++) if (AFTER[I].P = = AFTER[I+1].P) after[i].d = after[i+1].d = 0;        Output printf ("Case #%d:\n", CA);            for (int i = 0;i < n;i++) {int a = Order[i];            if (after[a].p <0 | | after[a].p>l) printf ("Fell off\n");        else printf ("%d%s\n", after[a].p,dir[after[a].d+1]);    } printf ("\ n"); } return 0;}


Uva-10881-piotr ' s Ants

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.