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;
    }
  • 相关阅读:
    推荐有关微信开发的十个开源项目
    curl 常见错误码
    mysql修改root密码
    BAT批处理之文件与文件夹操作代码(附xcopy命令详解)
    UE4 Pak包加载
    libcurl 文件上传
    win7右键在目录当前打开命令cmd窗口
    SQLiteDeveloper破解
    Excel替换应用
    解决局域网2级路由互相连接共享的问题
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5515644.html
Copyright © 2011-2022 走看看