10881 - Piotr's Ants(排序)
题目大意:在一个长为L的木棒上有n只蚂蚁,给出蚂蚁的初始位置以及方向,问说移动T秒后各个蚂蚁的位置以及状态,如果两只蚂蚁在移动的过程中相撞,则会同时掉头。
解题思路:问题只要解决说两只蚂蚁相撞的情况就差不多了,其实从整体上来看(不考虑蚂蚁的编号),“相撞”和对穿而过“是一样的,只不过移动到那个位置的蚂蚁并不是先前的那只。所以说只要记录下每只蚂蚁的顺序,它是不会因为移动而跳到另外一只的前面。
#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; const int N = 10005;
const char dirName[][10] = { "L", "Turning", "R" }; struct Ant {
int id;
int p;
int dir;
bool operator < (const Ant& c) const {
return p < c.p;
}
}before[N], after[N];
int L, T, n, order[N]; void input() {
scanf("%d%d%d", &L, &T, &n); int d;
char ch;
for (int i = 0; i < n; i++) {
scanf("%d %c", &before[i].p, &ch);
before[i].id = i;
d = after[i].dir = before[i].dir = (ch == 'L')? -1: 1;
after[i].p = before[i].p + T * d;
}
} void solve() { memset(order, 0, sizeof(order));
sort(before, before + n);
for (int i = 0; i < n; i++)
order[before[i].id] = i; sort(after, after + n);
for (int i = 1; i < n; i++)
if (after[i - 1].p == after[i].p) after[i - 1].dir = after[i].dir = 0;
} void output() {
for (int i = 0; i < n; i++) {
int a = order[i];
if (after[a].p < 0 || after[a].p > L) printf("Fell off\n");
else printf("%d %s\n", after[a].p, dirName[after[a].dir + 1]);
}
printf("\n");
} int main () {
int cas;
scanf("%d", &cas);
for (int i = 1; i <= cas; i++) {
input(); solve(); printf("Case #%d:\n", i);
output();
}
return 0;
}
10881 - Piotr's Ants(排序)的更多相关文章
- UVA.10881 Piotr's Ants (思维题)
UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...
- 思维题 UVA 10881 Piotr's Ants
题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...
- cogs 1456. [UVa 10881,Piotr's Ants]蚂蚁
1456. [UVa 10881,Piotr's Ants]蚂蚁 ★ 输入文件:Ants.in 输出文件:Ants.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述 ...
- UVA 10881 Piotr's Ants(等效变换 sort结构体排序)
Piotr's AntsTime Limit: 2 seconds Piotr likes playing with ants. He has n of them on a horizontal po ...
- 10881 - Piotr's Ants
Problem D Piotr's Ants Time Limit: 2 seconds "One thing is for certain: there is no stopping th ...
- [ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]
"One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one ...
- Uva 10881 Piotr’s Ants 蚂蚁
一根长度为 L 厘米的木棍上有 n 只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为 1 厘米/秒.当两只蚂蚁相撞时,二者同时调头(掉头用的时间忽略不计).给出每只蚂蚁的初始位置和朝向,计算 T 秒之后 ...
- UVa 10881 Piotr's Ants (等价变换)
题意:一个长度为L的木棍上有n个蚂蚁,每只蚂蚁要么向左,要么向右,速度为1,当两只蚂蚁相撞时, 它们同时掉头.给定每只蚂蚁初始位置和朝向,问T秒后,每只蚂蚁的状态. 析:刚看到这个题时,一点思路也没有 ...
随机推荐
- Dice (III) 概率dp
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> ...
- javascript 不间断向左滚动图片
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- DB2 WIN7 WIN8在指示的文件系统中找不到数据库目录
前言:win7下一些软件的不正常,跟win7的权限有很大关系. 在win7下安装db2 9.7客户端后,在cmd中运行db2cmd启动clp,输入db2的任何命令都显示:SQL ...
- <string> <string.h>
在C++开发过程中经常会遇到两个比较容易混淆的头文件引用#include<string.h> 和 #include<string>,两者的主要区别如下: #include< ...
- alter system register
alter system register的用法 1 Static Registration via set the listener.ora2 Dynamic Instance Registrati ...
- Spring 初学 1
Spring是一个轻量级的框架,他有自己的MVC框架SpringMVC,在以往的Web项目中大多采用Structs2+hibernate+Spring的框架,Structs做web层,Hibernat ...
- Meta 的两个 相关属性
Meta标签中的apple-mobile-web-app-status-bar-style属性及含义: “apple-mobile-web-app-status-bar-style”作用是控制状态栏显 ...
- BZOJ 4010 菜肴制作
Description 知名美食家小A被邀请至ATM 大酒店,为其品评菜肴. ATM酒店为小A准备了\(N\)道菜肴,酒店按照为菜肴预估的质量从高到低给予\(1\)到\(N\)的顺序编号,预估质量最高 ...
- 代理Delegate的小应用(代理日期控件和下拉框)
前言 在平时关于表格一类的的控件使用中,不可避免需要修改每个Item的值,通过在Item中嵌入不同的控件对编辑的内容进行限定,然而在表格的Item中插入的控件始终显示,当表格中item项很多的时候,会 ...
- cocos2d-iphone加入芒果广告
cocos2d-iphone加入芒果广告的时候在模拟器上显示不出广告 真机上却可以.提示信息如下: AdsMoGo don't start alternate ADDRESPONSE - ADDING ...