2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438
Buy and Resell
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1233 Accepted Submission(s): 407
Problem Description
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
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
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
题意概括:
有 N 个商店,小商按顺序走过每个商店(不能回头),在每个商店它可以选择在那个商店以那个商店的价格买入或者卖出一件物品,当然他也可以选择不买不卖。小商的背包容量无限大。我需要做的是使得小商走过所有商店之后获得最大的利润以及他所花费的交易次数。
解题思路:
之前牛客暑假多校的一个变形,不只是能拿一个了,可以拿很多个。这道题的解法也是贪心,但是与之前那道很不一样。
首先他可以买多个商品,也就是说,他可能有反悔的机会,也就是说虽然他前面已经把那件商品卖掉了,但如果后面有比前面更优的选择,他其实可以反悔,之前不卖不出去而是等到更优的时候再卖。
而我们要做的就是为他提供一个可以反悔的机会。
这时候就需要用 STL 里的优先队列来维护一个最小堆了,而最小堆里面的是买入的商品(这里指的买入的商品其实更确切的说是加上交换得来的商品,但交换而来的商品要区别于直接花钱买的商品,所以要打上标记),堆顶的值是这些商品中价格最小的。
有了这个反悔神器,小商每到一家商店就可以把商店的交易价和堆顶的值做比较,如果比堆顶的值大的则买入(拿便宜的换贵的嘛),盈利值就是贵的和便宜的价钱之差,并把当前这家商店的商品丢进堆里,那么原本堆顶用于交换的那件呢怎么办呢?这就要看它是当时花钱买来的还是换来的了,如果是花钱买来的就直接不要啦并且操作数+1;如果是交换而来的,那么就去掉标记重新入堆(这种情况下我们发现其实它相当于一个中转物品了,这件贵的物品其实是跟交换它的那件物品进行交换了,而不是跟它,所以操作数不变并且要去掉标记)。如果当前商店的交易价比堆顶的要小呢,当然丢进堆里啦(反正现在丢进堆里又不用钱),如果后面有机会用高价换掉它的话又赚一波。所以有了这个时光机,商人可是稳赚不赔的啊。
AC code:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long int
using namespace std;
const int MAXN = 1e5+;
int N;
struct tp{
int x, v;
bool sell;
bool friend operator<(tp a, tp b)
{
if(a.v == b.v) return !a.sell;
return a.v > b.v;
}
};
int main()
{
int T_case, a;
scanf("%d", &T_case);
while(T_case--)
{
priority_queue<tp> Q;
scanf("%d", &N);
scanf("%d", &a);
tp temp;
temp.x = ;
temp.v = a;
temp.sell = false;
Q.push(temp);
ll ans = , time = ;
for(int i = ; i <= N; i++)
{
scanf("%d", &a);
temp.sell = false;
if(a > Q.top().v)
{
tp top = Q.top();
Q.pop();
ans+=a-top.v;
if(top.sell)
{
top.sell = false;
Q.push(top);
}
else time++;
temp.sell = true;
}
temp.x = i;
temp.v = a;
Q.push(temp);
}
printf("%lld %lld\n", ans, time*);
}
return ;
}
2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】的更多相关文章
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu6438 Buy and Resell 买入卖出问题 贪心
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1001 A water problem (大数取余)
Problem Descripton Two planets named Haha and Xixi in the universe and they were created with the un ...
- HDU - 6440 Dream 2018中国大学生程序设计竞赛 - 网络选拔赛
给定的\(p\)是素数,要求给定一个加法运算表和乘法运算表,使\((m+n)^p = m^p +n^p(0 \leq m,n < p)\). 因为给定的p是素数,根据费马小定理得 \((m+n) ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 Dream hdu6440 Dream 给出一个(流氓)构造法
http://acm.hdu.edu.cn/showproblem.php?pid=6440 题意:让你重新定义任意一对数的乘法和加法结果(输出乘法口诀表和加法口诀表),使得m^p+n^p==(m+n ...
- hdu6444 2018中国大学生程序设计竞赛 - 网络选拔赛 1007 Neko's loop
Neko's loop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 Solution
A - Buy and Resell 题意:给出n个交易点,每次能够选择买或者卖,求获得最大利润 思路:维护两个优先队列,一个是卖,一个是替换,当价格差相同时,优先替换,因为次数要最少 #includ ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 4 - Find Integer 【费马大定理+构造勾股数】
Find Integer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
随机推荐
- (转)Nginx静态服务配置---详解root和alias指令
Nginx静态服务配置---详解root和alias指令 原文:https://www.jianshu.com/p/4be0d5882ec5 静态文件 Nginx以其高性能著称,常用与做前端反向代理服 ...
- (转)python strip()函数 去空格\n\r\t函数的用法
原文:http://www.cnblogs.com/zdz8207/p/python_learn_note_20.html python3.4学习笔记(二十) python strip()函数 去空格 ...
- 关于跨域登录中获取COOKIES解析BUG
FormsAuthentication.Decrypt 报错 Length of the data to decrypt is invalid. 关于同域名不同服务器之间的登录,加密配置说明 ...
- 百度地图api-动态添加覆盖物
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- bzoj 5298: [Cqoi2018]交错序列
Description 我们称一个仅由0.1构成的序列为"交错序列",当且仅当序列中没有相邻的1(可以有相邻的0).例如,000,001 ,101,都是交错序列,而110则不是.对 ...
- lua_nginx_module用例
content_by_lua server { listen ; server_name lua.luckybing.top; location / { default_type 'text/plai ...
- 1.net平台
.net/dotnet:一般指的是.Net Framework框架,是一种平台,一种技术. .net Framewoek框架是.net平台不可缺少的一部分,它提供了一个稳定的运行环境来保证.net ...
- EF 取出demical数据,但需要去点小数,排序
try { BasePaperWeightDAL.Get(o => o.IsDeleted == false && o.IsEnabled == true).OrderByDes ...
- 【Linux】安装配置Tomcat7
第一步:下载Tomcat安装包 下载地址:https://tomcat.apache.org/download-70.cgi [root@localhost ~]# wget http://mirro ...
- cf1064D. Labyrinth(01BFS)
题意 题目链接 给出一个\(n \times m\)的网格,给出起始点,要求向左走不超过\(L\)步,向右走不超过\(R\)步,求出能遍历到哪些点 Sol 一个很直观的想法,BFS的时候状态里记录下还 ...