millope

ACM作业

A

题目

在一根细木杆上,有一些速度相同蚂蚁,它们有的往左走,有的往右走,木杆很细,只允许一只蚂蚁通过,所以当两只蚂蚁碰头的时候,它们会掉头继续前进,直到走出边界,掉下木杆。
已知木杆的长度和每只蚂蚁的名字、位置和初始方向,问依次掉下木杆的蚂蚁花费的时间以及它的名字。

题解

code

#include <bits/stdc++.h> 

using namespace std; 
const int maxn = 105; 

struct ant{
	int pos; 
	char name[11]; 
}a[maxn]; 
struct node{
	int time; 
	char dir[5]; 
}b[maxn]; 

inline bool a_cmp(const ant &x, const ant &y) {
	return x.pos < y.pos; 
}

inline bool b_cmp(const node &x, const node &y) {
	return x.time < y.time; 
}

int main() {
	int T, n, L; 
	scanf("%d", &T); 
	for (int k = 1; k <= T; ++k) {
		scanf("%d%d", &n, &L); 
		for (int i = 1; i <= n; ++i) {
			scanf("%s%d%s", a[i].name, &a[i].pos, b[i].dir); 
			b[i].time = (b[i].dir[0] == 'L') ? a[i].pos : L - a[i].pos; 
		}

		sort(a + 1, a + n + 1, a_cmp); 
		sort(b + 1, b + n + 1, b_cmp); 
   
		printf("Case #%d:\n", k); 
		int x = 1, y = n; 
		for (int i = 1; i <= n; ++i) {
			if (b[i].dir[0] == 'L') printf("%d %s\n", b[i].time, a[x++].name); 
			else printf("%d %s\n", b[i].time, a[y--].name); 
		}
	}
	return 0; 
}

posted on 2022-03-04 09:00  millope  阅读(117)  评论(0编辑  收藏  举报

导航