当前位置:网站首页>Falling ants (Peking University entrance exam questions)
Falling ants (Peking University entrance exam questions)
2022-07-30 06:05:00 【Zhang Xueheng】
1:题目
一根长度为 1 米的木棒上有若干只蚂蚁在爬动.
它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.
如果两只蚂蚁碰头,则它们立即交换速度并继续爬动.
三只蚂蚁碰头,则两边的蚂蚁交换速度,中间的蚂蚁仍然静止.
如果它们爬到了木棒的边缘(0 或 100 厘米处)则会从木棒上坠落下去.
在某一时刻蚂蚁的位置各不相同且均在整数厘米处(即 1,2,3,…99 厘米),有且只有一只蚂蚁 A 速度为 0,其他蚂蚁均在向左或向右爬动.
给出该时刻木棒上的所有蚂蚁位置和初始速度,找出蚂蚁 A 从此时刻到坠落所需要的时间.
输入格式
第一行包含一个整数表示蚂蚁的个数 N,之后共有 N 行,每一行描述一只蚂蚁的初始状态.
每个初始状态由两个整数组成,中间用空格隔开,第一个数字表示初始位置厘米数 P,第二个数字表示初始方向,−1 表示向左,1 表示向右,0 表示静止.
输出格式
蚂蚁 A 从开始到坠落的时间.若不会坠落,输出 Cannot fall!.
数据范围
2≤N≤99,
1≤P≤99
输入样例:
4
10 1
90 0
95 -1
98 -1
输出样例:
98
难度:中等
时/空限制:1s / 64MB
总通过数:279
总尝试数:638
来源:北京大学考研机试题
算法标签
2:代码实现
#include <bits/stdc++.h>
#define vi vector<int>
#define vp vector<pair<int, int>>
using namespace std;
int A;
int main() {
int n; cin >> n;
vi l, r;
vp data;
while(n --)
{
int a, b; cin >> a >> b;
if(b == 0) A = a;
else data.push_back({
a, b});
}
sort(data.begin(), data.end());
for(auto i : data)
{
if(i.first < A && i.second == 1) l.push_back(i.first);
else if(i.first > A && i.second == -1) r.push_back(i.first);
}
if(l.size() == r.size()) cout << "Cannot fall!";
else if(l.size() > r.size()) cout << 100 - l[l.size()-r.size()-1];
else cout << r[l.size()];
return 0;
}
边栏推荐
- Solve the problem that the local nacos is not configured but the localhost8848 connection exception always occurs
- 分布式事务之 Atomikos 原理和使用(一)
- 破纪录者(Google Kickstart2020 Round D Problem A)
- MySQL的 DDL和DML和DQL的基本语法
- 丑陋的程序员
- 瑞吉外卖项目:新增菜品与菜品分页查询
- Redis学习
- JVM之GC 调优工具 Arthas 实战使用(二)
- 倒计数(来源:Google Kickstart2020 Round C Problem A)(DAY 88)
- curl (7) Failed connect to localhost8080; Connection refused
猜你喜欢
I went to meet some successful people worth tens of millions on May 1st, and I have some new ideas and inspirations
cmd(命令行)操作或连接mysql数据库,以及创建数据库与表
The Golden Circle Rule: Deep Thinking Methods for Successful People
IDEA的database使用教程(使用mysql数据库)
mysql 中 in 的用法
MySQL 灵魂 16 问,你能撑到第几问?
[Image processing] Image skeleton extraction based on central axis transformation with matlab code
MYSQL-InnoDB的线程模型
SOA(面向服务架构)是什么?
程序员赚钱实操,手把手教你做付费课程,自媒体,付费文章及付费技术课赚钱
随机推荐
腾讯面试居然跟我扯了半小时的CountDownLatch
[Mysql] DATEDIFF函数
期末作业C#实现学生宿舍管理系统
MySQL 数据库基础知识(系统化一篇入门)
JVM 垃圾回收 超详细学习笔记(二)
Difference between cookie and session
破纪录者(Google Kickstart2020 Round D Problem A)
navicat无法连接mysql超详细处理方法
个人博客系统(附源码)
MySQL - 函数及约束命令
Redis基础学习
图形镜像对称(示意图)
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
4461. 范围分区(Google Kickstart2022 Round C Problem B)
MySQL Basics (DDL, DML, DQL)
It's time to have to learn English, give yourself multiple paths
从字节码角度带你彻底理解异常中catch,return和finally,再也不用死记硬背了
使用DataEase开源工具制作一个高质量的数据大屏
[Mysql] CONVERT函数
Within the SQL connection table (link connections, left or right, cross connection, full outer join)