zoukankan      html  css  js  c++  java
  • HDU

    HDU - 5380

    感觉又是个脑洞题。

    如果没有卖的情况感觉写过很多次了。

    这题的关键在于你买糖的消耗只在加入队列的时候计算, 用不完的糖在最后退掉, 卖糖的盈利也包含在最后退糖中了,

    我们只要把所有比当前sell值小的全部边成当前的sell值就行了, 相当于卖掉了。 感觉相当巧妙。

    #include<bits/stdc++.h>
    #define LL long long
    #define LD long double
    #define ull unsigned long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ALL(x) (x).begin(), (x).end()
    #define fio ios::sync_with_stdio(false); cin.tie(0);
    
    using namespace std;
    
    const int N = 2e5 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    const double PI = acos(-1);
    
    template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
    template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
    template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
    template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}
    
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    
    int n, c, a[N];
    int sell[N], buy[N];
    
    
    struct Candy {
        int num, price;
    };
    
    int main() {
    
        int T; scanf("%d", &T);
        while(T--) {
            scanf("%d%d", &n, &c);
            for(int i = 1; i <= n; i++) {
                scanf("%d", &a[i]);
            }
            a[n + 1] = a[n];
            for(int i = 0; i <= n; i++) {
                scanf("%d%d", &buy[i], &sell[i]);
            }
    
            deque<Candy> que;
            int candytot = 0;
            LL ans = 0;
    
            for(int i = 0; i <= n; i++) {
                int cnt = 0;
                while(!que.empty() && que.back().price > buy[i]) {
                    candytot -= que.back().num;
                    ans -= 1LL * que.back().num * que.back().price;
                    que.pop_back();
                }
    
                if(candytot != c) {
                    que.push_back(Candy{c - candytot, buy[i]});
                    ans += 1LL * (c - candytot) * buy[i];
                    candytot = c;
                }
    
                while(!que.empty() && que.front().price < sell[i]) {
                    candytot -= que.front().num;
                    que.pop_front();
                }
    
                if(candytot != c) {
                    que.push_front(Candy{c - candytot, sell[i]});
                    candytot = c;
                }
    
    
                int dis = a[i + 1] - a[i];
                candytot -= dis;
    
    
                while(dis) {
                    assert(SZ(que));
                    if(dis < que.front().num) {
                        que.front().num -= dis;
                        dis = 0;
                    }
                    else {
                        dis -= que.front().num;
                        que.pop_front();
                    }
                }
    
            }
    
            while(!que.empty()) {
                ans -= 1LL * que.back().num * que.back().price;
                que.pop_back();
            }
    
            printf("%lld
    ", ans);
        }
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    大数据-数据分析-numpy库-数组的深拷贝和浅拷贝
    windows环境下mysql主从配置
    C#定时发送邮箱设置
    论《LEFT JOIN条件放ON和WHERE后的区别》
    记录成长
    RobotFramework+Selenium如何提高脚本稳定性
    Jekins 插件Extended Choice Parameter显示Json Parameter Type遇到的问题
    nGrinder 参数使用
    Jenkins REST API 实例
    java ee config / nacos / shit Alibaba Middleware
  • 原文地址:https://www.cnblogs.com/CJLHY/p/11193168.html
Copyright © 2011-2022 走看看