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;
}
时间: 2024-08-05 00:06:39

PAT (Advanced Level) 1033. To Fill or Not to Fill (25)的相关文章

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

Pat(Advanced Level)Practice--1060(Are They Equal)

Pat1060代码 题目描述: If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with simple chopping. Now given the number of significant digits on a machine and two flo