九度OJ 1537 买卖股票
2014-03-24 23:52
162 查看
转载请注明出处:http://blog.csdn.net/a1dark
分析:动态规划、降维
#include<stdio.h>
int dp[1005][1005];
int main()
{
int n,k;
int w[1005];
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
}
int temp=w[1];
for(int i = 1; i <= k; i++)
{
int max = dp[i - 1][1] - w[1];
for(int j = 2; j <= n; j++)
{
dp[i][j] = dp[i][j - 1] > (max + w[j]) ? dp[i][j - 1] : (max + w[j]);
int tmp = dp[i - 1][j] - w[j];
max = tmp > max ? tmp : max;
}
}
printf("%d\n", dp[k]
);
}
return 0;
}
分析:动态规划、降维
#include<stdio.h>
int dp[1005][1005];
int main()
{
int n,k;
int w[1005];
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
}
int temp=w[1];
for(int i = 1; i <= k; i++)
{
int max = dp[i - 1][1] - w[1];
for(int j = 2; j <= n; j++)
{
dp[i][j] = dp[i][j - 1] > (max + w[j]) ? dp[i][j - 1] : (max + w[j]);
int tmp = dp[i - 1][j] - w[j];
max = tmp > max ? tmp : max;
}
}
printf("%d\n", dp[k]
);
}
return 0;
}
相关文章推荐
- 九度 1537:买卖股票(区间DP)
- 九度OJ 1537 买卖股票(DP)
- 题目1537:买卖股票
- 题目1537:买卖股票
- (undone)动态规划-题目1537:买卖股票
- LeetCode OJ 之 Best Time to Buy and Sell Stock (买卖股票的最佳时间)
- 股票买卖--openjudge
- LeetCode 123. Best Time to Buy and Sell Stock III(股票买卖)
- LeetCode之买卖股票
- 买卖股票的最佳时机
- [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机
- LintCode_149_买卖股票的最佳时机
- 买卖股票的最佳时机
- 股票买卖时机:
- [Leetcode]2018.3.22买卖股票的最佳时机 II
- LeetCode——Best Time to Buy and Sell Stock I (股票买卖时机问题1)
- LeetCode OJ 之 Best Time to Buy and Sell Stock II (买卖股票的最佳时间 2)
- Leetcode 122 Best Time to Buy and Sell Stock II 不限次数买卖股票最大收益
- [各种面试题] 买卖股票最多K次
- **[Lintcode]Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV Leetcode