您的位置:首页 > 大数据 > 物联网

思维题 UVA 10881 Piotr's Ants

2015-07-14 10:16 585 查看
题目传送门

 /*
题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态
思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了
关键2:蚂蚁的相对位置不变    关键3:order数组记录顺序
*/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;

const int MAXN = 1e4 + 10;
const int INF = 0x3f3f3f3f;
const char dir_name[][10] = {"L", "Turning", "R"};
struct Ant
{
int pos, dir, id;
bool operator < (const Ant &a)    const
{
return pos < a.pos;
}
}pre[MAXN], now[MAXN];
int order[MAXN];

int main(void)        //UVA 10881 Piotr's Ants
{
//    freopen ("UVA_10881.in", "r", stdin);

int T;    int cas = 0;    int len, t, n;
scanf ("%d", &T);
while (T--)
{
scanf ("%d%d%d", &len, &t, &n);
char ch;
for (int i=1; i<=n; ++i)
{
int p, d;    char ch;
scanf ("%d %c", &p, &ch);
d = ((ch=='L') ? -1 : 1);
pre[i] = (Ant) {p, d, i};
now[i] = (Ant) {p+t*d, d, 0};
}

sort (pre+1, pre+1+n);        //计算相对位置
for (int i=1; i<=n; ++i)    order[pre[i].id] = i;    //输入(输出)的顺序

sort (now+1, now+1+n);
for (int i=1; i<n; ++i)
{
if (now[i].pos == now[i+1].pos)
now[i].dir = now[i+1].dir = 0;
}

printf ("Case #%d:\n", ++cas);
for (int i=1; i<=n; ++i)
{
int x = order[i];
if (now[x].pos < 0 || now[x].pos > len)    puts ("Fell off");
else
{
printf ("%d %s\n", now[x].pos, dir_name[now[x].dir+1]);
}
}

puts ("");
}

return 0;
}

/*
Case #1:
2 Turning
6 R
2 Turning
Fell off

Case #2:
3 L
6 R
10 R
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: