洛谷P1016 旅行家的预算

#include <iostream>
#include<cstdio>

using namespace std;

int main()
{
    double p[8], d[8];
    double d1, c, d2, x;
    int n;
    double min = 0;
    cin >> d1 >> c >> d2 >> p[0] >> n;
    d[0] = 0;    
    d[n + 1] = d1;
    for (int i = 1; i <= n; i++)
        cin >> d[i] >> p[i];
    for (int i = 0; i <= n; i++)
    {
        if (i != 0) x -= (d[i] - d[i - 1]) / d2;
        else x = 0;
        if (c * d2 < d[i + 1] - d[i])
        {
            cout << "No Solution" << endl;
            return 0;
        }
        int t = 0;
        for (int j = i + 1; j <= n; j++)
        {
            if (c * d2 < d[j] - d[i]) break;
            if (p[j] < p[i])
            {
                t = j;
                break;
            }
        }
        if (t == 0)
        {
            if (d[n + 1] - d[i] < c * d2)
            {
                min += ((d[n + 1] - d[i]) / d2 - x) * p[i];
                printf("%.2lf", min);
                return 0;
            }
            else
            {
                min += (c - x) * p[i];
                x = c;
            }
        }
        else
        {
            double need = (d[t] - d[i]) / d2;
            if (x < need)
            {
                min += (need - x) * p[i];
                x = need;
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值