UVA10881 Piotr‘s Ants

知识点:思维,模拟

这个题有个操作和刚刚练习的链表一样,就是排完序之后,记录输入元素在有序序列里面的位置,这个题用到的知识主要就是问题转换的思维,移动之后,每个蚂蚁的绝对位置会改变,但是它的相对位置不会改变,这类题解法的基础就是来自这里,我们先输入元素,排序,记录一下输入的第几个元素在有序序列里面的位置,然后模拟蚂蚁走路,然后就要输出了,输出的时候我们遍历b数组,b[0]表示的就是第一个输入的元素在有序序列里面的位置,就是下标之间的映射,链表题里面就是地址之间的映射,然后我们分类讨论,输出就行了,这个题写了不到20分钟就过了

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 5;

struct node {
	int x, r;
	char dir;
} a[N];

bool cmp(node b, node c) {
	return b.x < c.x;
}

int main() {
	int T;
	cin >> T;
	int tt = 1;
	while (T--) {
		cout << "Case #" << tt++ << ":\n";
		int l, t, n;
		cin >> l >> t >> n;
		for (int i = 0; i < n; i++) {
			cin >> a[i].x >> a[i].dir;
			a[i].r = i;
		}
		sort(a, a + n, cmp);
		int b[N];
		for (int i = 0; i < n; i++) {
			b[a[i].r] = i;
			if (a[i].dir == 'L') a[i].x -= t;
			else a[i].x += t;
		}
		sort(a, a + n, cmp);
		for (int i = 0; i < n; i++) {
			int t = b[i];
			if (a[t].x < 0 || a[t].x > l) {
				cout << "Fell off\n";
			} else {
				cout << a[t].x << " ";
				if (!t && a[t].x == a[t + 1].x) cout << "Turning\n";
				else if (t == n - 1 && a[t].x == a[t - 1].x) cout << "Turning\n";
				else if (t && t < n - 1 && (a[t].x == a[t - 1].x || a[t].x == a[t + 1].x)) cout << "Turning\n";
				else cout << a[t].dir << endl;
			}
		}
		cout << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值