/*
Migrated from Lutece 300 木杆上的蚂蚁
Description
在一根细木杆上,有一些速度相同蚂蚁,它们有的往左走,有的往右走,木杆很细,只允许一只蚂蚁通过,所以当两只蚂蚁碰头的时候,它们会掉头继续前进,直到走出边界,掉下木杆。已知木杆的长度和每只蚂蚁的名字、位置和初始方向,问依次掉下木杆的蚂蚁花费的时间以及它的名字。Input
输入包含多组测试数据。第一行包含一个整数
T(T≤20),代表测试数据组数。
每组测试数据的第一行包含两个整数
N, L,表示有N只蚂蚁(N≤100),木杆长度为L(L≤1000)。假设蚂蚁每秒前进一个单位距离,掉头转向的时间忽略不计。
以下N行,每行依次为蚂蚁的名字(长度不超过10,仅由英文字母组成),初始位置
p(0<p<L,整数,表示蚂蚁离木杆最左端的距离),初始方向(一个字符,L表示向左,R表示向右),以单个空格分隔,数据保证初始不会有两只蚂蚁在同一个位置。
Output
对于第k组测试数据,首先输出一行为Case #k:。
然后输出N行,给出依次掉下木杆的蚂蚁花费的时间以及它的名字,以单个空格分隔。 (按照掉下木杆的先后顺序输出,数据保证不会有两支蚂蚁同时掉下木杆)。Samples
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
//记录蚂蚁的名字和位置
struct Ant{std::string name;int pos;bool operator<(const Ant& other) const{return pos<other.pos;}
};
//记录蚂蚁掉落的时间和掉落的方向
struct DropTimeAndDropSide{int time;bool fallsLeft;bool operator<(const DropTimeAndDropSide& other) const{return time<other.time;}
};
void solve(int caseNo){int antCount,rodLength;std::cin>>antCount>>rodLength;//rod,竿std::vector<Ant> ants(antCount);std::vector<DropTimeAndDropSide> times(antCount);for(int i = 0;i<antCount;++i){std::cin>>ants[i].name;int pos;char dir;std::cin>>pos>>dir; ants[i].pos=pos;if(dir=='L'){times[i].time = pos;times[i].fallsLeft=true;}else{times[i].time = rodLength-pos;times[i].fallsLeft=false;}}//无论怎么移动, 蚂蚁之间的相对位置是不会变化的, 下一个掉落的一直都是在最左边或者最右边的蚂蚁//将蚂蚁按照位置排序, 以获取最左边和最右边的蚂蚁std::sort(ants.begin(),ants.end());//位置在最左边的蚂蚁掉落的时间取决于方向向左的蚂蚁中位置最左边的蚂蚁//->--<-------<;--<>------<--;<---->--<----; // a b//a会掉落,但是掉落的时间取决于b的位置//位置在最右边的蚂蚁掉落的时间取决于方向向右的蚂蚁中位置最右边的蚂蚁//下一个掉落的蚂蚁是左边的蚂蚁还是右边的蚂蚁取决于最左边向左的蚂蚁和最右边向右的蚂蚁哪个离端点近std::sort(times.begin(),times.end());int leftIndex = 0,rightIndex = antCount-1;std::cout<<"Case #"<<caseNo<<": "<<std::endl;for(const auto& time : times){if(time.fallsLeft){std::cout<<time.time<<' '<<ants[leftIndex++].name<<std::endl;}else{std::cout<<time.time<<' '<<ants[rightIndex--].name<<std::endl;}}
}
int main(){int T;std::cin>>T;for(int i = 1;i<=T;++i){solve(i);}
}