2015多校第8场 HDU 5380 Travel with candy 贪心,单调队列

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5380

题意:一个人从0走到n知道ai为i节点到0的距离,没行走单位距离要消耗一颗糖,在所有节点中可以进行买糖和卖糖价格为sell[i]和buy[i],问走到n节点话费最小为多少
解法:

1、保持油箱的油一直是满的。

2、到达每个城市之后,先将到达这个城市的花费减掉,这些消耗掉的应该是价格最低的。

3、对于油箱中的油的价格如果比当前城市买入的价格更高的话就直接退出油箱(退出油箱的意思:直接以购买的价格卖出 (这里的购买价格是会改变的) ),对于油箱中的油如果价格比当前城市卖出的价格低的话,那么可以将这些油的价格改成当前城市卖出的价格(可以想象成在当前城市卖出这些油,然后以卖出的价格买回来这部分油)。

4、然后在这个城市买油加满油箱。

这些操作可以用一个双端队列来维护。


注意题解里面有个退的过程,这就是为什么每个点都把油装满的原因。

官方:

每时每刻油箱都装满,使用退油的办法,维护所有不同价格的退油油量。路上把推油少的油消耗掉。卖油相当于所有价格取max,买油时可以先把贵的退光然后买满。可以用双端队列维护。


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct FastIO
{
    static const int S = 1310720;
    int wpos;
    char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar()
    {
        static char buf[S];
        static int len = 0, pos = 0;
        if (pos == len)
            pos = 0, len = fread(buf, 1, S, stdin);
        if (pos == len) return -1;
        return buf[pos ++];
    }
    inline int xuint()
    {
        int c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x;
    }
    inline int xint()
    {
        int s = 1, c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        if (c == '-') s = -1, c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x * s;
    }
    inline void xstring(char *s)
    {
        int c = xchar();
        while (c <= 32) c = xchar();
        for (; c > 32; c = xchar()) * s++ = c;
        *s = 0;
    }
    inline void wchar(int x)
    {
        if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
        wbuf[wpos ++] = x;
    }
    inline void wint(LL x)
    {
        if (x < 0) wchar('-'), x = -x;
        char s[24];
        int n = 0;
        while (x || !n) s[n ++] = '0' + x % 10, x /= 10;
        while (n--) wchar(s[n]);
    }
    inline void wstring(const char *s)
    {
        while (*s) wchar(*s++);
    }
    ~FastIO()
    {
        if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
    }
} io;
const int maxn = 2e5+10;
struct City{
    int buy,sell,dis;
}a[maxn];
struct Queue{
    int num,val;
}q[maxn];

int main()
{
    int T,n,m;
    T = io.xint();
    while(T--){
        n = io.xint(), m = io.xint();
        a[0].dis=a[1].dis=0;
        for(int i=2; i<=n+1; i++) a[i].dis = io.xint();
        for(int i=1; i<=n+1; i++) a[i].buy = io.xint(), a[i].sell = io.xint();
        LL ans=0;
        int head=0,tail=0;//单调队列维护口袋里的糖果。
        int sum = 0;//存当前口袋里的糖果总量
        for(int i=1; i<=n+1; i++){
            int dis = a[i].dis-a[i-1].dis;
            sum -= dis;//每到一个点,首先消耗dis个
            while(dis!=0){
                if(q[head+1].num<=dis){
                    dis-=q[head+1].num;
                    head++;
                }else{
                    q[head+1].num-=dis;
                    dis=0;
                }
            }
            //从对头开始吃掉dis个
            //将口袋里价值低于当前点出售价格的糖果价值更新为当前点的出售价格
            for(int j=head+1; j<=tail; j++)
                if(q[j].val < a[i].sell)
                    q[j].val = a[i].sell;
             //将口袋里价值高于当前点购买价格的糖果按它们最高价值卖掉。
             while(tail>head&&q[tail].val>a[i].buy){
                ans-=(LL)q[tail].val*q[tail].num;
                sum-=q[tail].num;
                tail--;
             }
             //离开该点前,将口袋补充满
             if(sum!=m){
                tail++;
                q[tail].num=m-sum;
                q[tail].val=a[i].buy;
                ans+=(LL)q[tail].num*q[tail].val;
                sum=m;
             }
        }
        //将最后剩下的糖果按照它们的最高价值卖掉
        while(head<tail){
            ans-=(LL)q[head+1].num*q[head+1].val;
            head++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值