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

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

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. #include<map>
  5. using namespace std;
  6.  
  7. #define LL long long
  8. const int N = 1e5+;
  9. int T, n;
  10. priority_queue<LL, vector<LL>, greater<LL>> Q;
  11. map<LL, int> vis;
  12.  
  13. int main()
  14. {
  15. scanf("%d", &T);
  16. while(T--)
  17. {
  18. vis.clear();
  19. while(!Q.empty()) Q.pop();
  20. scanf("%d", &n);
  21.  
  22. LL times=, ans=;
  23. for(int x,i=; i<n; i++) {
  24. scanf("%d", &x);
  25. Q.push(x);
  26. LL minx = Q.top();
  27. if(x <= minx) continue;
  28.  
  29. Q.pop();
  30. ans += x-minx; times++;
  31. vis[x]++;
  32. if(vis[minx] > ) {
  33. vis[minx]--;
  34. Q.push(minx);
  35. times--;
  36. }
  37. }
  38. printf("%lld %lld\n", ans, times*);
  39. }
  40. return ;
  41. }

【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. E20170512-hm

    implicit  adj. 不言明[含蓄]的; 无疑问的, conversion  n. 变换,转变; precision n. 精确度,  adj. 精确的, with precision 准确地 ...

  2. Moctf--Pubg题目

    假期举办的一场比赛,开始的题目比较基础,misc神马的都还好说,就是web有些坑了,比如我今天要写的这一道题目.不过大佬说很简单..不过最后我还是解出来了,把思路放在这里. 打开之后得到这个页面,在网 ...

  3. P5154 数列游戏(区间dp)

    传送门 果然和dp有关的东西我绝对做不出来啊-- 设\(dp[i][j]\)表示消完区间\([i,j]\)中的数之后能得到的最大分数,如果消不完则为\(-inf\),否则枚举断点.顺便如果\(a[i] ...

  4. web移动端下拉加载数据简单实现

    //下拉加载在移动端会经常使用,有些小伙伴不清楚一些原理下面就简答的介绍一下 //首先需要监听window的滚动事件,下拉其实就是在监听window滚动事件 var pageNum = 1;//分页第 ...

  5. 5.replace的用法,while循环,continue

    1.relace s='dadk12' print(s.replace('生活','dd'))==打印返回dadk12,即使没有也不会报错. 2.while continue while循环输出 1 ...

  6. the little schemer 笔记(2)

    第二章 Do it, Do it Again, and Again, and Again... 假设l是 (Jack Sprat could eat no chicken fat) 那么 (lat? ...

  7. Python while 1 和 while True 速度比较

    References http://legacy.python.org/dev/peps/pep-0285/http://stackoverflow.com/questions/3815359/whi ...

  8. JDBC全部分析

    2.1.搭建实验环境 1.在mysql中创建一个库,并创建user表和插入表的数据. SQL脚本如下: 1 create database jdbcStudy character set utf8 c ...

  9. PHP pack和unpack函数详解

    pack 压缩资料到位字符串之中. 语法: string pack(string format, mixed [args]...); 返回值: 字符串 函数种类: 资料处理 内容说明 本函数用来将资料 ...

  10. angularjs之ng-mode获取lobject类型里的键值

    有时候数据库定义的时候,用一个对象来代表某个属性,之后直接访问对象就可以获取全部该对象的属性,但是有时需求访问对象中包含中的键值,引用键值的时候可以直接用.来获取对象的键值,比如 对象points: ...