【2018 CCPC网络赛】1001 - 优先队列&贪心
题目地址: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 - 优先队列&贪心的更多相关文章
- 2018 CCPC网络赛
2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物 ...
- HDU 6438 Buy and Resell ( 2018 CCPC 网络赛 && 贪心 )
题目链接 题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少? 分析 : 和 CF 867 ...
- 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 ...
- 2018 CCPC 网络赛
The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed ...
- 2018 CCPC网络赛 几道数学题
1002 Congruence equation 题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=6439 题解 : https://www.zyb ...
- 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 ...
- 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 ...
- 【2018 CCPC网络赛 1004】Find Integer(勾股数+费马大定理)
Problem Description people in USSS love math very much, and there is a famous math problem . give yo ...
- 【2018 CCPC网络赛】1009 - 树
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6446 题目给出的数据为一棵树,dfs扫描每条边,假设去掉某条边,则左边 x 个点,右边 n-x 个点, ...
随机推荐
- 如何实现session的共享?
1.以cookie加密的方式保存在客户端. 优点是减轻服务器端的压力 缺点是受到cookie的大小限制,可能占用一定带宽,因为每次请求会在头部附带一定大小的cookie信息,另外这种方式在用户禁止使用 ...
- 51nod 1433【数学】
思路: 不晓得阿,n%9==0即n数值各个位加起来要%9==0: 如果知道这个,那么%90==0就是末尾多个0就好了,那么后面就是随便搞吧: #include <stdio.h> #inc ...
- hdoj1003【DP】
这道题目的DP,写到现在才明白... 每次加或者不加的条件就是这个前面这个子序列合是不是大于等于0,如果不是加了就会让这个位置的值没有意义,如果大于0,他还是在往递增的方向继续前进. 以及这个条件的继 ...
- python 处理时间和日期
转自: https://www.cnblogs.com/65702708/archive/2011/04/17/2018936.html Python格式化日期时间的函数为datetime.datet ...
- python 中site-packages 和 dist-packages的区别
dist-packages is a Debian-specific convention that is also present in its derivatives, like Ubuntu. ...
- 仅用递归函数操作逆序一个栈(Swift 4)
/// 取出栈底的数 func getAndRemoveLastElement(_ items: inout [Int]) -> Int { let value = items.remove(a ...
- JSP | 基础 | 在同一表单中提交两个不同的action
通过与跟JS配合使用实现需求 <head> <title>Chat Room</title> <script type="text/javascri ...
- C++构造函数与析构函数的解析
创建一个对象时,常常需要作某些初始化的工作,例如对数据成员赋初值. 注意,类的数据成员是不能在声明类时初始化的.如果一个类中所有的成员都是公用的,则可以在定义对象时对数据成员进行初始化.如: clas ...
- 523 Continuous Subarray Sum 非负数组中找到和为K的倍数的连续子数组
非负数组中找到和为K的倍数的连续子数组 详见:https://leetcode.com/problems/continuous-subarray-sum/description/ Java实现: 方法 ...
- 前端打印console
很多时候,我们都想知道,是否已经选中或得到数据时,我们可以利用console 打印出来.console有几种方式使用.具体有: console.log($scope.getParkId); conso ...