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 ...
随机推荐
- Android线程池(转)
.前言 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52415337 使用线程池可以给我们带来很多好处,首先通过线程池中线程的重用, ...
- 抽象常量class
需要把经常用到的常量抽象到一个类里面管理 如:
- sqlserver 视图用 case when
视图用 case when 需要 用如下格式,[需要的列名]= case when...,而表里面的case 不用这样 [isNormal]=CASE WHENdbo.c_bdm_head.I_E_F ...
- ftpclient 遇到的一些问题
1. FTPFile[] files=ftpClient.listFiles(ftpDirectory); 没有数据 public static boolean ftpLogin(String ser ...
- (九)mybatis之生命周期
生命周期 SqlSessionFactoryBuilder SqlSessionFactoryBuilder的作用就是生成SqlSessionFactory对象,是一个构建器.所以我们一旦构建 ...
- ubuntu14.04 red5
jdk tar -xzvf jdk-8u151-linux-x64.tar.gz nano /etc/profile export HADOOP_HOME=/usr/local/src/hadoope ...
- 记录我开发工作中遇到HTTP跨域和OPTION请求的一个坑
我通过这篇文章把今天工作中遇到的HTTP跨域和OPTION请求的一个坑记录下来. 场景是我需要在部署在域名a的Web应用里用JavaScript去消费一个部署在域名b的服务器上的服务.域名b上的服务也 ...
- iPhone Tutorials
http://www.raywenderlich.com/tutorials This site contains a ton of fun written tutorials – so many t ...
- House of force
0x00 利用要点 1.申请一块非常大的块. 2.精心构造size覆盖top chunk的chunk header. 3.调用malloc()实现任意地址写 0x01 申请一块非常大的块. 申请一个负 ...
- js常用技巧汇总
将彻底屏蔽鼠标右键 oncontextmenu="window.event.returnvalue=false" <table border oncontextmenu=re ...