思维题 UVA 10881 Piotr's Ants
/*
题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态
思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了
关键2:蚂蚁的相对位置不变 关键3:order数组记录顺序
*/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std; const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f;
const char dir_name[][] = {"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 = ; int len, t, n;
scanf ("%d", &T);
while (T--)
{
scanf ("%d%d%d", &len, &t, &n);
char ch;
for (int i=; i<=n; ++i)
{
int p, d; char ch;
scanf ("%d %c", &p, &ch);
d = ((ch=='L') ? - : );
pre[i] = (Ant) {p, d, i};
now[i] = (Ant) {p+t*d, d, };
} sort (pre+, pre++n); //计算相对位置
for (int i=; i<=n; ++i) order[pre[i].id] = i; //输入(输出)的顺序 sort (now+, now++n);
for (int i=; i<n; ++i)
{
if (now[i].pos == now[i+].pos)
now[i].dir = now[i+].dir = ;
} printf ("Case #%d:\n", ++cas);
for (int i=; i<=n; ++i)
{
int x = order[i];
if (now[x].pos < || now[x].pos > len) puts ("Fell off");
else
{
printf ("%d %s\n", now[x].pos, dir_name[now[x].dir+]);
}
} puts ("");
} return ;
} /*
Case #1:
2 Turning
6 R
2 Turning
Fell off Case #2:
3 L
6 R
10 R
*/
思维题 UVA 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,但不知道它的朝 ...
- 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 ...
- UVA 10881 - Piotr's Ants【模拟+思维】
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [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 解题报告
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&pa ...
- uva 10881 - Piotr's Ants
这个题的突破点就在于蚂蚁不能够穿过对方,故相对位置不变: 另外,又可以把蚂蚁看成运动方向不变: 代码: #include<cstdio> #include<algorithm> ...
- UVa 10881 Piotr's Ants (等价变换)
题意:一个长度为L的木棍上有n个蚂蚁,每只蚂蚁要么向左,要么向右,速度为1,当两只蚂蚁相撞时, 它们同时掉头.给定每只蚂蚁初始位置和朝向,问T秒后,每只蚂蚁的状态. 析:刚看到这个题时,一点思路也没有 ...
随机推荐
- [thrift] thrift基本原理及使用
参考文章RPC 基本原理与 Apach Thrift 初体验 RPC基本原理 RPC(Remote Procedure Call),远程过程调用,大部分的RPC框架都遵循如下三个开发步骤: 1. 定义 ...
- Java使用Memcached和Redis简单示例
package xmq.study.memcached; import java.io.IOException; import java.net.InetSocketAddress; import n ...
- Mysql的timestamp类型,自动记录数据的更新时间
datetime也可以设置自动更新的
- uva 10765 Doves and Bombs(割顶)
题意:给定一个n个点的连通的无向图,一个点的"鸽子值"定义为将它从图中删去后连通块的个数.求每一个点的"鸽子值". 思路dfs检查每一个点是否为割顶,并标 ...
- AVD那些事儿
启动了AVD却说找不到AVD 错误提示: No active compatible AVD's or devices found. Relaunch this configuration after ...
- Android学习笔记-junit单元测试
我们都知道测试对于程序员来说是必不可少的,所以,做Android程序,也要学会使用junit,这里比着java的junit测试,要稍微复杂一点,需要一些配置,下面饿哦就介绍一下怎样使用junit的测试 ...
- NDK开发,没有你想象的那么难
NDK:Native Development Kit原生开发工具 NDK能干什么:NDK使得在android中,java能够调用C函数库. 为什么要用NDK:我们都知道.java是半解释型语言,非常e ...
- 大数据R语言简析
R语言是用于统计分析.画图的语言和操作环境.R是属于GNU系统的一个自由.免费.源码开放的软件.它是一个用于统计计算和统计制图的优秀工具. R是统计领域广泛使用的诞生于1980年左右的S语言的一个分支 ...
- confusion_matrix函数的使用
from:http://blog.csdn.net/m0_38061927/article/details/77198990 官方文档中给出的用法是 sklearn.metrics.confusion ...
- A Go library implementing an FST (finite state transducer)——mark下
https://github.com/couchbaselabs/vellum Building an FST To build an FST, create a new builder using ...