逆向思维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 ...
随机推荐
- 【转】string常用函数
原文地址:http://hi.baidu.com/baowup/blog/item/3a27465c86d71546faf2c066.html/cmtid/de1ef3f0de7554a0a40f52 ...
- Oracle 存储过程(2)
http://www.cnblogs.com/chinafine/archive/2010/07/12/1776102.html http://blog.itpub.net/29485627/view ...
- 如何学习H264协议
如何学习h.264协议 首先,我假定你已经具有如下基础: 1 了解基本的视频知识,知道什么是YCbCr/YUV: 2 知道基本的视频压缩原理: 如果这两条还不具备,那么,停一下,补一下课.这方面的相关 ...
- codevs1044四子连棋(Dfs)
/* 数据范围太小 暴力暴力 Dfs直接 终止条件嘛 就是4中目标棋局 挨着枚举一遍就好了 搜索的起点一定是空格 当然 空格周围有黑有白 黑先走或者白先走答案可能不一样 所以 维护一个b 表示这一步走 ...
- AJAX入门学习(转)
一.基础概念 1.全称:Asynchronous.JavaScript.And.XML(异步的 JavaScript 和 XML). 2.定义: Ajax不是一个技术,它实际上是几种技术,每种技术都有 ...
- 自签名SSL生成
本教程以AppServ生成自签名证书为例,同时配置OpenSSL环境1.生成自签名证书,控制台程序进入Apache目录下的bin目录 >openssl req -config ../conf/o ...
- PHP解析xml
<?xml version="1.0" encoding="UTF-8"?> <ZIP_result> <result name= ...
- ASP.NET5 静态文件
静态文件,包括HTML文件,CSS文件,图像文件和JavaScript文件,它是一个应用里所包含的资源. 1. 提供静态文件 默认的,静态文件存储在你的webroot目录下面,webroot的路径定义 ...
- 从服务器将Oracle数据库导出到本地Oracle数据库的方法
1.将服务器上的Oracle数据库导入到本地 在CMD模式下执行以下命令: exp username1/password@服务器端数据库 file=本地硬盘:/文件名.dmp 例如: exp ...
- qt实现类似QQ伸缩窗口--鼠标事件应用
原创文章,引用请保证原文完整性,尊重作者劳动,原文地址http://blog.csdn.net/hiwubihe/article/details/38678305,qq:1269122125. 上一章 ...