Maximum Investment Return UVALive7658
来源:互联网 发布:dota 幽鬼攻略知乎 编辑:程序博客网 时间:2024/06/13 00:07
题意:给出 n 天中每天的股票价格,Joe 需要决定在哪天买哪天卖,他的这些决定构成了这样一些数对
思路:想要赚到尽量多的钱我们肯定要在价格尽量低的时候买入,在价格尽量高的时候卖出。由于
为此,我们只需要从左往右扫一遍每天的股票价格,每一个
比如:
10天的股票价格分别为1,2,3,4,2,3,3,4,5,1
1,2,3,4是一个阶段,2,3,3,4,5是一个阶段
产生两个队
代码:
#include<cstdio>#include<cstring>#include<cstdlib>#include<stack>#include<queue>#include<utility>#include<vector>#include<cmath>#include<set>#include<map>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;int n, k;int p[1010];vector<int> vec;bool cmp(int& a, int& b){ return a > b;}int main(){ //freopen("in.txt", "r", stdin); int T; scanf("%d", &T); while(T--){ vec.clear(); scanf("%d%d", &n, &k); int t = 0; bool first = true; for(int i=1; i<=n; i++){ //一边读入一边处理 scanf("%d", &p[i]); if(first){ first = false; t = p[i]; } else{ if(p[i]<p[i-1]){ vec.push_back(p[i-1]-t); t = p[i]; } else if(p[i]>=p[i-1] && i==n){ vec.push_back(p[i]-t); } } } sort(vec.begin(), vec.end(), cmp); //从大到小排序 int ans = 0; int sz = vec.size(); for(int i=0; i< min(k, sz); i++){ ans += 1000*vec[i]; } printf("%d\n", ans); } return 0;}
阅读全文
0 0
- Maximum Investment Return UVALive7658
- Return on Software: Maximizing the Return on Your Software Investment
- Investment
- dmabounce investment
- poj2063 - Investment
- poj2063 Investment
- POJ2063--Investment
- POJ2063 Investment
- Business investment
- POJ2063 Investment
- POJ2063 Investment
- poj2063 Investment
- Investment algorithm
- poj2063 Investment
- 【POJ2063】【Investment】
- poj2063 Investment
- Investment HDU
- Investment HDU
- 【POCO】POCO学习总结(二)——配置选择
- 数据库访问工具 DBUtl
- 20170928链接
- 起床困难综合症——贪心
- 题目42:一笔画问题
- Maximum Investment Return UVALive7658
- PHP加JS实现商品秒杀倒计时
- 计算机网络体系的三大模型
- C语言 fread()与fwrite()函数说明与示例
- 某安全浏览器竟然也被查出高危漏洞?开源安全问题不容忽视
- Eclipse汉化后如何还原为EN英文(实用技巧) --转
- google 官方ndk demo解析(二)——bitmap-plasma
- "<Module>"的类型初始值设定项引发异常的解决办法
- 2017云栖大会·杭州峰会:《云数据·大计算:海量日志数据分析与应用》之《数据分析展现:可视化报表及嵌入应用》篇