UVA 10881 - Piotr's Ants

题目链接:https://vjudge.net/problem/UVA-10881
题意:有很多只蚂蚁在一条直线上,每个蚂蚁移动速度都是1,并且有一个初始方向。
并且当相邻两个蚂蚁相撞时转向。现在问t时间后各个蚂蚁的位置。

这题的一个技巧就是把两只蚂蚁的相撞看作是两只蚂蚁交换穿过对方并且交换蚂蚁的编号。
最主要是要注意到
第一遍排序后 得到按位置升序排序的蚂蚁位置
不论之后这些蚂蚁怎么爬 他们的相对位置都是不变的啊!!!!
就是第1只蚂蚁一直都是在第1位 第2只一直都在第2位...!!!!
所以对最终全部蚂蚁的位置排序

第1个位置就是第1只蚂蚁的 第2个位置就是第2只蚂蚁的....


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
const int N = 1e4 + 5;
const int mod = 1e8;

struct Ant{
	int pos, dir, id;
	bool operator < (const Ant& a) const{
		return pos < a.pos;
	}
}ant[N];
int order[N];
int main(void)
{
//	freopen("out.txt", "w", stdout);
	int T, len, t, n, cas = 1;
	string s;
	cin >> T;
	while (T--)
	{
		cin >> len >> t >> n;
		for (int i = 1; i <= n; i++){
			cin >> ant[i].pos >> s;
			if (s[0] == 'R') ant[i].dir = 1;
			else ant[i].dir = -1;
			ant[i].id = i;
		}	
		sort(ant+1, ant+1+n);
		for (int i = 1; i <= n; i++){
			order[ant[i].id] = i;  // 记录输入顺序和蚂蚁相对顺序的一个映射
			ant[i].pos += t * ant[i].dir;
		}
		sort(ant+1, ant+1+n);
		ant[0].pos = ant[n+1].pos = -1;
		printf("Case #%d:\n", cas++); 
		for (int i = 1; i <= n; i++){
			int p = order[i];
			if (ant[p].pos < 0 || ant[p].pos > len)// 位置可以等于0。。。。 
				puts("Fell off");
			else if (ant[p-1].pos == ant[p].pos || ant[p+1].pos == ant[p].pos)
				printf("%d Turning\n", ant[p].pos);
			else 
				printf("%d %c\n", ant[p].pos, ant[p].dir==1?'R':'L');
		}
		puts("");
	}
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值