18CCPC网赛A 贪心

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

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

题意:给出 n ,表示 n 天。给出 n 个数,a[i] 表示第 i 天,物品的价格是多少。每天可以选择买一个物品,或者卖一个已有物品,也可以什么都不做,问最后最大能赚多少钱,最少操作次数是多少?

思路:要想收入最高一定是在高价卖,低价买,如果我们在遇见一个高价就卖了,但是后面可能会有更高的价格,所以每当遇到一个高价,我就直接卖,等后面遇到更高的价格在更新。

代码:

 1 #include"bits/stdc++.h"
 2
 3 #define db double
 4 #define ll long long
 5 #define vl vector<ll>
 6 #define ci(x) scanf("%d",&x)
 7 #define cd(x) scanf("%lf",&x)
 8 #define cl(x) scanf("%lld",&x)
 9 #define pi(x) printf("%d\n",x)
10 #define pd(x) printf("%f\n",x)
11 #define pl(x) printf("%lld\n",x)
12 #define rep(i, a, n) for (int i=a;i<n;i++)
13 #define per(i, a, n) for (int i=n-1;i>=a;i--)
14 #define fi first
15 #define se second
16 using namespace std;
17 typedef pair<int, int> pii;
18 const int N = 1e5 + 5;
19 const int mod = 1e9 + 5;
20 const int MOD = 992353;
21 const db PI = acos(-1.0);
22 const db eps = 1e-10;
23 const int inf = 0x3f3f3f3f;
24 const ll INF = 0x3fffffffffffffff;
25 int n;
26 struct P {
27     int x, id;
28     P(int a, int b) : x(a), id(b) {};
29     bool operator<(const P &a) const {
30         if (x == a.x) return id < a.id;//反向重载
31         return x > a.x;
32     }
33 };
34
35 int main() {
36     int T;
37     ci(T);
38     while (T--) {
39         ci(n);
40         priority_queue<P> q;
41         ll ans = 0, cnt = 0;
42         for (int i = 0, x; i < n; i++) {
43             ci(x);
44             if (!q.empty() && q.top().x < x) {
45                 P tmp = q.top();q.pop();
46                 ans += x - tmp.x;
47                 if (!tmp.id) cnt++;
48                 q.push(P(x, 1));//以x的价格卖出过,以后可能还会以更高的价格卖
49             }
50             q.push(P(x, 0));//要以x的价格买入
51         }
52         printf("%lld %lld\n", ans, 2 * cnt);
53     }
54     return 0;
55 }

原文地址:https://www.cnblogs.com/mj-liylho/p/9567146.html

时间: 2024-08-02 18:13:19

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

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

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

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosmsg=Submission+received+with+ID+1708713 /** UVA LA 7146 2014上海亚洲赛(贪心) 题目大意:给定敌我双方士兵的数量和每个士兵的攻击力和防守力,如果两个士兵对战,一方

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

Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j( i ≠ j ) For sequence a and sequence b, the integrating degree t is defined as follows(“?” denotes exclus

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

以为是贪心,结果不是,2333 贪心最后对自己绝对有利的情况 点我 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 10000000

[CSP-S模拟测试]:赛(贪心+三分)

题目描述 由于出题人思维枯竭所以想不出好玩的背景.有$n$个物品,第$i$个物品的价格是$v_i$,有两个人,每个人都喜欢$n$个物品中的一些物品.要求选出正好$m$个物品,满足选出的物品中至少有$k$个物品被第一个人喜欢,$k$个物品被第二个人喜欢.并求出最小的价格和. 输入格式 第一行三个数$n,m,k$.第二行$n$个数,第$i$个数表示$v_i$.第三行包含一个数$a$,表示第一个人喜欢的物品数.第四行包含$a$个数,表示第一个人喜欢的物品是哪几个.第五行包含一个数$b$,表示第二个人喜

hdu 5025 BFS + 优化 广东区域赛网赛

http://acm.hdu.edu.cn/showproblem.php?pid=5025 TLE了好几次 写的时候,问题在于, 1.钥匙怎么处理 参考了他人的写法,vis[key_num][i][j],相当于将图多维化,这样就可以判重了,否则只是按照普通的BFS,钥匙数不同的时候,可以重复,这个代码难易表达出来 另外此处一个很好的优化,当cost_now<cost[key_now][x_now][y_now]的时候,才q.push(Node)  这个剪枝很好啊 2.蛇怎么处理 没蛇的话,第一

【转】HDU 6194 string string string (2017沈阳网赛-后缀数组)

转自:http://blog.csdn.net/aozil_yang/article/details/77929216 题意: 告诉你一个字符串和k , 求这个字符串中有多少不同的子串恰好出现了k 次. 思路: 后缀数组. 我们先考虑至少出现k 次的子串, 所以我们枚举排好序的后缀i (sa[i]) . k段k 段的枚举. 假设当前枚举的是 sa[i]~sa[i + k -1] 那么假设这一段的最长公共前缀  是L 的话. 那么就有L 个不同的子串至少出现了k次. 我们要减去至少出现k + 1次

CCPC网赛,HDU5832_ A water problem

Problem Description Two planets named Haha and Xixi in the universe and they were created with the universe beginning.          There is 73 days in Xixi a year and 137 days in Haha a year.           Now you know the days N after Big Bang, you need to

hdu5438 dfs+并查集 长春网赛

先dfs对度小于2的删边,知道不能删为止. 然后通过并查集来计算每一个分量里面几个元素. #include<iostream> #include<cstring> #include<vector> #define maxn 10010 #define LL __int64 using namespace std; int in[maxn],vis[maxn],p,pa[maxn],cou[maxn]; LL sum[maxn]; vector<int>mp[