64-bit integer IO format: %lld Java class name: Main
Piotr's Ants
Time Limit: 2 seconds
"One thing is for certain: there is no stopping them; the ants will soon be here. And I, for one, welcome our new insect overlords." |
Piotr likes playing with ants. He has n of them on a horizontal pole L cm long. Each ant is facing either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it is facing and wants to calculate where the ants will end up 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 (measured in 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 two or more ants are at the same location, print "Turning" instead of "L" or "R" for their direction. If an 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 |
2 10 1 4 1 R 5 R 3 L 10 R 10 2 3 4 R 5 L 8 R | Case #1: 2 Turning 6 R 2 Turning Fell off Case #2: 3 L 6 R 10 R |
https://www.bnuoj.com/v3/problem_show.php?pid=19299
一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝又爬,速度为1厘米/秒.当两只蚂蚁相撞时,二者同时掉头(掉头时间忽略不计).给出每只蚂蚁的初始和朝向,计算T
秒之后每只蚂蚁的位置.
按输入顺序输出每只蚂蚁的位置和朝向(Turning表示正在碰撞).在第T秒之前已经掉下木棍的边缘的不算)输出Fell off.
从远处看就是一群密密麻麻的小黑点在移动 由于碰撞掉头时间不会耗时相当于两个点"对穿而过" 没有任何的影响
输入的时候不一定按顺序 按输入的输出
关键是要弄清楚最后的时刻 哪只蚂蚁是刚开始的哪只
#include<bits/stdc++.h>
using namespace std;
struct point
{
int h,d,w;
friend bool operator < (point a,point b)
{
if(a.h==b.h)
return a.d<b.d;
else return a.h<b.h;
}
} aa[10010],bb[10010];
int cmp(point a,point b)
{
return a.w<b.w;
}
int main()
{
int kk=0,i,tt,l,t,n;
char gg;
scanf("%d",&tt);
while(tt--)
{
printf("Case #%d:\n",++kk);
scanf("%d%d%d",&l,&t,&n);
for(i=0; i<n; i++)
{
scanf("%d %c",&aa[i].h,&gg);
aa[i].w=i;//记录位置
if(gg=='R')
aa[i].d=1;
else aa[i].d=-1;
}
sort(aa,aa+n);//排到在木棍上的位置
for(i=0; i<n; i++)
{
bb[i].h=aa[i].h+aa[i].d*t;
bb[i].d=aa[i].d;
}
sort(bb,bb+n);
bb[n].h=-1;
for(i=0;i<n;i++)
if(bb[i].h==bb[i+1].h||bb[i].h==bb[i-1].h)
bb[i].d=2;
for(i=0; i<n; i++)
{
aa[i].h=bb[i].h;
aa[i].d=bb[i].d;
}
sort(aa,aa+n,cmp);//按原来位置回来
for(i=0; i<n; i++)
{
if(aa[i].h<0||aa[i].h>l)
printf("Fell off\n");
else if(aa[i].d==2)
printf("%d Turning\n",aa[i].h);
else
{
if(aa[i].d==-1)
printf("%d L\n",aa[i].h);
else printf("%d R\n",aa[i].h);
}
}
printf("\n");
}
return 0;
}