题目链接

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:

 #include <iostream>
using namespace std; typedef long long LL;
const int maxn = ;
int a[maxn]; int main(void) {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
for (int i = ; i <= n; i++) cin >> a[i];
int max_price = -;
LL ans = ;
for (int i = n; i >= ; i--) {
max_price = max(max_price, a[i]);
ans += max_price - a[i];
}
cout << ans << endl;
} return ;
}

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

  1. HackerRank# Stock Maximize

    原题地址 不知道为什么要用动态规划做,明明是扫几遍就行了啊 HackerRank上的题目特别喜欢long long类型啊,不用就爆.. 代码: #include <cmath> #incl ...

  2. HackerRank - common-child【DP】

    HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...

  3. 逆向思维Stock Maximize

    题目出处 题目描述: 这个题的意思是: 给出一段时间内 某个股票的每天的价格 每天可以进行的操作有:买一股,卖掉所有股,不作为 问在给定的序列里怎样让价值最大 数据规模: 每组数据包含case数 T( ...

  4. 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 al ...

  5. dp practice 1

    https://codeforces.com/problemset/problem/553/A dp+组合数学 dp[i] 放前i种颜色的方法数 #include<bits/stdc++.h&g ...

  6. poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】

    BUY LOW, BUY LOWER Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:11148   Accepted: 392 ...

  7. CFA一级知识点总结

    更多来自:   www.vipcoursea.com   Ethics 部分 Objective of codes and standard:永远是为了maintain public trust in ...

  8. Introduction to Financial Management

    Recently,i am learning some useful things about financial management by reading <Essentials of Co ...

  9. 面试10大算法汇总——Java篇

    问题导读 1 字符串和数组 2 链表 3 树 4 图 5 排序 6 递归 vs 迭代 7 动态规划 8 位操作 9 概率问题 10 排列组合 11 其他 -- 寻找规律 英文版 以下从Java角度解释 ...

随机推荐

  1. loj2212 方伯伯的OJ

    题意: n<=1e8,m<=1e5. 标程: #include<cstdio> #include<algorithm> #include<cstring> ...

  2. leetcode-129-求根到叶子结点数字之和

    题目描述: 第一次提交: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self. ...

  3. mybatis接口映射

    通过sqlSession.getMapper();方法获取映射的接口及方法 sqlSession调用Configuration的getMapper方法,方法中使用了mapperRegistry.get ...

  4. Spring简洁总结

    Spring简洁总结  要的对象不是自己建的,而是IOC容器(XML文件)给的,我们通过getbean来调用. 依赖注入的话就是对象(bean)的成员的赋值不是我们手动完成,而是容器(XML文件)给我 ...

  5. MyEclipse使用总结——MyEclipse安装maven插件[转]

    打开 myeclipse, MyEclipse -->  Preferences -->Maven4MyEclipse 确定jdk是可用的 选择自己安装的maven: 点击Add选中我们的 ...

  6. iOS之CALayer属性简介

    /* CoreAnimation - CALayer.h Copyright (c) 2006-2017, Apple Inc. All rights reserved. */ #import < ...

  7. LINK : fatal error LNK1104: 无法打开文件“ucrtd.lib”

    先说解决方案: 选中项目->右键->属性->常规 -->Windows SDK     改成当前系统的SDK版本,我这边是10.0.15063.0,重新生成即可 下载cefsh ...

  8. vue中使用watch函数,当数据改变时自动引发事件

    本来我的需求是这样的,使用ElementUI的日期选择器,当日期选择器被更改时需要根据新日期来向服务器获取新数据,但是发现这个日期选择器没有change事件,后来终于发现vue有个watch函数就是用 ...

  9. python-爬免费ip并验证其可行性

    前言 最近在重新温习python基础-正则,感觉正则很强大,不过有点枯燥,想着,就去应用正则,找点有趣的事玩玩 00xx01---代理IP 有好多免费的ip,不过一个一个保存太难了,也不可能,还是用我 ...

  10. lc13 Roman to Integer

    lc13 Roman to Integer 遇到那六种特殊情况分别-2,-20,-200, 按照罗马数字的规则,每种只可能出现一次.所以只需要考虑一次,用indexOf()即可判断是否出现这几种特殊情 ...