原题地址

不知道为什么要用动态规划做,明明是扫几遍就行了啊

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的更多相关文章

  1. 逆向思维Stock Maximize

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

  2. Introduction to Financial Management

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

  3. 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 ...

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

  5. zoj 2921 Stock(贪心)

    Optiver sponsored problem. After years of hard work Optiver has developed a mathematical model that ...

  6. USACO Stock Market

    洛谷 P2938 [USACO09FEB]股票市场Stock Market 洛谷传送门 JDOJ 2625: USACO 2009 Feb Gold 2.Stock Market JDOJ传送门 题目 ...

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

  8. 【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 ...

  9. [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 ...

随机推荐

  1. Java 堆内存和栈内存

    看了一些别人总结的博客,感觉对堆内存和栈内存有了一个初步的认识.所以来写写自己对堆内存和栈内存的理解. Java把内存分成两种,一种叫做栈内存,一种叫做堆内存. 在函数中定义的一些基本类型的变量和对象 ...

  2. log日志应用 自定义的log

    package com.kxd.utils; import java.util.Hashtable; import android.util.Log; /** * The class for prin ...

  3. (转)Linux下清理Cache方法

    频繁的文件访问会导致系统的Cache使用量大增, 系统运行缓慢. 1 首先用free 命令查看内存的使用:$ free -m             total       used       fr ...

  4. 动画 iOS基础

    动画 iOS基础 1.     basic animation  基础动画 一个基础动画 在一个开始值和一个结束值之间运动   messageLabel.alpha=0.0; [UIView  ani ...

  5. Node.js 打造实时多人游戏框架

    在 Node.js 如火如荼发展的今天,我们已经可以用它来做各种各样的事情.前段时间UP主参加了极客松活动,在这次活动中我们意在做出一款让“低头族”能够更多交流的游戏,核心功能便是 Lan Party ...

  6. codevs 1497 取余运算

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 输入b,p,k的值,编程计算bp mod k的值.其中的b,p,k*k为长 ...

  7. UVA1515 Pool construction (最小割模型)

    如果不允许转化'#'和'.'的话,那么可以直接在'#'和'.'之间连容量为b的边,把所有'#'和一个源点连接, 所有'.'和一个汇点连接,流量不限,那么割就是建围栏(分割'#'和'.')的花费. 问题 ...

  8. nodejs:遍历文件夹文件统计文件大小

    根据 http://blog.csdn.net/hero82748274/article/details/45700465这里的思路对读写文件做了一个 封装: webpack在打包的时候可以借助ass ...

  9. C#中Lock关键字的使用

    C# 中的 Lock 语句通过隐式使用 Monitor 来提供同步功能.lock 关键字在块的开始处调用 Enter,而在块的结尾处调用 Exit. 通常,应避免锁定 public 类型,否则实例将超 ...

  10. 第1节 flume:11、flume的failover机制实现高可用

    1.4 高可用Flum-NG配置案例failover 在完成单点的Flume NG搭建后,下面我们搭建一个高可用的Flume NG集群,架构图如下所示: 图中,我们可以看出,Flume的存储可以支持多 ...