车车排队队(数学题)

题意:在过一个路口的时候,主角车车发现自己前面有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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
出租车排队模型在python中可以通过使用据结构来实现。列是一种先进先出(FIFO)的据结构,非常适合模拟出租车排队的过程。 首先,需要导入Python的queue模块,并创建一个空的列对象。代码如下所示: ```python import queue taxi_queue = queue.Queue() ``` 接下来,可以使用列对象的put()方法来添加出租车到列中,模拟出租车加入排队的过程。代码如下所示: ```python taxi_queue.put("出租车A") taxi_queue.put("出租车B") taxi_queue.put("出租车C") ``` 然后,可以使用列对象的get()方法来获取列中的出租车,并模拟出租车按照先后顺序出的过程。代码如下所示: ```python print(taxi_queue.get()) # 输出:出租车A print(taxi_queue.get()) # 输出:出租车B print(taxi_queue.get()) # 输出:出租车C ``` 通过以上代码,就可以实现一个简单的出租车排队模型。当出租车加入列时,使用put()方法将出租车添加到列末尾;当出租车出时,使用get()方法从列头部取出出租车。 在实际应用中,可以结合其他功能和逻辑,例如设置出租车排队最大长度、使用循环来模拟不断有出租车加入和出的过程等等,以更好地模拟实际的排队情况。 最后,需要注意在使用列时,需要保证多个线程或进程之间的同步,以避免出现不一致或意外的情况。可以使用queue模块提供的线程安全的列类,例如queue.Queue(),来避免多线程环境下的竞争问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值