车车排队队(数学题)

题意:在过一个路口的时候,主角车车发现自己前面有n个车车,每个车车有三个属性:车车长度,车车离停车线距离,车车最大速度。然后如果有一个车车比前面那个车快,他不能超车,他只能以零距离紧贴在前面的车车后面以前面的车车速度行驶。否则就以最大速度行驶。
主角想要知道自己的车车车头碰到停车线需要的时间。

输入:多组输入
一个n
n+1个车车的长度(第0个是主角车)
n+1个车车距离停车线的距离
n+1个车车的最大速度

输出:时间,误差在1e-6

Sample Input
1
2 2
7 1
2 1
2
1 2 2
10 7 1
6 2 1

Sample Output
3.5000000000
5.0000000000

思路:想了一百种直接算的方法,实在是太复杂了,不得不放弃。
然后想了好一会,才明白其实主角车无论如何都是紧贴在(0-n)辆车的屁股后面的,所以其实只要求某一辆车开到超过停车线 他屁股后面所有的车的长度(不包括主角车)之和 的最大值就好了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<math.h>
#include<vector>
#include<stack>
#include<string>
#include<set>
#include<map>
#include<fstream>
#include<time.h>
#pragma warning(disable:6031)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + 10;
const ll mode = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846264338327950;
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); }
template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); }

int n;
double ans = 0, slen = 0;

struct node 
{
    double l, s, v;
}car[maxn];

bool cmp(node x, node y) 
{
    return x.s < y.s;
}

void solve()
{
    ans = 0, slen = 0;
    for (int i = 1; i < n; i++)
        slen += car[i].l;
    sort(car, car + n, cmp);
    for (int i = 0; i < n; i++)
    {
        ans = max(ans, (car[i].s + slen) / car[i].v);
        slen -= car[i].l;
    }
}

int main()
{
    while (scanf("%d", &n) != EOF) 
    {
        n++;
        for (int i = 0; i < n; i++) 
            scanf("%lf", &car[i].l);
        for (int i = 0; i < n; i++) 
            scanf("%lf", &car[i].s);
        for (int i = 0; i < n; i++) 
            scanf("%lf", &car[i].v);

        solve();

        printf("%.8lf\n", ans);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值