当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

CISP-PTE真题演示

mysql 中 in 的用法
![[其他] DS5](/img/20/6863bb7b58d2e60b35469ba32e5830.png)
[其他] DS5

Programmers make money and practice, teach you how to do paid courses, self-media, paid articles and paid technical courses to make money

2022 SQL big factory high-frequency practical interview questions (detailed analysis)

More fragrant open source projects than Ruoyi in 2022

报错:npm ERR code EPERM

The Golden Circle Rule: Deep Thinking Methods for Successful People

互联网(软件)公司项目管理软件调研报告

瑞吉外卖项目:新增菜品与菜品分页查询
随机推荐
navicat无法连接mysql超详细处理方法
[GO Language Basics] 1. Why do I want to learn Golang and the popularization of GO language entry
MySQL stored procedure
五一去见了一些身价数千万的成功人士,我一些新的思路和启示
I went to meet some successful people worth tens of millions on May 1st, and I have some new ideas and inspirations
SQL连接表(内连接、左连接、右连接、交叉连接、全外连接)
curl (7) Failed connect to localhost8080; Connection refused
MySQL-Explain详解
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
JVM 内存结构 超详细学习笔记(一)
MySQL - Function and Constraint Commands
文档在线化管理系统Confluce使用
Programmers care guide, give yourself a chance to make the occasional relaxation of body and mind
MySql string splitting realizes the split function (field splitting, column switching, row switching)
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
微信小程序开发学习
navicat新建数据库
cnpm安装步骤
参与开源,让程序员找回热血和激情
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及