HDU 6438 Buy and Resell 【贪心+思维+优先队列】

news/2024/11/30 3:50:57/


传送门:HDU 6438


Buy and Resell
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description
The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed to trade it. The trading price of the Power Cube in the i-th city is ai 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 i-th city and choose exactly one of the following three options on the i-th day:
1.spend ai dollars to buy a Power Cube
2. resell a Power Cube and get ai 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 n 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.

Input
There are multiple test cases. The first line of input contains a positive integer T (T≤250), indicating the number of test cases. For each test case:
The first line has an integer n. (1≤n≤105)
The second line has n integers a1,a2,…,an where ai means the trading price (buy or sell) of the Power Cube in the i-th city. (1≤ai≤109)
It is guaranteed that the sum of all n is no more than 5×105.

Output
For 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

Hint
In the first case, he will buy in 1, 2 and resell in 3, 4. profit = - 1 - 2 + 10 + 9 = 16
In the second case, he will buy in 2 and resell in 4. profit = - 5 + 10 = 5
In the third case, he will do nothing and earn nothing. profit = 0



题意:

有n个城市,在第i个城市中,你有三种策略,你可以用ai元购买一个物品,或者用ai元卖掉一个物品,或者不买也不卖,你可以同时保存多个物品。最开始你身上没有物品,但是又无限的金钱,现在要求你从城市1走到城市n,问你最大的收益是多少,同时交易次数是多少。


题解:

很容易想到的是,我们应该花较少的钱买入,然后卖出更多的钱。如果我们在第i天买入,第j天卖出那么必须满足 a [ j ] > a [ i ] a[j]>a[i] a[j]>a[i],收入为 a [ j ] − a [ i ] a[j]-a[i] a[j]a[i],如果后面存在第k天,使得 a [ k ] > a [ j ] a[k]>a[j] a[k]>a[j],那么我们就应该在第k天卖出物品,因为收益更大,为 a [ k ] − a [ i ] a[k]-a[i] a[k]a[i]
我们发现有恒等式: a [ k ] − a [ i ] = a [ k ] − a [ j ] + a [ j ] − a [ i ] a[k]-a[i]=a[k]-a[j]+a[j]-a[i] a[k]a[i]=a[k]a[j]+a[j]a[i]; 这说明第j天有没有卖出并不会影响最终结果(相当于中间商,且无差价)
但是我们又考虑到,第j天虽然有没有卖出不影响结果,但是有没有买入是影响结果的,所有我们还要考虑买入的情况。

优先队列小根堆)来实现上述过程,如果 j 的中间商,那么我们就往队列里存两次,一次是中间商(卖出),一次是买入。当当前的a大于队首元素时,我们就进行交易,同时如果队首元素是中间商的话交易次数是不改变的。



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=110000;
int t,n;
ll cnt,ans;
priority_queue<ll,vector<ll>,greater<ll> >q;
map<ll,int> mp;
int main()
{ll a;scanf("%d",&t);while(t--){while(!q.empty()) q.pop();mp.clear();cnt=0,ans=0;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%I64d",&a);if(!q.empty()&&q.top()<a){ll tmp=q.top();q.pop();ans+=a-tmp;q.push(a);//两次存入队列cnt++;mp[a]++;if(mp[tmp]!=0)//中间商{cnt--;//交易次数不改变mp[tmp]--;}}q.push(a);}printf("%I64d %I64d\n",ans,cnt*2);}return 0;
}

http://www.ppmy.cn/news/682505.html

相关文章

markdown自动生成导航目录

把这一段代码插入到markdown生成的HTML文件的head标签中&#xff0c;将会自动根据markdown的标题按级别生成导航目录 <script src"http://code.jquery.com/jquery-1.7.2.min.js"></script> <script type"text/javascript"> //是否显示…

HDU 6447 YJJ's Salesman 【离散化+树状数组求区间最大】

传送门&#xff1a;HDU 6447 YJJs Salesman Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destina…

有关《家》的经典歌曲_【经典】50首诗词,做成50首歌曲,够你享受一整年!(收藏了慢慢听)...

原标题&#xff1a;【经典】50首诗词&#xff0c;做成50首歌曲&#xff0c;够你享受一整年&#xff01;(收藏了慢慢听) 一边痛饮一边唱着《将进酒》&#xff0c;该有多潇洒&#xff0c;当《关雎》化成歌声&#xff0c;是何等缱绻缠绵&#xff0c;当《送别》用现代歌曲唱出来&am…

古文诗词赋

古文诗词 人心惟危&#xff0c;道心惟微&#xff1b;惟精惟一&#xff0c;允执厥中。天行健&#xff0c;君子以自强不息。地势坤&#xff0c;君子以厚德载物。为天地立心&#xff0c;为生民立命&#xff0c;为往圣继绝学&#xff0c;为万世开太平。一、古文 滕王阁序 滕王阁…

硬件基本常识杂记1

文章目录 电感&#xff08;RL电路&#xff09;&#xff0c;电容&#xff08;RC电路&#xff09;&#xff0c;(LRC电路)谐振&#xff08;串联、并联&#xff09;滤波器&#xff08;高通RC、低通RC、高通RL、低通RL、带通、Π型&#xff09;积分电路、微分电路截至频率w信号传输、…

清华大学计算机夏文韬,太猛了--南京外国语学校2007届高三毕业生去向

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 南京外国语学校2007届高三毕业生去向 清华大学(15人) 黄声希、许多多、姜 楠、欧阳云、陈燕然、刘硕瑶、顾益玮、冯翔欣、刘 畅、刘雨霏、马 君、金 璐、周 逸、 陶雨洁、(陈莉茜) 北京大学(10人) 范心露、王靖、徐叶、韩锋…

数学计算机学具制作,神奇的数学

神奇的数学 神奇的数学1 好玩的数学&#xff0c; 加减乘除的运用&#xff0c; 就像奇妙的游戏&#xff0c; 带来无穷的乐趣&#xff0c; 数学真好玩。 奇妙的数学&#xff0c; 就像头脑一样&#xff0c; 知识永无止境&#xff0c; 有着魔力般的吸引力&#xff0c; 让人想成长。…

试论开源生态的经济模型

| 作者&#xff1a;庄表伟 | 编辑&#xff1a;金心悦 | 设计&#xff1a;朱亿钦 | 责编&#xff1a;王玥敏 缘起这是一个我一直想写&#xff0c;却一直没有想得非常清楚的课题。目标非常清楚&#xff1a;通过创建一种经济模型&#xff0c;来分析开源生态中的各种问题。上一次在…