PAT (Advanced Level) 1033. To Fill or Not to Fill (25)

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1033. To Fill or Not to Fill (25)相关的知识,希望对你有一定的参考价值。

贪心。注意x=0处没有加油站的情况。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

struct X
{
    double cost, x, v;
    int id;
}s[500 + 10];
double C, D, P;
double len;
int n;

bool f(double a, double b)
{
    if (fabs(a - b)<1e-7) return 1;
    return 0;
}

struct Y
{
    int id;
    double cost, x, v;
    Y(int ID, double COST, double X,double V)
    {
        id = ID;
        cost = COST;
        x = X;
        v = V;
    }
    bool operator < (const Y &a) const {
        if (f(cost, a.cost)) return x>a.x;
        return cost>a.cost;
    }
};

bool cmp(const X&a, const X&b) { return a.x<b.x; }

bool FAIL()
{
    len = 0;
    if (s[1].x > 0) return 1;
    for (int i = 1; i < n; i++)
    {
        len = s[i].x + P*C;
        if (len < s[i + 1].x) return 1;
    }
    return 0;
}

int main()
{
    scanf("%lf%lf%lf%d", &C, &D, &P, &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lf%lf", &s[i].cost, &s[i].x);
        s[i].v = 0;
    }
    sort(s + 1, s + 1 + n, cmp);
    if (s[n].x<D)  n++, s[n].x = D;
    for (int i = 1; i <= n; i++) s[i].id = i;

    if (FAIL()) printf("The maximum travel distance = %.2lf\n", len);
    else 
    {
        double sum = 0, ans = 0;
        int p = 1;

        priority_queue<Y>Q;
        Q.push(Y(1, s[1].cost, s[1].x, 0));

        for (int i = 2; i <= n; i++)
        {
            double d = s[i].x - s[i - 1].x;
            double need = d / P;

            while (1)
            {
                if (f(need, 0)) break;
                while (1)
                {
                    Y head = Q.top(); Q.pop();
                    if (head.id < p) continue;
                    else if (f(s[head.id].v, C)) continue;
                    else
                    {
                        p = head.id;
                        double h = min(need, C - s[p].v);
                        need = need - h;
                        sum = sum + h;
                        s[p].v = sum - s[p].x / P;
                        ans = ans + s[p].cost*h;
                        Q.push(head);
                        break;
                    }
                }
            }

            Q.push(Y(i, s[i].cost, s[i].x, 0));
        }
        printf("%.2lf\n", ans);
    }
    return 0;
}

 

以上是关于PAT (Advanced Level) 1033. To Fill or Not to Fill (25)的主要内容,如果未能解决你的问题,请参考以下文章

PAT Basic Level 1033

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095