题目出处

题目描述:

这个题的意思是:

给出一段时间内 某个股票的每天的价格

每天可以进行的操作有:买一股,卖掉所有股,不作为

问在给定的序列里怎样让价值最大

数据规模:

每组数据包含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的更多相关文章

  1. HackerRank# Stock Maximize

    原题地址 不知道为什么要用动态规划做,明明是扫几遍就行了啊 HackerRank上的题目特别喜欢long long类型啊,不用就爆.. 代码: #include <cmath> #incl ...

  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. Android常用功能代码块(转)

    1.设置activity无标题,全屏 // 设置为无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为全屏模式 getWindow(). ...

  2. python 之路,Day11(上) - python mysql and ORM

    python 之路,Day11 - python mysql and ORM   本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 ...

  3. ADO.NET基础

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  4. Android permission访问权限大全

    1.android.permission.WRITE_USER_DICTIONARY 允许应用程序向用户词典中写入新词 2.android.permission.WRITE_SYNC_SETTINGS ...

  5. android自定义listview实现圆角

    在项目中我们会经常遇到这种圆角效果,因为直角的看起来确实不那么雅观,可能大家会想到用图片实现,试想上中下要分别做三张图片,这样既会是自己的项目增大也会增加内存使用量,所以使用shape来实现不失为一种 ...

  6. 关于图表的JS插件

    http://echarts.baidu.com/ http://echarts.baidu.com/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6% ...

  7. iOS微信支付

    SDK接入 服务器签名版本 官方已经是建议使用服务器签名来接入微信支付,实际上从安全上考虑,确实是每个客户端不应该知道RAS密钥,也不需要每个客户端都写一遍签名的算法. 服务端接入流程文档:https ...

  8. BOM和DOM详解

    DOM介绍 D(文档)可以理解为整个Web加载的网页文档,O(对象)可以理解为类似window对象只来的东西,可以调用属性和方法,这里我们说的是document对象,M(模型)可以理解为网页文档的树形 ...

  9. 重新开始学习javase_内部类

    转(http://www.cnblogs.com/dolphin0520/p/3811445.html) 内部类: 在Java 1.1 中,可将一个类定义置入另一个类定义中.这就叫作“内部类”.创建内 ...

  10. 用core dump来调试程序段错误

    有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的.但这不像编译错误一样会提示到文件->行, 而是没有任何信息, 使得我们的调试变得困难起来 ...