题目地址: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. 【WEB基础】HTML & CSS 基础入门(4)列表及其样式

    前面 网页中漂亮的导航.整齐规范的文章标题列表和图片列表等等.这些都是离不开HTML里一个重要的元素----列表,在HTML中有无序列表.有序列表和定义列表三种类型.其中,无序列表应用最为广泛,下面, ...

  2. Oracle数据库创建表空间及用户授权

    /*分为四步 */ /*第1步:创建临时表空间 */ create temporary tablespace test_temp tempfile 'E:\app\Administrator\orad ...

  3. 多线程 线程间通信 wait,notify

    1. 方法wait锁释放,notify()锁不释放

  4. Oracle 单引号与双引号的区别

    双引号一般是用来转义的,如果alias里面有空格或其它保留符号,必须使用双引号.而单引号是用来特制的,比如字符串的引用,日期字符串的引用,都必须包括在单引号中,可以参与运算或其它表达式中.两者不可混用 ...

  5. JSP | 基础 | 两种方式循环输出

    用循环连续输出三个你好,字体从小变大 第一种: <body> <%! //通过表达式方式调用实现 String HelloJSP1(){ String str = "&qu ...

  6. Qt基本应用

    1 使用方式 在qt designer中直接设计图形界面,然后使用pyGUI转换成py文件. 可以发现,转换的文件为一个class.并不是一个完整的程序(运行时无法出现窗口).这个类名字是Ui_Mai ...

  7. Codeforces Round #418 (Div. 2) B

    Description Sengoku still remembers the mysterious "colourful meteoroids" she discovered w ...

  8. hibernate Day1

    1 Web内容回顾(1) JavaEE三层架构web层(struts2框架)service层(spring框架)dao层(hibernate框架):负责对数据库进行CRUD操作(2) MVC模式(这是 ...

  9. h5-19-文件操作-文件域

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 【LeetCode 337 & 329. memorization DFS】House Robber III

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...