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 ;
}
 

18CCPC网赛A 贪心的更多相关文章

  1. UVA LA 7146 2014上海亚洲赛(贪心)

    option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...

  2. ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

    Description There is a special number sequence which has n+1 integers. For each number in sequence, ...

  3. ccpc网赛 hdu6705 path(队列模拟 贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=6705 这是比赛前8题过的人数第二少的题,于是就来补了,但感觉并不难啊..(怕不是签到难度 题意:给个图,给几条路 ...

  4. hdu 4038 2011成都赛区网络赛H 贪心 ***

    贪心策略 1.使负数为偶数个,然后负数就不用管了 2.0变为1 3.1变为2 4.2变为3 5.若此时操作数剩1,则3+1,否则填个1+1,然后回到5

  5. hdu 4023 2011上海赛区网络赛C 贪心+模拟

    以为是贪心,结果不是,2333 贪心最后对自己绝对有利的情况 点我 #include<cstdio> #include<iostream> #include<algori ...

  6. 2017 ACM-ICPC西安网赛B-Coin

    B-Coin Bob has a not even coin, every time he tosses the coin, the probability that the coin's front ...

  7. UVALive 8519 Arrangement for Contests 2017西安区域赛H 贪心+线段树优化

    题意 等价于给一个数列,每次对一个长度为$K$的连续区间减一 为最多操作多少次 题解: 看样例猜的贪心,10分钟敲了个线段树就交了... 从1开始,找$[i,i+K]$区间的最小值,然后区间减去最小值 ...

  8. 2018宁夏邀请赛网赛 I. Reversion Count(java练习题)

    题目链接 :https://nanti.jisuanke.com/t/26217 Description: There is a positive integer X, X's reversion c ...

  9. HDU 4001 To Miss Our Children Time(2011年大连网络赛 A 贪心+dp)

    开始还觉得是贪心呢... 给你三类积木叫你叠楼房,给你的每个积木包括四个值:长 宽(可以互换) 高 类型d d=0:你只能把它放在地上或者放在 长 宽 小于等于 自己的积木上面 d=1:你只能把它放在 ...

随机推荐

  1. CodeIgniter框架学习要点

    以下内容从兄弟连的CI教学视频中摘抄: http://codeigniter.org.cn/tutorials/ ------------------------------------------- ...

  2. [原]零基础学习在Android进行SDL开发后记

    本着学习交流记录的目的编写了这个系列文章,主要用来记录如何从零开始学习SDL开发的过程,在这个过程中遇到了很多问题,差点就放弃了.首先是SDL的Android移植的时候遇到了比较坑的是SDL移植到An ...

  3. scanf函数读取缓冲区数据的问题

    标准I\O的缓冲类型 标准I\O根据不同的应用需求,提供了全缓冲.行缓冲.无缓冲三种缓冲方式. 全缓冲:只有当划定的缓冲区被填满或者数据读取至末尾时,才开始执行I\O操作(执行系统提供的read\wr ...

  4. 用npm-run自动化任务(转)

    自动构建JavaScript有不少好工具.不过其实很少有人知道,npm run命令就能很好地完成这一任务,配置起来也很简单. script npm会在项目的package.json文件中寻找scrip ...

  5. 泛型:上边界和通配符的使用以及对ArrayList的学习

      --------------- public class Wildcord { public static void main(String[] args) { /** * 类引用结构说明Pers ...

  6. BZOJ1002:[FJOI2007]轮状病毒(找规律,递推)

    Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子 和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下 ...

  7. JS常用方法封装

    迭代添加各种常用方法:项目中一定会有很多常用的方法,包括:取值,校验,等...... 获取 url 后的参数 function getQueryString(name) { var reg = new ...

  8. 4.spring:@Profile,AOP

    Profile: 可以根据当前的环境,动态激活和切换一系列的组件功能 指定组件在那个环境下才能被注册到容器中,不指定任何环境下都能注册到 1.加了环境标识的bean只有环境激活的时候才能注册到容器中 ...

  9. loading等待效果

    效果预览:这两个球一直在转,不能进行其他操作 div放在最外层 <div id="loadingImg" style="height: 100%;width: 10 ...

  10. Java 内部类综述

    转载自:https://blog.csdn.net/justloveyou_/article/details/53245561