HDU 6438 Buy and Resell

高卖低买,可以交易多次

维护一个优先队列,贪心

相当于每天卖出

用当前元素减优先队列最小得到收益

用0/卖出,1/买入标志是否真实进行了交易,记录次数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll A[500005];
#define P pair<ll,ll>
priority_queue<P,vector<P>,greater<P> > q;
int main()
{
    int T,N;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        ll ans=0,t,tt=0;
        while(!q.empty())q.pop();
        for(int i=0;i<N;i++){
            scanf("%lld",&A[i]);
            q.push(P(A[i],0));
            q.push(P(A[i],1));
            t=A[i]-q.top().first;
            if(q.top().second==1)tt+=2;
            q.pop();
            ans+=t;

        }
        cout<<ans<<‘ ‘<<tt<<‘\n‘;
    }
}

原文地址:https://www.cnblogs.com/liulex/p/11367476.html

时间: 2024-07-28 19:33:28

HDU 6438 Buy and Resell的相关文章

HDU 6438 Buy and Resell ( 2018 CCPC 网络赛 &amp;&amp; 贪心 )

题目链接 题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少? 分析 : 和 CF 867E 一模一样 传送门 可以去搜这题的题解.有很多 对于每个元素产生的贡献 可以先算出暂时的最优值 如果下次碰到更优的选择再进行替换 具体就是首先使用小顶堆维护枚举过的元素 然后对于当前枚举到的元素 用它和堆顶元素做对比.如果小于或等于堆顶元素 那么它无法和之前枚举过的所有元素的任何一个做减法产生贡献

HDU6438 Buy and Resell

HDU6438 Buy and Resell 比较经典的贪心问题了 爬山 or 堆 ? #include <bits/stdc++.h> using namespace std; const int maxn = 100005; typedef long long ll; struct goods { int f,cost; goods(){}; goods(int _f,int _cost) { f = _f;cost = _cost; } bool operator < (const

2018中国大学生程序设计竞赛 - 网络选拔赛 hdu6438 Buy and Resell 买入卖出问题 贪心

Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1126    Accepted Submission(s): 359 Problem Description The Power Cube is used as a stash of Exotic Power. There are n cities numbe

(01背包)HDU - 2126 Buy the souvenirs

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 题意:给n个物品,和m块钱,输出最多物品个数和其方案数. 委屈:求出最多物品个数就是一个裸的01背包,但是同时求出方案数,难住了我. 想了半天,感觉可以一波dp求出来,但是又想不明白状态是怎么表示和转移的. 无奈就先写个dfs提交试一发,果断超时了. 最后无奈看了题解,只能说,01背包还是不会. 其实与其说01背包不会不如说动态规划不会,感觉好难. 感觉自己好lowbee啊.. 感觉碰到dp就

[HDU 2126] Buy the souvenirs (动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 题意:给你n个物品,m元钱,问你最多能买个多少物品,并且有多少种解决方案. 一开始想到的是,先解决给m元钱因为我花的钱少就一定能购买够多的物品,因此是个贪心算法. 记买最多的物品数为c. 然后就是设计状态dp[i][j]代表我从前i个物品里花了j元钱,买c个物品有多少种方案. 后来发现状态维数不够,得重新想想. 于是就想到: 设计状态dp[i][j][k]代表我从前i个物品里买了j个,花的钱不

hdu 2126 Buy the souvenirs(记录总方案数的01背包)

Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1662    Accepted Submission(s): 611 Problem Description When the winter holiday comes, a lot of people will have a trip. Genera

HDU 2126 Buy the souvenirs

Buy the souvenirs Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 212664-bit integer IO format: %I64d      Java class name: Main When the winter holiday comes, a lot of people will have a trip. Generally, the

hdu 6438

The Power Cube is used as a stash of Exotic Power. There are nn cities numbered 1,2,…,n1,2,…,n where allowed to trade it. The trading price of the Power Cube in the ii-th city is aiai dollars per cube. Noswal is a foxy businessman and wants to quietl

HDU——1133 Buy the Ticket

Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7152    Accepted Submission(s): 2998 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the nex