BNU 26579 Andrew the Ant 蚂蚁问题
Time Limit: 4000msMemory Limit: 65536KB64-bit integer IO format: %lld Java class name: Main
Prev Submit Status Statistics Discuss Next
Font Size:
Type:
Andrew the Ant is fascinated by the behavior of his friends. Thousands of them are marching their paths on and on. They can build highly organized ant-hills. Sometimes, however, they act a little bit stupidly.
Recently, Andrew watched his fellow ants marching on top of a long piece of wood. He noticed their behavioral pattern is very 易做图: Each ant walks slowly forward with a constant speed of 1 cm per second. Whenever it meets another ant, both of them only touch with their antennae and immediately turn around and walk the opposite direction. If an ant comes to the end of the wood, it falls down and does not affect other ants anymore.
The picture above shows an example of moving ants in time 0 s. In one second, the ants E and A meet at position 2 and change their directions. The ant A then meets B in the next 1.5 seconds. At the same time (2.5 seconds after the start), the ants C and D will meet too. All four of them change their directions. In the next 0.5 second (time 3 s), the first ant (E) falls down off the left end, etc.
Your task is to simulate the movement of ants. For simplicity, suppose that the ants have zero size (although the picture could suggest something else).
Input
The input consists of several scenarios. Each scenario starts with a line containing two integer numbers L and A, separated by a space. L is the length of the wood in cms (1 ≤ L ≤ 99 999), and A is the number of ants at the beginning of the simulation (1 ≤ A ≤ L + 1).
Then there are A lines, each containing a positive integer Xi, one space, and an uppercase letter. The number (0 ≤ Xi ≤ L) specifies the position of the i-th ant and the letter its initial direction: either “L” for left (towards zero) or “R” for right. No two ants will start at the same position.
Output
For each scenario, you should print a single line containing the text “The last ant will fall down in T seconds - started at P.”, where T is the exact time when the last ant (or two) reaches the end of the wood, and P is the position where that particular ant has originally started in time 0. If two last ants fall down at the same time, print “started at P and Q”, indicating both of their positions, P <Q.
Sample Input
90000 1
0 R
10 1
0 L
14 5
3 L
6 L
13 L
8 R
1 R
Sample Output
The last ant will fall down in 90000 seconds - started at 0.
The last ant will fall down in 0 seconds - started at 0.
The last ant will fall down in 13 seconds - started at 6 and 8.
Hint
(The last sample input scenario corresponds to the picture.)
Source
CTU Open Contest 2012
题意:给出a个蚂蚁的位置和其正在行走的方向,若相碰则都转向,若到边缘则掉下去,每秒钟走一格,
问最后一只(或两只)蚂蚁是什么时候掉下去的,其初始位置在什么地方;
思路:(参考同学的 )
(以刘汝佳的某题的解题思路为背景)
蚂蚁看作质点,如图,A左走,E右走,显然A和E碰后E左走,而E的左走就相当于A向左走的一个延续,同理A相当于E的延续,这样,n秒后各个蚂蚁的位置就相当于每个蚂蚁不转向走n秒后的位置,但这时仅仅只是所有位置确定了,但该位置我蚂蚁并不确定;又由于相碰后转向,实际上每个蚂蚁的相对顺序没变,如图,五只蚂蚁,初始位置分别为1 3 6 8 13,则3秒后五只蚂蚁的位置为4 0 3 5 10(即 0 3 4 5 10),最后要确定这五个位置分别是哪几个蚂蚁时,就根据原来的相对顺序就知道E在0,A在3,B在4,D在5,C在10;若要找最后一个掉下去的时间,则要找到最右边那个向左走的蚂蚁和最左边那个向右走的蚂蚁,比较一下就行了;
代码是我的
[cpp]
#include<stdio.h>
#include<algorithm>
using namespace std;
struct haha
{
int s;
char flag;
}ant[100000];
int aa[100000];
int cmp(struct haha a,struct haha b)
{
return a.s>b.s;
}
int main()
{
int i,len,n,time,left,right,cnt,n1,n2;
while(scanf("%d %d",&len,&n)!=EOF)
{
right=len;
left=0;
for(i=0;i<n;i++)
{
scanf("%d %c",&ant[i].s,&ant[i].flag);
}
sort(ant,ant+n,cmp);
for(i=n-1;i>=0;i--)
if(ant[i].flag=='R')//向右跑
{
right=ant[i].s;
break;
}
for(i=0;i<n;i++)
if(ant[i].flag=='L')//向左跑
{
left=ant[i].s;
break;
}
time=len-right>left?len-right:left;
for(i=0;i<n;i++)
if(ant[i].flag=='R')
aa[i]=ant[i].s+time;
else aa[i]=ant[i].s-time;
sort(aa,aa+n);
cnt=0;
for(i=0;i<n;i++)
{
if(aa[i]==0||aa[i]==len)
{
cnt++;
if(cnt==1) n1=ant[n-1-i].s;
if(cnt==2) n2=ant[n-1-i].s;
补充:软件开发 , C++ ,
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊