Buy and Resell

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1126    Accepted Submission(s): 359

Problem Description
The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed to trade it. The trading price of the Power Cube in the i-th city is ai dollars per cube. Noswal is a foxy businessman and wants to quietly make a fortune by buying and reselling Power Cubes. To avoid being discovered by the police, Noswal will go to the i-th city and choose exactly one of the following three options on the i-th day:

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
There are multiple test cases. The first line of input contains a positive integer T (T≤250), indicating the number of test cases. For each test case:
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
For each case, print one line with two integers —— the maximum profit and the minimum times of trading to get the maximum profit.
 
Sample Input
3
4
1 2 10 9
5
9 5 9 10 5
2
2 1
 
Sample Output
16 4
5 2
0 0

Hint

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个点,每个点都有自己的价值val[i],我们可以在每个点买入和卖出,如在A点买入B点卖出,我的收益是val[B]-val[A],怎样操作我的收益最大?
分析:在每一个点上进行卖出操作,将val[i]放进优先级队列q(已负数形式加入相当于最小的在最上面),则每一次我卖出的收益为val[i]-q.top()
   当前面没有比这个点价值小的点时我会卖出这个点,这样存入的点要买入都会先卖出一次然后才能在遇到更大的价值时再买入一次,所以每个点存入两次
   这样我可以求出所得的最大利益
   现在求最大利益下的买入卖出次数,对每个push进队列的数我们打一个标记,1代表第一次卖出这个数,0代表第二次买入这个数
   由上诉分析可以得到:当我买入一个数时,也就代表了我进行了买入卖出一共两次操作
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e4+10;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
const double pi = acos(-1.0);
priority_queue<pair<ll,ll> > q;
int main() {
ll T, n;
scanf("%lld",&T);
while( T -- ) {
scanf("%lld",&n);
ll ans = 0, cnt = 0;
while(!q.empty()) {
q.pop();
}
for( ll i = 0, x; i < n; i ++ ) {
scanf("%lld",&x);
q.push(make_pair(-x,0)), q.push(make_pair(-x,1));
ans += x + q.top().first;
if( !q.top().second ) {
cnt += 2;
}
q.pop();
}
printf("%lld %lld\n",ans,cnt);
}
return 0;
}

  

2018中国大学生程序设计竞赛 - 网络选拔赛 hdu6438 Buy and Resell 买入卖出问题 贪心的更多相关文章

  1. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

  2. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...

  3. 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. HDU - 6440 Dream 2018中国大学生程序设计竞赛 - 网络选拔赛

    给定的\(p\)是素数,要求给定一个加法运算表和乘法运算表,使\((m+n)^p = m^p +n^p(0 \leq m,n < p)\). 因为给定的p是素数,根据费马小定理得 \((m+n) ...

  5. 2018中国大学生程序设计竞赛 - 网络选拔赛 Dream hdu6440 Dream 给出一个(流氓)构造法

    http://acm.hdu.edu.cn/showproblem.php?pid=6440 题意:让你重新定义任意一对数的乘法和加法结果(输出乘法口诀表和加法口诀表),使得m^p+n^p==(m+n ...

  6. 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 ...

  7. 2018中国大学生程序设计竞赛 - 网络选拔赛 Solution

    A - Buy and Resell 题意:给出n个交易点,每次能够选择买或者卖,求获得最大利润 思路:维护两个优先队列,一个是卖,一个是替换,当价格差相同时,优先替换,因为次数要最少 #includ ...

  8. 2018中国大学生程序设计竞赛 - 网络选拔赛 4 - Find Integer 【费马大定理+构造勾股数】

    Find Integer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  9. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu 6440 Dream 模拟

    Dream Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

随机推荐

  1. 【Algorithm】插入排序法

    通常人们整理桥牌的方法是一张一张的来,将每一张插入到其他已经有序的牌中的适当位置. • 思想:每步将一个待排序的记录,按其顺序码大小插入到前面已经排序的序列的合适位置,直到全部插入排序完为止. Jav ...

  2. go interface衍生的插件化处理

    在设计程序的许多应用场景中我们会遇到大体分为三个阶段的任务流. 第一.入口 一个或多个入口,等待阻塞的.或者主动请求方式的. ============================== 比如任务流需 ...

  3. Linux部署项目遇到问题解决

    使用Linux部署web项目,可能会遇到各种各样问题导致服务启动失败,以下是我近期部署项目遇到的问题以及解决方案 一.场景:把war包放入tomcat的webapps文件夹下,然后启动tomcat服务 ...

  4. vue-cli项目下引入vant组件

    前言 Vant是有赞前端团队基于有赞统一的规范实现的 Vue 组件库,提供了一整套 UI 基础组件和业务组件.通过 Vant,可以快速搭建出风格统一的页面,提升开发效率.目前已有近50个组件,这些组件 ...

  5. 关于stm32f1使用ST官方DSP库中的FFT方法

    先分享一下ST官方基于F1的DSP固件库,自从ST推出F4系列的DSP固件库之后,网上好像很难找到原来F1系列的DSP固件库了. 链接:https://pan.baidu.com/s/1S5h2Ti3 ...

  6. Linux--shell重定向与文件处理命令--02

    一.IO重定向 1.数据输入:键盘---标准输入,但并不是唯一输入方式 ” | passwd –stdin username #同时添加用户和密码 while line;do 循环体...$line ...

  7. ES 27 - Elasticsearch脚本的使用实践

    目录 1 关于脚本 2 脚本使用的最佳实践 2.1 创建脚本并存储 2.2 脚本的缓存 2.3 Script Field - 脚本字段 本文以 ES 6.6.0 版本为例进行演示. 1 关于脚本 ES ...

  8. Unable to load template file 'rj\ThinkPHP/Tpl/dispatch_jump.tpl'----thinkphp3.2.3

    Unable to load template file 'rj\ThinkPHP/Tpl/dispatch_jump.tpl'----thinkphp3.2.3 1.报错原因:将thinkphp默认 ...

  9. gin+vue的前后端分离开源项目

    该项目是gin+vue的前后端分离项目,使用gorm访问MySQL,其中vue前端是使用vue-element-admin框架简单实现的: go后台使用jwt,对API接口进行权限控制.此外,Web页 ...

  10. net core Webapi基础工程搭建(七)——小试AOP及常规测试_Part 1

    目录 前言 拦截器 异常拦截器 测试结果 身份验证拦截器 测试 小结 补充 2019-07-31 前言 一天天不知道怎么过的,但确实挺忙,事赶事不带停那种,让我感觉跟在流水线干活一样,忙活的事差不多了 ...