hdu3438 Buy and Resell(优先队列+贪心)
Buy and Resell
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2441 Accepted Submission(s): 924
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.
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.
4
1 2 10 9
5
9 5 9 10 5
2
2 1
5 2
0 0
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
#include<iostream>
#include<vector>
#include<queue>
#include<stdio.h>
#include<assert.h>
#include<functional>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
const ll mod=;
ll powmod(ll a,ll b) {ll res=;a%=mod; assert(b>=); for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
int T;
int n;
ll val;
int main()
{
cin>>T;
priority_queue<pll, vector<pll>, greater<pll> > que;
while(T--)
{
while(que.size()) que.pop();
cin>>n;
ll ans=;
ll num=;
for(int i=;i<n;i++)
{
cin>>val;
if(que.size()&&que.top().fi<val)
{
num++;
pll tmp=que.top(); que.pop();
ans+=val-tmp.fi;
if(tmp.se==)num--;
else num++;
que.push(mp(val,));
que.push(mp(val,));
}
else
que.push(mp(val,));
}
cout<<ans<<" "<<num<<endl;
}
return ;
}
hdu3438 Buy and Resell(优先队列+贪心)的更多相关文章
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- HDU 6438"Buy and Resell"(贪心+优先级队列)
传送门 •参考资料 [1]:HDU6438(优先队列+思维) •题意 有n个城市,第 i 天你会达到第 i 个城市: 在第 i 个城市中,你可以用 ai 元购买一个物品,或者用 ai 元卖掉一个物品, ...
- HDU6438:Buy and Resell(贪心+数据结构)
题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少 分析:对每一个元素产生的贡献可以先计算 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...
- hdu6438 Buy and Resell 买卖物品 ccpc网络赛 贪心
题目传送门 题目描述: 有n座城市,每座城市都可以对一个物品进行一次的买进或者卖出,可以同时拥有多个物品,计算利润最大值,并且交易次数要最少.(买入卖出算两次操作) 思路: 建立两个小根堆 优先队列, ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu6438 Buy and Resell 买入卖出问题 贪心
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 最高的奖励 - 优先队列&贪心 / 并查集
题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...
- POJ2431 优先队列+贪心 - biaobiao88
以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...
- HDU6438 Buy and Resell 解题报告(一个有趣的贪心问题的严格证明)
写在前面 此题是一个很容易想到的贪心题目,但是正确性的证明是非常复杂的.然而,目前网上所有题解并未给出本题贪心算法的任何正确性证明,全部仅停留在描述出一个贪心算法.本着对算法与计算机科学的热爱(逃), ...
随机推荐
- Linux统计文件内容
wc:统计文件的行数.单词数.字节数(word count) - wc char.txt:统计出文件char.txt的换行符个数.单词数.字节数 (char.txx有14行.13个单词.66字节) - ...
- 安装Sublime Text 3 的过程
在Sublime Text官网下载最新的APP https://www.sublimetext.com 我下载的是 3143 安装之后开始在网上找注册码 我用了成功的注册码的是 这个应该是单个用户的, ...
- fputc, fputs, putc, putchar, puts - 输出字符和字符串
总览 (SYNOPSIS) #include <stdio.h> int fputc(int c, FILE *stream); int fputs(const char *s, FILE ...
- Git--07 Gitlab备份与恢复
目录 Gitlab备份与恢复 01). 备份 02). 恢复 Gitlab备份与恢复 对gitlab进行备份将会创建一个包含所有库和附件的归档文件.对备份的恢复只能恢复到与备份时的gitlab相同 ...
- BZOJ2097 [Usaco2010 Dec]Exercise 奶牛健美操 贪心
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2097 题解 显然二分一个 \(mid\) 表示每一块的直径长度的最大值,求最少需要多少连通块. ...
- Ubuntu14.04安装Ruby2.2方法
直接使用系统的sudo apt-get install ruby2.0安装后,ruby -v显示ruby的版本依然是ruby 1.9. 以下方法可以顺序地在Ubuntu14.04安装Ruby2.2 s ...
- TypeError:NoneType object is not callable情况下的错误
只用在该视图函数下面加上return ‘值’
- PWA 应用
1. 使用例子,vue官网,在手机浏览器器打开时,保存在桌面那个应用.还有饿了么网站也是PWA应用.
- Win7、Win10、Linux局域网文件共享
目录 1.WIN7系统局域网文件共享 2.WIN10系统局域网文件共享 3.Linux 局域网文件共享 WIN7系统局域网文件共享 步骤一: 打开网络和共享中心----更改高级共享设置 步骤二: 选择 ...
- python的map、reduce和filter(过滤器)函数(廖雪峰老师python基础)
1.map 语法: map(func,Iterable) map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返 ...