逆向思维Stock Maximize
题目描述:
这个题的意思是:
给出一段时间内 某个股票的每天的价格
每天可以进行的操作有:买一股,卖掉所有股,不作为
问在给定的序列里怎样让价值最大
数据规模:
每组数据包含case数 T(<=10)
每个case中 股票的天数n(<=50000)
每个股票的价格vi(1<=vi<=100000)
弱智思路:
本人的弱智思路是这样的,因为这一个题目被分在了dp分类之中,于是各种蛋疼,dp[i]表示在第i天能达到的最大价值,于是有转移方程:
dp[i] = dp[j]+v[i]*(i-j+1)-从i-j的股票价格之和(j从0到i)
然后悠然自得地写了如下代码交了:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = ; int a[MAXN];
long long dp[MAXN];
int n; void input(){
cin>>n;
for ( int i = ; i <= n; i++ ){
cin>>a[i];
}
memset(,sizeof(dp),);
} long long MAX( long long a, long long b ){
return a > b ? a : b ;
} void solve(){
for ( int i = ; i <= n; i++ ){
long long v = ;
dp[i] = dp[i-];
if ( a[i] > a[i-] ){
for ( int j = i-; j >= ; j-- ){
v += a[j];
dp[i] = MAX(dp[i],dp[j]-v+a[i]*(i-j));
}
}
}
cout<<dp[n]<<endl;
} void deal(){
int t;
cin>>t;
while ( t-- ){
input();
solve();
} } int main() {
deal();
return ;
}
弱爆了
然后就超时了
思考了好几天状态压缩的DP,还是各种WA,无奈之下做了伸手党要来了Lazy的代码:
#include <cstdio>
using namespace std; const int MAXN = + ; int N;
int p[MAXN]; void input() {
scanf("%d", &N);
for (int i=; i<N; ++i) {
scanf("%d", &p[i]);
}
} void solve() {
long long rev = ;
int max_p = p[N-];
for (int i=N-; i>=; --i) {
if (p[i] < max_p) {
rev += max_p - p[i];
} else {
max_p = p[i];
}
} printf("%lld\n", rev);
} int main() {
int t;
scanf("%d", &t);
for (int i=; i<t; ++i) {
input();
solve();
}
}
快捷回复给:转身/;!孤单
碉堡了
然后恍然大悟:
这题目实际上是一个贪心,最后的价格最大就可以保证总价值最大,因此从后往前扫,并且更新max_p的值。最后输出结果
反省一下:
在思考这个题目的时候上述规律我也是发现了的,但是没有犯过头来倒着想,对编码和思考带来了很大的难度,最后还是失败了。以后思维还是灵活一点比较好.
gg~~
逆向思维Stock Maximize的更多相关文章
- HackerRank# Stock Maximize
原题地址 不知道为什么要用动态规划做,明明是扫几遍就行了啊 HackerRank上的题目特别喜欢long long类型啊,不用就爆.. 代码: #include <cmath> #incl ...
- 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 ...
随机推荐
- 判断textview是否被截断
Layout l = textview.getLayout(); if ( l != null){ int lines = l.getLineCount(); if ( lines > 0) i ...
- C#中的转义字符
一些常用的转义字符: \n 换行 \b backspace,删除光标前面的一个字符 \t tab键 由多个空格组成的一个字符,具有行与行之间的对齐功能 \\ \ 如果在字符串前面加@的话: 1 ...
- JSP学习--常用作用域
page:当前页面,也就是只要跳到别的页面就失效了 request:一次会话,简单的理解就是一次请求范围内有效 session:浏览器进程,只要当前页面没有被关闭(没有被程序强制清除),不管怎么跳转都 ...
- 常用布局,div竖直居中
常用两列布局,多列布局和div竖直居中 body { margin:; padding:; } .w200 { width: 200px; } .mar-left200 { margin-left: ...
- java中this关键字和static关键字和super关键字的用法
this关键字 1. this 关键字是类内部当中对自己的一个引用,可以方便类中方法访问自己的属性: 2.可以返回对象的自己这个类的引用,同时还可以在一个构造函数当中调用另一个构造函数(这里面上面有个 ...
- 【javascript模式】Chapter2: 基本 技巧
1 尽量少用全局变量,最好一个应用程式只有一个全局变量 隐含全局变量(不使用var声明)与明确定义的全局变量区别: (1)使用var创建的全局变量(在函数外部声明)不能用delete删除 (2) ...
- asp.net上传文件并创建文件夹和删除文件
上传文件部分代码: /// <summary> /// 上传保存文件并返回文件的保存地址和文件名称 /// </summary> /// <param name=&quo ...
- ReactNative for Android入坑(一)
最近找工作发现有些公司要求会ReactNative,决定入坑. 搭建环境:官网详细的教程附链接. 坑一:FQ,建议整个搭建过程中FQ.(FQ链接,注册有200M试用流量,环境搭建够了)第一步:安装Ch ...
- pugixml使用教程
pugixml介绍 pugixml是一个高性能.轻量级并且简单易用的xml解析库,支持UTF8 encoding.Little-endian UTF16.Big-endian UTF16.UTF16 ...
- Qt中绘图坐标QPainter,Viewport与Window的关系
在Qt中常常要自己重载一些paintEvent函数,这个时候往往忽略了两个很关键的API,那就是setViewport和setWindow. Viewport,顾名思义,反应的是物理坐标,就是你实际想 ...