Hackerrank--Stock Maximize(DP Practice)

题目链接

Your algorithms have become so good at predicting the market that you now know what the share price of Wooden Orange Toothpicks Inc. (WOT) will be for the next N days.

Each day, you can either buy one share of WOT, sell any number of shares of WOT that you own, or not make any transaction at all. What is the maximum profit you can obtain with an optimum trading strategy?

Input

The first line contains the number of test cases T. T test cases follow:

The first line of each test case contains a number N. The next line contains N integers, denoting the predicted price of WOT shares for the next N days.

Output

Output T lines, containing the maximum profit which can be obtained for the corresponding test case.

Constraints

1 <= T <= 10 
1 <= N <= 50000

All share prices are between 1 and 100000

Sample Input

3
3
5 3 2
3
1 2 100
4
1 3 1 2

Sample Output

0
197
3

Explanation

For the first case, you cannot obtain any profit because the share price never rises. 
For the second case, you can buy one share on the first two days, and sell both of them on the third day. 
For the third case, you can buy one share on day 1, sell one on day 2, buy one share on day 3, and sell one share on day 4.

经典的dp题目,每天可以买进一份股票,或者卖出任意份的股票(当然你要有这么多份股票),或者什么也不做。

首先,如果你在某一天买一份股票,那么这一天的股票价格一定不是从当天起到最后一天的最大值,想一想。

其次,如果选择在某一天卖出若干份股票,那么一定是将所有的股票都卖出才是最优策略。

最后,如果选择在某一天卖出股票,那么当天的股票价格一定是从当天起到最后一天的最大值。

Accepted Code:

 1 #include <iostream>
 2 using namespace std;
 3
 4 typedef long long LL;
 5 const int maxn = 50002;
 6 int a[maxn];
 7
 8 int main(void) {
 9     ios::sync_with_stdio(false);
10     int T;
11     cin >> T;
12     while (T--) {
13         int n;
14         cin >> n;
15         for (int i = 1; i <= n; i++) cin >> a[i];
16         int max_price = -1;
17         LL ans = 0;
18         for (int i = n; i >= 1; i--) {
19             max_price = max(max_price, a[i]);
20             ans += max_price - a[i];
21         }
22         cout << ans << endl;
23     }
24
25     return 0;
26 }

Hackerrank--Stock Maximize(DP Practice),布布扣,bubuko.com

时间: 2024-10-13 01:29:52

Hackerrank--Stock Maximize(DP Practice)的相关文章

HackerRank - &quot;Stock Maximize&quot;

First I thought it should be solved using DP, and I gave a standard O(n^2) solution: #include <iostream> #include <vector> #include <algorithm> #include <numeric> using namespace std; #define REP(i, s, n) for(int i = s; i < n; i

LeetCode-Best Time to Buy and Sell Stock III[dp]

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

Educational Codeforces Round 19 B. Odd sum(贪心或dp)

题意:给出一组数,从中拿出几个,要让它们之和最大并且为奇数. 这道题给出的n不大,贪心暴力一下就可以了.(-?-;) 1.贪心 我是先把数据大于0并且为偶数的数都先加起来(保证开始的sum是偶数),数据大于0且为奇数的存在a数组里,数据小于0的存在b数组里. 然后如果有a数组有奇数个,直接加起来输出就好了.奇*奇=奇 偶数个的话就从sum中拿出a1,b1.特判一下a1(a数组里面最小的那个)和b1(b数组里最大的那个)哪个的绝对值大. a1<b1 直接输出之前的sum,a1>=b1则输出sum

CFA一级知识点总结

Ethics 部分 Objective of codes and standard:永远是为了maintain public trust in 1. Financial market 2. Investment profession 6个code of ethics 1. Code 1—ethics and pertinent d persons a. 2. Code 2---primacy of client’s interest a. Integrity with investment pr

POJ3903 5thweek.problem_E LIS

Description The world financial crisis is quite a subject. Some people are more relaxed while others are quite anxious. John is one of them. He is very concerned about the evolution of the stock exchange. He follows stock prices every day looking for

面试10大算法汇总

英文版 以下从Java角度解释面试常见的算法和数据结构:字符串,链表,树,图,排序,递归 vs. 迭代,动态规划,位操作,概率问题,排列组合,以及一些需要寻找规律的题目. 1. 字符串和数组 字符串和数组是最常见的面试题目类型,应当分配最大的时间.关于字符串,首先需要注意的是和C++不同,Java字符串不是char数组.没有IDE代码自动补全功能,应该记住下面的这些常用的方法. toCharArray() //获得字符串对应的char数组 Arrays.sort() //数组排序 Arrays.

基本 Python 面试问题

基本 Python 面试问题 * 1. Python 中的列表和元组有什么区别? * 2. Python 的主要功能是什么? * 3. python 是编程语言还是脚本语言? * 4. Python 是一种解释性语言吗? * 5. 什么是 pep 8? * 6. python 是如何进行内存管理的? * 7. 什么是 Python 的命名空间? * 8. python 语言中 PYTHONPATH 是什么? * 9. 什么是 python 模块?在 Python 中命名一些常用的内置模块? *

有人男妓

http://quote.hexun.com/stock/icb.aspx?code=1&name=%A8%7B%C6%BD%C1%B9%C4%C4%C0%EF%D3%D0%C7%E8%BB%AF%CE%EF%C2%F2%A3%D1%3A%A3%B1%A3%B1%A3%B2%A3%B7%A3%B4%A3%B0%A3%B1%A3%B1%A3%B7%A3%B5 http://quote.hexun.com/stock/icb.aspx?code=1&name=%A8%7C%D5%C5%D2%B

poj3903 Stock Exchange 二分+dp

题目地址:http://poj.org/problem?id=3903 题目: Description The world financial crisis is quite a subject. Some people are more relaxed while others are quite anxious. John is one of them. He is very concerned about the evolution of the stock exchange. He fo