uva10881 Piotr's Ants
来源:互联网 发布:立体模拟软件 编辑:程序博客网 时间:2024/06/05 09:25
\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 rst 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 containingCase # x : ' followed by
Turning ’ instead of
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, printL ' 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.
结论1:两个蚂蚁相碰掉头,和直接相背而行,在考虑n个蚂蚁的位置的时候是等价的。
但是,我们还要知道这些位置哪个属于哪个蚂蚁。
结论2:蚂蚁之间的相对位置【谁在谁左右】不变。
先处理出每个蚂蚁在第几个位置,然后对末位置排序就知道谁是谁了。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct ant{ int p,d,num; /*-1:left 1:right*/ bool operator < (const ant & aa) const { if (p!=aa.p) return p<aa.p; return d<aa.d; }}a[10010];int ord[10010],n,l,t;int main(){ int T,i,x,y,K; char c[3]; scanf("%d",&T); for (K=1;K<=T;K++) { scanf("%d%d%d",&l,&t,&n); for (i=1;i<=n;i++) { scanf("%d%s",&a[i].p,c); if (c[0]=='L') a[i].d=-1; else a[i].d=1; a[i].num=i; } sort(a+1,a+n+1); for (i=1;i<=n;i++) ord[a[i].num]=i; for (i=1;i<=n;i++) if (a[i].d==1) a[i].p=min(l+1,a[i].p+t); else a[i].p=max(-1,a[i].p-t); sort(a+1,a+n+1); printf("Case #%d:\n",K); for (i=1;i<=n;i++) { x=a[ord[i]].p; y=a[ord[i]].d; if (x==-1||x==l+1) printf("Fell off\n"); else { printf("%d ",x); if ((ord[i]>1&&x==a[ord[i]-1].p)||(ord[i]<n&&x==a[ord[i]+1].p)) printf("Turning\n"); else printf("%s",y==1?"R\n":"L\n"); } } printf("\n"); }}
- UVA10881 Piotr's Ants
- UVA10881 Piotr's Ants
- Uva10881 Piotr's Ants
- uva10881 Piotr's Ants
- UVA10881----Piotr's Ants
- Uva10881 Piotr's Ants
- UVa10881-Piotr's Ants
- UVa10881-Piotr's Ants
- uva10881 Piotr's Ants
- UVA10881(Piotr's Ants)
- 【Uva10881】Piotr's Ants
- UVa10881 Piotr's Ants
- UVA10881 Piotr's Ants (模拟)
- BNU19299 UVA10881 Piotr's Ants
- [UVA10881][模拟]Piotr's Ants
- UVa10881 Piotr's Ants【模拟】
- Uva10881 - Piotr's Ants(蚂蚁)
- uva10881 - Piotr's Ants (等效变换)
- 【VB6】实现VB6中类的静态方法
- linux下离线安装tesseract-ocr
- 编程中无穷大的量的使用技巧
- 读取手机联系人
- CintaNotes:怎么恢复误删的笔记
- uva10881 Piotr's Ants
- abap中利用se95还原程序到原始版本
- 初识Java深拷贝和浅拷贝(深克隆和浅克隆)
- 电商放大镜效果
- 强引用和弱引用
- 用Mybatis自动生成代码
- 软件测试的几个误区
- leetcode_441. Arranging Coins 分配硬币构造台阶
- MSQL—JOIN的使用