zoukankan      html  css  js  c++  java
  • 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
    ", 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
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    数字形式转换
    货币转换
    温度转换
    volatile 的可见性,禁止指令重排序,无法保证原子性的理解
    mysql索引的结构的分析
    史上最详细的ORACLE19c安装说明
    Solaris 修改联网代理的设置
    Oracle Drop表并未直接删除 drop table xx purge
    oracle自定义函数创建函数索引
    连线法合并两个有序链表
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5515644.html
Copyright © 2011-2022 走看看