HackerRank# Stock Maximize
不知道为什么要用动态规划做,明明是扫几遍就行了啊
HackerRank上的题目特别喜欢long long类型啊,不用就爆。。
代码:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; #define MAX_N 50008 long long share[MAX_N];
bool sell[MAX_N]; int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int T, N;
cin >> T;
while (T--) {
long long max_share = ;
long long profit = ;
long long cnt = ;
cin >> N;
for (int i = ; i < N; i++)
cin >> share[i];
max_share = share[N - ];
for (int i = N - ; i >= ; i--) {
sell[i] = share[i] >= max_share;
max_share = max(max_share, share[i]);
}
for (int i = ; i < N; i++) {
if (sell[i]) {
profit += cnt * share[i];
cnt = ;
} else {
profit -= share[i];
cnt += ;
}
}
cout << profit << endl;
}
return ;
}
HackerRank# Stock Maximize的更多相关文章
- 逆向思维Stock Maximize
题目出处 题目描述: 这个题的意思是: 给出一段时间内 某个股票的每天的价格 每天可以进行的操作有:买一股,卖掉所有股,不作为 问在给定的序列里怎样让价值最大 数据规模: 每组数据包含case数 T( ...
- Introduction to Financial Management
Recently,i am learning some useful things about financial management by reading <Essentials of Co ...
- LeetCode_Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Leetcode - 309. Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- zoj 2921 Stock(贪心)
Optiver sponsored problem. After years of hard work Optiver has developed a mathematical model that ...
- USACO Stock Market
洛谷 P2938 [USACO09FEB]股票市场Stock Market 洛谷传送门 JDOJ 2625: USACO 2009 Feb Gold 2.Stock Market JDOJ传送门 题目 ...
- Leetcode No.121 Best Time to Buy and Sell Stock(c++实现)
1. 题目 1.1 英文题目 You are given an array prices where prices[i] is the price of a given stock on the it ...
- 【leetcode】121. Best Time to Buy and Sell Stock(股票问题)
You are given an array prices where prices[i] is the price of a given stock on the ith day. You want ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- WebService学习之旅(七)Axis2发布WebService的几种方式
前面几篇文章中简单的介绍了如何使用Axis2发布WebService及如何使用Axis2实现Web服务的客户端调用,本节將详细介绍Axis2发布WebService的几种方式. 一.使用aar包方式发 ...
- sqlite总结1
I Shell下命令行程序CLP I .help II 命令的简写 .e = .quit .s .h = .help II 数据库管理 A 创建数据库 1 CREATE TABLE id_name(i ...
- 项目经验——jboss 配置数据库连接池
数据库的连接和关闭是非常消耗系统资源的,在多层结构的应用环境中,这种资源消耗又直接的反映到系统性能上来.在项目实际应用中,最常用的解决方案便是建立数据库连接池. 一.数据库连接池基本原理 当程序启动时 ...
- POJ 1739 Tony's Tour (插头DP,轮廓线DP)
题意:给一个n*m的矩阵,其中#是障碍格子,其他则是必走的格子,问从左下角的格子走到右下角的格子有多少种方式. 思路: 注意有可能答案是0,就是障碍格子阻挡住了去路. 插头DP有两种比较常见的表示连通 ...
- maven打包错误:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.118 sec <<< FAILURE! - in ...
- HDOJ4550 卡片游戏 随便销毁内存的代价就是wa//string类的一些用法
思路 标记最小的最后的位置 放在第一位 标记位置之前按left值小的左方大的右方 标记位置之后按顺序放在最后 不多说先贴上销毁内存的wa代码 销毁内存的wa代码 #include<cstdio ...
- leetcode_1049. Last Stone Weight II_[DP]
1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii/ 题意:从一堆石头里任选两个石头s1,s2, ...
- Scala 的list
9.1 使用列表 列表类型:跟数组一样,列表也是同质化的(homogeneous).即所有元素都要是同种类型. 列表结构:所有列表由两部分组成:Nil 和 ::(cons). 基本操作:主要有三个:h ...
- myna代码
https://github.com/TalkingData/Myna/tree/master/Dataset https://github.com/TalkingData/Myna
- 当然,perl等脚本服务器是一般默认安装了,你入侵了一台主机,总不能先装配 Java 环境然后再开干吧?
转自:https://www.zhihu.com/question/20173592 当然,perl等脚本服务器是一般默认安装了,你入侵了一台主机,总不能先装配 Java 环境然后再开干吧?