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 <= 50000All 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)的更多相关文章
- HackerRank# Stock Maximize
原题地址 不知道为什么要用动态规划做,明明是扫几遍就行了啊 HackerRank上的题目特别喜欢long long类型啊,不用就爆.. 代码: #include <cmath> #incl ...
- HackerRank - common-child【DP】
HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...
- 逆向思维Stock Maximize
题目出处 题目描述: 这个题的意思是: 给出一段时间内 某个股票的每天的价格 每天可以进行的操作有:买一股,卖掉所有股,不作为 问在给定的序列里怎样让价值最大 数据规模: 每组数据包含case数 T( ...
- 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 ...
- dp practice 1
https://codeforces.com/problemset/problem/553/A dp+组合数学 dp[i] 放前i种颜色的方法数 #include<bits/stdc++.h&g ...
- poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】
BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions:11148 Accepted: 392 ...
- CFA一级知识点总结
更多来自: www.vipcoursea.com Ethics 部分 Objective of codes and standard:永远是为了maintain public trust in ...
- Introduction to Financial Management
Recently,i am learning some useful things about financial management by reading <Essentials of Co ...
- 面试10大算法汇总——Java篇
问题导读 1 字符串和数组 2 链表 3 树 4 图 5 排序 6 递归 vs 迭代 7 动态规划 8 位操作 9 概率问题 10 排列组合 11 其他 -- 寻找规律 英文版 以下从Java角度解释 ...
随机推荐
- 图片上传的ImageIO工具类
ImageIO类说明 最近的项目中遇到ImageIO,因此记录下这个类的用法 一.ImageIO: 这个类中的方法都是静态方法,可以用来进行简单的图片IO操作 1.读入的三种方法 public sta ...
- vc 识别移动硬盘 U盘,本地硬盘
说明:有时候我们在做设备监控的时候,要识别一些链接设备,在使用函数GetDriveType的时候,U盘可以返回DRIVE_REMOVABLE,而本地硬盘硬盘和移动硬盘DRIVE_FIXED,因此还需要 ...
- 以太坊solidity智能合约语言学习资源整理
暂时看到篇文章写的不错,先收集下来,后面有机会自己也整理一个 Solidity语言学习(一)Solidity语言学习(二)——Solidity的安装与编译Solidity语言学习(三)——智能合约编程 ...
- [JZOJ4633] 【GDOI2017模拟7.15】萌萌哒
题目 描述 题目大意 给你一个数列,接下来有许多个操作,使得区间[l1,r1][l_1,r_1][l1,r1]和[l2,r2][l_2,r_2][l2,r2]对应的位置染上同样的颜色(使得它们 ...
- Python-函数基础(1)
目录 函数定义 什么是函数? 定义函数三种形式 函数定义的特性 函数调用 函数返回值 return的特性: 函数的参数 有参函数 形参 位置形参 默认形参 实参 位置实参 关键字实参 可变长参数 形参 ...
- MYSQL批量创建表的存储过程
因为业务需要,创建了100个表,但是这些表的结构都是一样的,作为程序员,就是要解决这种重复劳动.然而这种事情还要单独写个php脚本的话太麻烦了吧,所以就干脆学了一下直接用Mysql存储过程怎么实现: ...
- memcache课程---1、memcache介绍及安装(memcache作用)
memcache课程---1.memcache介绍及安装(memcache作用) 一.总结 一句话总结: 减少对数据库的访问,因为数据库的访问比较花费时间 1.memcache为什么比操作数据库快的多 ...
- 老师的blog整理 .网络编程部分 .网络编程部分 前端部分 django基础部分
老师的blog整理 .网络编程部分 .网络编程部分 前端部分 django基础部分 老师的blog整理 python基础部分: 宝哥blog: https://www.cnblogs.com/gu ...
- PAT甲级——A1007 Maximum Subsequence Sum
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- js去除空格或所有空格
function trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } /***is_global 设置"g&q ...