G - Traffic Management(模拟)

Sancho was making a road trip with his family and got really bored, so he recorded the position and initial speed of every car on the highway. For Sancho, the highway is an infinite straight line, the starting time is 0, the cars are moving points, and no car will pass another car (Sancho is very idealistic).

The cars maintain their initial speed unless they are slowed down by other cars. Car A is slowed down by car B if B started in front of A, but after a while, A reached B. When this happens, both cars move together and A's speed becomes the same as B's.

Sancho knows that there will be only a finite amount of times that a car will be slowed down by another car, and wants to know when the last slowing down will take place. If no car will be slowed down, the answer is 0.

Input

The first line of the input contains a single integer N (1 ≤ N ≤ 105), indicating the number of cars.

N lines follow. The i-th line contains two integers Si and Vi (0 ≤ S, V ≤ 109), indicating the starting position of the i-th car and its speed. No two cars start in the same position.

Output

Output a single decimal number - the time of the last slow down. Your answer will be considered correct if the absolute or relative error is no greater than 10 - 4.

Namely: let's assume that your answer is a, and the official answer is b. The checker program will consider your answer correct, if .

Example

Input

4
0 10
10 3
20 3
30 2

Output

20.000000

题目大意:给出n个车的位置和速度,车和车之间会发生碰撞,碰撞会两个车合为一个,以速度小的那辆车的速度继续行驶,问最后一次发生碰撞的时间是多少

思路概括:用一个结构体将所有车的位置从大到小排好,然后依次遍历,如果开始的车速度比下一辆车的速度大或者相等的话,是不会发生碰撞的,我们就可以忽略这个车,如果下一辆车的速度比现在车的速度小的话,就会发生碰撞,我们就可以计算时间并取出最大值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define lt k<<1
#define rt k<<1|1
using namespace std;
typedef long long ll;
typedef long double ld;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mem(a, b) memset(a, b, sizeof(a))
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e6 + 5;
struct node
{
    ll l, v;
}s[maxn];

bool cmp(node a, node b)
{
    if(a.l != b.l)
    {
        return a.l > b.l;
    }
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
    {
        scanf("%lld %lld", &s[i].l, &s[i].v);
    }
    sort(s, s+n, cmp);
    double ans = 0.0;
    int tot = 0;
    for(int i=0;i<n;i++)
    {
        if(s[i].v <= s[tot].v) tot = i;
        else
        {
            ans = max(ans, 1.0 * (s[tot].l - s[i].l)/(s[i].v - s[tot].v));
        }
    }
    printf("%.6f\n", ans);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值