Piotr's Ants

Porsition:Uva 10881 白书P9

中文改编题:【T^T】【FJUT】第二届新生赛真S题地震了

"One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one, welcome our new insect overlords."Kent Brockman

Piotr likes playing with ants. He has n of them on a horizontal pole L cm long. Each ant is facing either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it is facing and wants to calculate where the ants will end up T seconds from now.

Input

The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containing 3 integers: L , T and n (0 ≤ n ≤ 10000). The next n lines give the locations of the n ants (measured in cm from the left end of the pole) and the direction they are facing (L or R).

Output

For each test case, output one line containing ‘Case #x:’ followed by n lines describing the locations and directions of the n ants in the same format and order as in the input. If two or more ants are at the same location, print ‘Turning’ instead of ‘L’ or ‘R’ for their direction. If an ant falls off the pole before T seconds, print ‘Fell off’ for that ant. Print an empty line after each test case.

Sample Input

2

10 1 4

1 R

5 R

3 L

10 R

10 2 3

4 R

5 L

8 R

Sample Output

Case #1:

2 Turning

6 R

2 Turning

Fell off

Case #2:

3 L

6 R

10 R

Solution

脑洞大开,两只蚂蚁相撞返回相当于穿过?但保证每只蚂蚁初始的顺序.所以每只蚂蚁直接向左向右走,实际上它会穿过很多只蚂蚁,每穿过一次就变一次身,但他们的先后顺序是保证的,就是不会真正穿过去,只是用对面那只蚂蚁代替自己,所以只要记录蚂蚁排列顺序对应在原序列第几个即可。

福利数据

戳这~

Code

// <ants.cpp> - Mon Oct 10 16:18:55 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long LL;
const int MAXN=10010;
inline int gi() {
register int w=0,q=0;register char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')q=1,ch=getchar();
while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
return q?-w:w;
}
struct node{
int p,id;char c;
bool operator<(node b)const{return p<b.p;}
}a[MAXN];int d[MAXN];
int main()
{
freopen("ants.in","r",stdin);
freopen("ants.out","w",stdout);
int T=gi();
for(int o=1;o<=T;o++){
printf("Case #%d:\n",o);
int l=gi(),t=gi(),n=gi();
for(int i=1;i<=n;i++)scanf("%d %c",&a[i].p,&a[i].c),a[i].id=i;
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
d[a[i].id]=i,a[i].p-=(a[i].c=='L'?1:-1)*t;
sort(a+1,a+1+n);
for(int i=1,x;x=d[i],i<=n;i++)
if((a[x].p==a[x-1].p&&x-1)||(x+1<=l&&a[x].p==a[x+1].p))printf("%d Turning\n",a[x].p);
else if(a[x].p>=0&&a[x].p<=l)printf("%d %c\n",a[x].p,a[x].c);
else printf("Fell off\n");printf("\n");
}
return 0;
}

  

【UVa 10881】Piotr's Ants的更多相关文章

  1. 【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁

    </pre></center><center style="font-family: Simsun;font-size:14px;"><s ...

  2. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  3. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  4. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【Uva 11584】Partitioning by Palindromes

    [Link]:https://cn.vjudge.net/contest/170078#problem/G [Description] 给你若干个只由小写字母组成的字符串; 问你,这个字符串,最少能由 ...

  9. 【Uva 11400】Lighting System Design

    [Link]: [Description] 你要构建一个供电系统; 给你n种灯泡来构建这么一个系统; 每种灯泡有4个参数 1.灯泡的工作电压 2.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...

随机推荐

  1. CSU 2018年12月月赛 H(2220): Godsend

    Description Leha somehow found an array consisting of n integers. Looking at it, he came up with a t ...

  2. [GXOI/GZOI2019]与或和(单调栈)

    想了想决定把这几题也随便水个解题报告... bzoj  luogu 思路: 首先肯定得拆成二进制30位啊 此后每一位的就是个01矩阵 Q1就是全是1的矩阵个数 Q2就是总矩阵个数减去全是0的矩阵个数 ...

  3. LeetCode(60) Permutation Sequence

    题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...

  4. POJ3641 (快速幂) 判断a^p = a (mod p)是否成立

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  5. CSU1160

    十进制-十六进制 Time Limit: 1 Sec  Memory Limit: 128 MB Description 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. ...

  6. layer弹层content写错导致div复制了一次,导致id失效 $().val() 获取不到dispaly:none div里表单的值

    ​ 错误之源: $("a.consult").click(function () {         lib_consult_html = $('#consult-html').h ...

  7. Mvc Action可以通过jsonp方式调取

    jsonp其实是一种特殊的数据获取格式,所以在Aicton直接调取的时候肯定会出现问题,下面代码是对于jsonp调取做的处理 protected virtual ActionResult Create ...

  8. HDU 4941

    Magical Forest Problem Description There is a forest can be seen as N * M grid. In this forest, ther ...

  9. Python模块:shutil、序列化(json&pickle&shelve)、xml

    shutil模块: 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fscr,fdst [, length])   # 将文件内容拷贝到另一个文件中 import shu ...

  10. [NOIP2008] 提高组 洛谷P1125 笨小猴

    题目描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设maxn是单词中出现次数最 ...