关闭

zoj3699 Dakar Rally (单调队列+贪心)

标签: zoj单调队列贪心
291人阅读 评论(0) 收藏 举报
分类:

传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3699

ime Limit: 2 Seconds      Memory Limit: 65536 KB

Description

The Dakar Rally is an annual Dakar Series rally raid type of off-road race, organized by the Amaury Sport Organization. The off-road endurance race consists of a series of routes. In different routes, the competitors cross dunes, mud, camel grass, rocks, erg and so on.

Because of the various circumstances, the mileages consume of the car and the prices of gas vary from each other. Please help the competitors to minimize their payment on gas.

Assume that the car begins with an empty tank and each gas station has infinite gas. The racers need to finish all the routes in order as the test case descripts.

Input

There are multiple test cases. The first line of input contains an integer T (T ≤ 50) indicating the number of test cases. Then T test cases follow.

The first line of each case contains two integers: n -- amount of routes in the race; capacity -- the capacity of the tank.

The following n lines contain three integers each: mileagei -- the mileage of the ith route; consumei -- the mileage consume of the car in the ith route , which means the number of gas unit the car consumes in 1 mile; pricei -- the price of unit gas in the gas station which locates at the beginning of the ith route.

All integers are positive and no more than 105.

Output

For each test case, print the minimal cost to finish all of the n routes. If it's impossible, print "Impossible" (without the quotes).

Sample Input

2
2 30
5 6 9
4 7 10
2 30
5 6 9
4 8 10

Sample Output

550
Impossible


题意:一条线上n个点,按顺序的排好,从第i个点到第i+1个点需要花费t升油,在点上才能补油,每次最多只能带m升油,不同点补油的价格不同,问跑到终点最少花费是多少。


很明显的贪心。

解题思路:

1、如果i点到i+1点花费超过了m,就不能到达,其余都可以

2、贪心油价

贪心思路:

1、到终点的时候油箱一定是空的。

2、如果这个点的油价比它能到的所有点油价都便宜,肯定装满(下一站终点除外)。

3、如果能到一个比当前点便宜的点,肯定只装恰好到那个点的油,不管更后面能不能到更便宜的点,因为从第一个便宜的点到下一个更便宜的点这段路是可以用那个便宜点的油来跑的。

4、如果这个点的油价比它能到的所有点油价都便宜,不仅要装满,而且要到它能到的点里面最便宜的点去补油。(单调队列实现)

下面贴代码。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
#include<iostream>
using namespace std;
struct ss
{
    long long ju;//当前点距离起点的距离
    long long mo;//当前点的油价
}s[100000+1000];
int que[100000+1000];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        long long n,m;
        cin>>n>>m;
        long long sum=0;
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            s[i].ju=sum;
            sum+=(long long)(x*y);
            s[i].mo=z;
            if((long long)(x*y)>m)
            {
                flag=1;
            }
        }
        n++;
        s[n].ju=sum;
        s[n].mo=0;//因为存的当前点距离起点的距离,所以第一个点是0,然后补一个重点,油价为0
        if(flag==1)
        {
            cout<<"Impossible\n";
            continue;
        }
        long long has=0;
        long long go=1;
        int he=1;
        int en=1;
        que[he]=1;
        long long now=1;
        long long ans=0;
        while(que[he]<n)
        {
            has-=(s[que[he]].ju-s[now].ju);
            now=que[he];
            he++;
            for(int i=go+1;i<=n;i++)
            {
                if(he<=en&&s[que[he]].mo<s[now].mo)
                    break;
                if(s[i].ju-s[now].ju<=m)
                {
                    while(en>=he&&s[que[en]].mo>=s[i].mo)
                    {
                        en--;
                    }
                    en++;
                    que[en]=i;
                }
                else
                    break;
            }
            if(s[now].mo>s[que[he]].mo)
            {
                ans+=s[now].mo*(s[que[he]].ju-s[now].ju-has);
                has=s[que[he]].ju-s[now].ju;
            }
            else
            {
                ans+=s[now].mo*(m-has);
                has=m;
            }
            go=que[en];
        }
        cout<<ans<<endl;
    }
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:18836次
    • 积分:426
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:0篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论