题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6438

获得最大的利润,将元素依次入栈,期中只要碰到比队顶元素大的,就吧队顶元素卖出去,答案加上他们期中的差值,并把新加入的元素用map标记为中间变量,若以后再卖出这件物品,可看做直接由之前的最小值卖出,而该中间变量重新入队,当做从未买卖过;

因为买入=买出,故输出只需将times*2即可;

也可在每个元素入队和交易最小变量时都times++,结果输出时减去队内元素数目即可;

 #include<iostream>
#include<cstdio>
#include<queue>
#include<map>
using namespace std; #define LL long long
const int N = 1e5+;
int T, n;
priority_queue<LL, vector<LL>, greater<LL>> Q;
map<LL, int> vis; int main()
{
scanf("%d", &T);
while(T--)
{
vis.clear();
while(!Q.empty()) Q.pop();
scanf("%d", &n); LL times=, ans=;
for(int x,i=; i<n; i++) {
scanf("%d", &x);
Q.push(x);
LL minx = Q.top();
if(x <= minx) continue; Q.pop();
ans += x-minx; times++;
vis[x]++;
if(vis[minx] > ) {
vis[minx]--;
Q.push(minx);
times--;
}
}
printf("%lld %lld\n", ans, times*);
}
return ;
}

【2018 CCPC网络赛】1001 - 优先队列&贪心的更多相关文章

  1. 2018 CCPC网络赛

    2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物 ...

  2. HDU 6438 Buy and Resell ( 2018 CCPC 网络赛 && 贪心 )

    题目链接 题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少? 分析 : 和 CF 867 ...

  3. 2018 CCPC 网络赛 Buy and Resell

    The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed ...

  4. 2018 CCPC 网络赛

    The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed ...

  5. 2018 CCPC网络赛 几道数学题

    1002 Congruence equation 题目链接  : http://acm.hdu.edu.cn/showproblem.php?pid=6439 题解 : https://www.zyb ...

  6. 2018 CCPC网络赛 hdu6444 Neko's loop

    题目描述: Neko has a loop of size n.The loop has a happy value ai on the i−th(0≤i≤n−1) grid. Neko likes ...

  7. 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...

  8. 【2018 CCPC网络赛 1004】Find Integer(勾股数+费马大定理)

    Problem Description people in USSS love math very much, and there is a famous math problem . give yo ...

  9. 【2018 CCPC网络赛】1009 - 树

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6446 题目给出的数据为一棵树,dfs扫描每条边,假设去掉某条边,则左边 x 个点,右边 n-x 个点, ...

随机推荐

  1. EOJ3263:丽娃河的狼人传说(贪心)

    传送门 题意 分析 考虑将区间按右端点排序,再遍历区间,操作即可 建议以加方式写 trick 1.不需要判区间重合 代码 #include<cstdio> #include<cstr ...

  2. python __builtins__ property类 (55)

    55.'property',  获取对象的所有属性 class property(object) | property(fget=None, fset=None, fdel=None, doc=Non ...

  3. layui开始时间小于结束时间

    直接上代码 // var endDate= laydate.render({ // elem: '#end_time',//选择器结束时间 // type: 'datetime', // min:&q ...

  4. bzoj1878 [SDOI2009]HH的项链【莫队】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1878 以每个询问左端点所属的块的编号为第一关键字,右端点本身为第二关键字,排序,然后保利扫描 ...

  5. SPFA/Dijkstra POJ 3013 Big Christmas Tree

    题目传送门 题意:找一棵树使得造价最少,造价为每个点的子节点造价和*边的造价和 分析:最短路跑出1根节点到每个点的最短边权值,然后每个点的权值*最短边距和就是答案,注意INF开足够大,n<=1特 ...

  6. 维骨力Glucosamine的最关键的几点...

    1.每日劑量應為多少?長期服用安全嗎? 由於葡萄糖胺(Glucosamine)和軟骨素(Chondroitin)原來就存在於人體,是人體每天會生產製造的必需營養素,因此,一般認為服用此類產品的安全性相 ...

  7. NTP服务简介

    定义:NTP全称为Network Time Protocol,即网络时间协议.是用来使计算机时间同步的一种协议.它可以使计算机对服务器或时钟源做同步,可以提供高精度的时间校正(LAN 上与标准时间小于 ...

  8. Linux下用matplotlib画决策树

    1.trees = {'no surfacing': { 0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}} 2.从我的文件trees.txt里读的决策树, ...

  9. Android图片压缩上传(二)

    之前有用到libjpeg,还是有一定的局限性,最近用了一个新的方式,效果还是挺不错,随着作者的版本更新,Bug也随之变少,目前项目中运用已上线. 1.之前的方式Android图片压缩,不失真,上线项目 ...

  10. mac下安装nodejs

    下载 https://nodejs.org/en/ 安装 一步步继续就ok 验证 npm -v node -v Done!