UVA.10881 Piotr's Ants (思维题)
UVA.10881 Piotr’s Ants (思维题)
题意分析
有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头。求解第T秒时这n只蚂蚁的状态。
若此时相撞 输出:Turning
若此时已经掉下木棍 输出:Fell off
且要按照输入时的顺序输出各个蚂蚁的状态
此题难点有2个:
1.其实相撞可以理解为交叉走过(因为速度相同)。
2.蚂蚁的相对位置保持不变。
想明白了这两点,难点就转换成了:
1.如何确定蚂蚁的位置;
2.如何处理好蚂蚁的状态。
解决方法如下:
1.首先要按照输入的顺序,给蚂蚁编上号,即ini;
2.给这n只蚂蚁按照从左向右的顺序(即pos升序)排序,并且将其ini写入loca数组。(可以理解为ini是每个蚂蚁的ID,由ini来确认每个蚂蚁的身份)。
3.模拟每只蚂蚁的移动。(利用相撞即交叉走过的原则)。
4.按照移动完的顺序,接着依照pos升序排序。
5.按照loca数组,依次改写从左向右每只蚂蚁的ini(利用的相对位置不变的原则)。
6.按照题目要求输出即可。
代码总览
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define nmax 10005
using namespace std;
struct ant{
int pos,dir,ord,ini;
}a[nmax];
int loca[nmax];
bool cmp(ant x, ant y)
{
return x.pos<y.pos;
}
bool cmp1(ant x, ant y)
{
return x.ini<y.ini;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int cas = 0;
const char *arr [] ={"L","Turning","R"};
int N;
scanf("%d",&N);
while(N--){
printf("Case #%d:\n",++cas);
memset(a,0,sizeof(a));
memset(loca,0,sizeof(loca));
int L,T,n;
scanf("%d%d%d",&L,&T,&n);
for(int i = 0; i<n; ++i){
int p;char t;
scanf("%d %c",&p,&t);
a[i].pos = p;
a[i].dir = (t == 'L'?-1:1);
a[i].ini = i;
}
sort(a,a+n,cmp);
for(int i = 0; i<n;++i){
loca[i] = a[i].ini;
a[i].pos = a[i].pos + a[i].dir * T;
}
sort(a,a+n,cmp);
for(int i = 0;i<n;++i){
a[i].ini = loca[i];
}
for(int i = 0; i<n-1;++i)
if(a[i].pos == a[i+1].pos) a[i].dir =a[i+1].dir = 0;
sort(a,a+n,cmp1);
for(int i = 0;i<n;++i){
if(a[i].pos<0 || a[i].pos>L) printf("Fell off\n");
else{
printf("%d %s\n",a[i].pos,arr[a[i].dir+1]);
}
}
printf("\n");
}
return 0;
}
UVA.10881 Piotr's Ants (思维题)的更多相关文章
- 思维题 UVA 10881 Piotr's Ants
题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...
- 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秒后,每只蚂蚁的状态. 析:刚看到这个题时,一点思路也没有 ...
随机推荐
- hdu2553N皇后问题(dfs,八皇后)
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Selenium(Python) ddt数据驱动
首先, 添加ddt模块: import unittestfrom time import sleep from ddt import ddt, data, unpack# 导入ddt模块from se ...
- Siki_Unity_1-6_C#编程初级教程(未学)
Unity 1-6 C#编程初级教程 任务1:C#和.Net框架 C#是.Net里的一个成分 2002年微软发布第一个.Net框架(多平台,行业标准,安全性) .Net框架 IDE编程工具 --产生- ...
- 《Effective C++》读书笔记 条款03 尽可能使用const 使代码更加健壮
如果你对const足够了解,只需记住以下结论即可: 将某些东西声明为const可帮助编译器侦测出错误用法,const可被施加于任何作用于内的对象.函数参数.函数返回类型.成员函数本体. 编译器强制实施 ...
- LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告
1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...
- 1.编译azkaban
1.下载azkaban的源码 https://github.com/azkaban/azkaban.git 然后解压得到azkaban-master.zip,解压:unzip azkaban-mast ...
- UESTC 1717 Journey(DFS+LCA)(Sichuan State Programming Contest 2012)
Description Bob has traveled to byteland, he find the N cities in byteland formed a tree structure, ...
- 一个改变this指向bind的函数,vue源代码
function bind(fn, ctx) { return function (a) { var l = arguments.length; return l ? l > 1 ? fn.ap ...
- 2019-1-92.4G射频芯片培训资料
2019-1-92.4G射频芯片培训资料 培训 RF 小书匠 欢迎走进zozo的学习之旅. 2.4G芯片选型 2.4G芯片开发 Q&A 2.4G芯片选型 芯片类型 soc 防盗标签2.4G无 ...
- wf效能分析
听从了老师的建议我请教了其他的同学,修改了代码实现了功能四,以下是我的效能测试: 1.采用ptime.exe测试的3次截图 可以看到的是三次执行时间分别为:1.449秒:0.915秒:0.871秒,取 ...