Buy and Resell

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

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

 
Source
题意:给出 n ,表示 n 天。给出 n 个数,a[i] 表示第 i 天,物品的价格是多少。每天可以选择买一个物品,或者卖一个已有物品,也可以什么都不做,问最后最大能赚多少钱,最少操作次数是多少?
思路:要想收入最高一定是在高价卖,低价买,如果我们在遇见一个高价就卖了,但是后面可能会有更高的价格,所以每当遇到一个高价,我们就直接卖,等后面遇到更高的价格在更新。
代码:
 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3fffffffffffffff;
int n;
struct P {
int x, id;
P(int a, int b) : x(a), id(b) {};
bool operator<(const P &a) const {
if (x == a.x) return id < a.id;//反向重载
return x > a.x;
}
}; int main() {
int T;
ci(T);
while (T--) {
ci(n);
priority_queue<P> q;
ll ans = , cnt = ;
for (int i = , x; i < n; i++) {
ci(x);
if (!q.empty() && q.top().x < x) {
P tmp = q.top();q.pop();
ans += x - tmp.x;
if (!tmp.id) cnt++;
q.push(P(x, ));//以x的价格卖出过,以后可能还会以更高的价格卖
}
q.push(P(x, ));//要以x的价格买入
}
printf("%lld %lld\n", ans, * cnt);
}
return ;
}
 

最新文章

  1. 将Resource中的图片资源动态绑定到PictureBox中:
  2. JQGrid 学习1
  3. iOS学习笔记—ViewController/生命周期
  4. Hibernate,JPA注解@Embeddable
  5. 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
  6. CSS有用的代码片段
  7. 【BZOJ2721】樱花(数论)
  8. 什么是IIS ?
  9. C# HtmlAgilityPack 爬虫框架
  10. 健壮程序之--SQL优化
  11. zombodb 几点说明
  12. 详述socket编程之select()和poll()函数
  13. 稍稍解读下JDK8的HashMap
  14. ODAC(V9.5.15) 学习笔记(五)TSmartQuery
  15. GPS坐标定位与距离计算
  16. HDU - 5547 数独(回溯法)
  17. 如何删除 Windows 10 系统生成的 WindowsApps 文件夹
  18. [CF1065C]Make It Equal
  19. DNS分别在什么情况下使用UDP和TCP?
  20. maya2016安装失败如何卸载重装

热门文章

  1. python全栈学习笔记(二)网络基础之子网划分
  2. Hadoop学习---Zookeeper+Hbase配置学习
  3. 沉淀,再出发:Git的再次思考
  4. dbms_stats应用相关
  5. CMake是用于生成make文件的跨平台编译文件
  6. 马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与 分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴CEO的马云自曝了他第一次上电视是在1995年。“我刚开始创
  7. jemter 使用if控制器,选择需要的内容
  8. Struts2.3.4.1 + Spring3.1.2 + Hibernate4.1.6整合
  9. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】
  10. BZOJ 3289: Mato的文件管理 【莫队 + 树状数组】