[OpenJudge 3064]坠落的蚂蚁
[OpenJudge 3064]坠落的蚂蚁
试题描述
一根长度为1米的木棒上有若干只蚂蚁在爬动。它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右。如果两只蚂蚁碰头,则它们立即交换速度并继续爬动。三只蚂蚁碰头,则两边的蚂蚁交换速度,中间的蚂蚁仍然静止。如果它们爬到了木棒的边缘(0或100厘米处)则会从木棒上坠落下去。在某一时刻蚂蚁的位置各不相同且均在整数厘米处(即1,2,3,…99厘米),有且只有一只蚂蚁A速度为0,其他蚂蚁均在向左或向右爬动。给出该时刻木棒上的所有蚂蚁位置和初始速度,找出蚂蚁A从此时刻到坠落所需要的时间。
输入
第一行包含一个整数表示蚂蚁的个数N(2<=N<=99),之后共有N行,每一行描述一只蚂蚁的初始状态。每个初始状态由两个整数组成,中间用空格隔开,第一个数字表示初始位置厘米数P(1<=P<=99),第二个数字表示初始方向,-1表示向左,1表示向右,0表示静止。
输出
蚂蚁A从开始到坠落的时间。若不会坠落,输出“Cannot fall!”
输入示例
-
-
输出示例
数据规模及约定
见“输入”
题解
如果不考虑蚂蚁的编号,观察当两只蚂蚁撞上后调换方向其实和互相穿过没什么区别,所以能掉出去的蚂蚁一定会在 100 步内掉出(我们可以假设蚂蚁可以互相穿过,那么最晚掉下去的蚂蚁要么是开始时最左边向右走的或是最右边向左走的,而它们掉下去的步数显然不会超过 100)。
那么就暴力模拟 100 步咯。(有许多细节,不如拆成 200 步走,即每两个时刻的正中间再加一个时刻。)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 110
int n;
struct Ant {
int x, v, id;
bool has, pau;
Ant() {}
Ant(int _, int __, int ___): x(_), v(__), id(___) {}
void Move() {
if(!pau) x += v;
pau = 0;
return ;
}
bool operator < (const Ant& t) const { return x != t.x ? x < t.x : v < t.v; }
} as[maxn];
bool cmp(Ant a, Ant b) { return a.id < b.id; } int main() {
// freopen("data.in", "r", stdin);
while(scanf("%d", &n) == 1) {
int tar;
for(int i = 1; i <= n; i++) {
int a = read(), b = read();
as[i] = Ant(a << 1, b, i); as[i].has = 1;
if(!as[i].v) tar = i;
} bool ok = 0;
for(int i = 1; i <= 200; i++) {
for(int j = 1; j <= n; j++) as[j].Move();
sort(as + 1, as + n + 1);
for(int j = 1; j <= n; j++) if(as[j].has) {
if(as[j].x == 0 || as[j].x == 200) {
if(as[j].id == tar){ printf("%d\n", i >> 1); ok = 1; break; }
as[j].has = 0;
}
else if(as[j].x == as[j+1].x && as[j+1].x == as[j+2].x) swap(as[j].v, as[j+2].v), sort(as + j, as + j + 3), j++;
else if(as[j].x == as[j+1].x) swap(as[j].v, as[j+1].v), sort(as + j, as + j + 2);
}
if(ok) break;
// sort(as + 1, as + n + 1, cmp);
// for(int j = 1; j <= n; j++) if(as[j].x < 10) printf("%d %d %d\n", as[j].id, as[j].x, as[j].v); putchar('\n');
} if(!ok) puts("Cannot fall!");
} return 0;
}
[OpenJudge 3064]坠落的蚂蚁的更多相关文章
- 九度OJ 1159:坠落的蚂蚁 (模拟、排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1098 解决:277 题目描述: 一根长度为1米的木棒上有若干只蚂蚁在爬动.它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.如 ...
- 九度oj 题目1159:坠落的蚂蚁
题目描述: 一根长度为1米的木棒上有若干只蚂蚁在爬动.它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.如果两只蚂蚁碰头,则它们立即交换速度并继续爬动.三只蚂蚁碰头,则两边的蚂蚁交换速度, ...
- [poj3046][Ant counting数蚂蚁]
题目链接 http://noi.openjudge.cn/ch0206/9289/ 描述 Bessie was poking around the ant hill one day watching ...
- 【OpenJudge 8463】Stupid cat & Doge
http://noi.openjudge.cn/ch0204/8463/ 挺恶心的一道简单分治. 一开始准备非递归. 大if判断,后来发现代码量过长,决定大打表判断后继情况,后来发现序号不对称. 最后 ...
- 【OpenJudge 191】【POJ 1189】钉子和小球
http://noi.openjudge.cn/ch0405/191/ http://poj.org/problem?id=1189 一开始忘了\(2^{50}\)没超long long差点写高精度Q ...
- 【OpenJudge 1665】完美覆盖
http://noi.openjudge.cn/ch0405/1665/?lang=zh_CN 状压水题,手动转移 #include<cstdio> #include<cstring ...
- 【OpenJudge 1793】矩形覆盖
http://noi.openjudge.cn/ch0405/1793/ 好虐的一道题啊. 看数据范围,一眼状压,然后调了好长时间QwQ 很容易想到覆盖的点数作为状态,我用状态i表示至少覆盖状态i表示 ...
- OpenJudge 2990:符号三角形 解析报告
2990:符号三角形 总时间限制: 1000ms 内存限制: 65536kB 描述 符号三角形的第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“ ...
- 蚂蚁【A001】
[1005]出自附中练习场,其他编号(1005)[难度A]——————————————————————————————————————————————————————————————————————— ...
随机推荐
- opc 方面研究
http://opcuaservicesforwpf.codeplex.com/ WPF + OPC UA
- jQuery应用之(一)使用jQuery选择器(荐)
如上文(地址)jQuery预先的javascript的编程,提供了计划所有css3下的标准选择器,开发者可以利用这些选择器轻松选择各种元素,供javascript使用. 重要的是jQuery对这些选择 ...
- Daily Scrum – 1/12
Meeting Minutes Merge Wordlist & Word Recite entry. (P0) – Done. Remove "Word Challenge&quo ...
- 转:java多线程--同步容器
java同步容器 在Java的集合容器框架中,主要有四大类别:List.Set.Queue.Map.List.Set.Queue接口分别继承了Collection接口,Map本身是一个接口.注意Col ...
- BZOJ SCOI2005骑士精神
裸IDA*,ans从1到15循环来限制搜索深度. #include<cstdio> #include<cstring> #include<algorithm> us ...
- 读JS高级(兼容&&BOM&&私有变量&&面向对象)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- hdu1542矩阵的并 线段树+扫描线
求矩阵的并,也就是要求所有的面积.那可以吧总的图形按照矩阵来切割.使其为一块一块. 输入的时候用坐标表示,这里扫描线从下到上扫描.初始时让下面的边为1,上面的为-1: 用一条先从下面开始想上扫描.遇到 ...
- POJ1325 Machine Schedule
Description As we all know, machine scheduling is a very classical problem in computer science and h ...
- BZOJ1001 狼抓兔子(裸网络流)
Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...
- 洛谷P2925 [USACO08DEC]干草出售Hay For Sale
题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...