【UVa 10881】Piotr's Ants
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的更多相关文章
- 【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁
</pre></center><center style="font-family: Simsun;font-size:14px;"><s ...
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- 【贪心+中位数】【UVa 11300】 分金币
(解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...
- 【UVa 116】Unidirectional TSP
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVa 1347】Tour
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVA 437】The Tower of Babylon(记忆化搜索写法)
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【uva 1025】A Spy in the Metro
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【Uva 11584】Partitioning by Palindromes
[Link]:https://cn.vjudge.net/contest/170078#problem/G [Description] 给你若干个只由小写字母组成的字符串; 问你,这个字符串,最少能由 ...
- 【Uva 11400】Lighting System Design
[Link]: [Description] 你要构建一个供电系统; 给你n种灯泡来构建这么一个系统; 每种灯泡有4个参数 1.灯泡的工作电压 2.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...
随机推荐
- Web的三大基石
[HTML] 实现了Web页面. [URL] 1.url Uniform Resource Locator的缩写,称为统一资源定位符.通过URL可以访问到互联网上的一个资源.如:图片.视频.网页等.通 ...
- Leetcode 139.单词拆分
单词拆分 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典 ...
- POJ-2239 Selecting Courses,三维邻接矩阵实现,钻数据空子。
Selecting Courses Time Limit: 1000MS Memory Limit: 65536K Description It is well known that ...
- ProxySQL Tutorial : setup in a MySQL replication topology
ProxySQL Tutorial : setup in a MySQL replication topology 时间 2015-09-15 05:23:20 ORACLE数据库技术文刊 原文 h ...
- bzoj 1005 [HNOI2008] 明明的烦恼 (prufer编码)
[HNOI2008]明明的烦恼 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5907 Solved: 2305[Submit][Status][Di ...
- ModelForm组件和forms组件补充
forms组件补充: forms组件的三个字段:ChoiceField, ModelChoiceField & ModelMultipleChoiceField # forms组件:Choic ...
- SpringBoot Data JPA 关联表查询的方法
SpringBoot Data JPA实现 一对多.多对一关联表查询 开发环境 IDEA 2017.1 Java1.8 SpringBoot 2.0 MySQL 5.X 功能需求 通过关联关系查询商店 ...
- 可并堆试水--BZOJ1367: [Baltic2004]sequence
n<=1e6个数,把他们修改成递增序列需把每个数增加或减少的总量最小是多少? 方法一:可以证明最后修改的每个数一定是原序列中的数!于是$n^2$DP(逃) 方法二:把$A_i$改成$A_i-i$ ...
- Codeforces 404D Minesweeper 1D
题意: 给定字符串,其中'*'表示地雷,'1'表示左/右边有一个地雷相邻,'2'表示左右两边均有地雷相邻,'0'表示左右均无地雷相邻,'?'表示待定,可填入0,1,2或者地雷,有多少种表示方法使字母串 ...
- Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)
E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...