0
点赞
收藏
分享

微信扫一扫

坠落的蚂蚁(暑假每日一题 40)


一根长度为

它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右。

如果两只蚂蚁碰头,则它们立即交换速度并继续爬动。

三只蚂蚁碰头,则两边的蚂蚁交换速度,中间的蚂蚁仍然静止。

如果它们爬到了木棒的边缘(

在某一时刻蚂蚁的位置各不相同且均在整数厘米处(即 厘米),有且只有一只蚂蚁 速度为 ,其他蚂蚁均在向左或向右爬动。

给出该时刻木棒上的所有蚂蚁位置和初始速度,找出蚂蚁

输入格式
第一行包含一个整数表示蚂蚁的个数 ,之后共有

每个初始状态由两个整数组成,中间用空格隔开,第一个数字表示初始位置厘米数 ,第二个数字表示初始方向,​​​−1​​​ 表示向左,​​1​​​ 表示向右,​​0​​ 表示静止。

输出格式
蚂蚁 从开始到坠落的时间。若不会坠落,输出 ​​​Cannot fall!​​。

数据范围


输入样例:

4
10 1
90 0
95 -1
98 -1

输出样例:

98

#include<iostream>
#include<vector>
#include<algorithm>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

int main(){

int n;
cin >> n;

vector<PII> q;
int A;
for(int i = 0; i < n; i++){
int x, y;
cin >> x >> y;
if(!y) A = x;
q.push_back({x, y});
}

sort(q.begin(), q.end());

vector<int> l, r;
for(auto &p: q){

if(!p.y || p.x < A && p.y < 0 || p.x > A && p.y > 0)
continue;
if(p.x < A) l.push_back(p.x);
else r.push_back(p.x);
}

if(l.size() == r.size()) cout << "Cannot fall!" << endl;
else if(l.size() < r.size()) cout << r[l.size()] << endl;
else cout << 100 - l[l.size() - r.size() - 1] << endl;

return 0;
}


举报

相关推荐

0 条评论