# Andrew the Ant

1、蚂蚁可以视为“对穿而过”。

2、蚂蚁的相对位置永远不会改变，即最左边的蚂蚁永远在第二左边蚂蚁的左边...

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

struct data{
int x;
char c;
};
bool cmp(data x, data y){
return x. x < y. x;
}
int main(){
int l, a;
while(cin >> l >> a){
int t = -999;
int mov[100000];
int ans1, ans2;
int count = 0;
data ant[100000];
for(int i = 0; i < a; i ++){
cin >> ant[i]. x >> ant[i]. c;
if(ant[i]. c == 'L')
t = max(t, ant[i]. x);
else
t = max(t, l - ant[i]. x);
}
sort(ant, ant + a, cmp);     //将蚂蚁按坐标排序，为移动后与其匹配做准备
for(int i = 0; i < a; i ++){
if(ant[i]. c == 'L')
mov[i] = ant[i]. x - t;
else
mov[i] = ant[i]. x + t;
}
sort(mov, mov + a);				//本题的灵魂之处，既显示了“对穿而过”，又彰显了“相对位置不变”
for(int i = 0; i < a; i ++){
if(mov[i] == 0 || mov[i] == l){
if(count == 1){
ans2 = ant[i]. x;
count ++;
break;
}
if(count == 0){
ans1 = ant[i]. x;
count ++;
}
}
}
if(count == 1){
printf("The last ant will fall down in %d seconds - started at %d.\n", t, ans1);
}
if(count == 2){
printf("The last ant will fall down in %d seconds - started at %d and %d.\n", t, ans1, ans2);
}
}
return 0;
}

• 评论

• 下一篇
• 上一篇