hdu 6438
Posted coolwx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 quietly make a fortune by buying and reselling Power Cubes. To avoid being discovered by the police, Noswal will go to the ii-th city and choose exactly one of the following three options on the ii-th day:
1. spend aiai dollars to buy a Power Cube
2. resell a Power Cube and get aiai dollars if he has at least one Power Cube
3. do nothing
Obviously, Noswal can own more than one Power Cubes at the same time. After going to the nn cities, he will go back home and stay away from the cops. He wants to know the maximum profit he can earn. In the meanwhile, to lower the risks, he wants to minimize the times of trading (include buy and sell) to get the maximum profit. Noswal is a foxy and successful businessman so you can assume that he has infinity money at the beginning.
InputThere are multiple test cases. The first line of input contains a positive integer TT (T≤250T≤250), indicating the number of test cases. For each test case:
The first line has an integer nn. (1≤n≤1051≤n≤105)
The second line has nn integers a1,a2,…,ana1,a2,…,an where aiai means the trading price (buy or sell) of the Power Cube in the ii-th city. (1≤ai≤1091≤ai≤109)
It is guaranteed that the sum of all nn is no more than 5×1055×105.
OutputFor each case, print one line with two integers —— the maximum profit and the minimum times of trading to get the maximum profit.Sample Input
3 4 1 2 10 9 5 9 5 9 10 5 2 2 1
Sample Output
16 4 5 2 0 0 插入堆两次其实是差分的思想,插入一次pop一次其实就是不操作了
#include<bits/stdc++.h> #define mme(a,b) memset((a),(b),sizeof((a))) using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; int main() int tim; int n; scanf("%d", &tim); while(tim--) scanf("%d",&n); priority_queue<pair<int,int>>q; LL ans = 0; int tot = 0; for(int i = 0, x; i < n; ++i) scanf("%d",&x); q.push(make_pair(-x,1));//买入 q.push(make_pair(-x,2));//卖出 LL tmp=x+q.top().first; if(q.top().second == 1)tot+=2;//对于实际交易,买入一次必对应一次卖出 ans += tmp; q.pop(); printf("%lld %d\n",ans,tot); return 0;
以上是关于hdu 6438的主要内容,如果未能解决你的问题,请参考以下文章
HDU 6438 Buy and Resell (CCPC网络赛) 堆 贪心