题目链接 https://www.nowcoder.com/acm/contest/140#question

【题目描述】
White Cloud is exercising in the playground.
White Cloud can walk 1 meters or run k meters per second.
Since White Cloud is tired,it can’t run for two or more continuous seconds.
White Cloud will move L to R meters. It wants to know how many different ways there are to achieve its goal.
Two ways are different if and only if they move different meters or spend different seconds or in one second, one of them walks and the other runs.

【输入格式】
The first line contains an integer T(T<=5), denoting the number of test cases.
In each test case, there is one integer n(n<=10^5) in the first line,denoting the number of stores.
For the next line, There are n integers in range [0,2147483648), denoting a[1..n].

【输出格式】
For each test case, print a single line containing 2 integers, denoting the maximum profit and the minimum number of transactions.

【题意】
你要按照顺序依次经过n个商店,每到达一个商店你可以购买一件商品,也可以出售你手中的商品。同一时刻你手上最多拿一件商品。在第i个商店购买和出售的代价都是a[i]。
问你经过完n个商店后的最大收益。
同时,在最大化收益的前提下,求最小的交易次数。

【思路】
如果所有相邻商店的代价都不一样,那么最优解一定是在极小值点买入,在极大值点卖出,这样获得的收益是最大的。如果有相邻商店的代价是一样的话,那么可以删掉其它的只留下一个,所以在一开始对数组unique去重一下然后去找极小值和极大值把它们所有差值的和计算出来就可以了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;const ll inf=1e17;
const int maxn=100050;int n;
ll a[maxn];int main(){int T;scanf("%d",&T);while(T--){scanf("%d",&n);a[0]=inf;a[n+1]=-inf;for(int i=1;i<=n;++i) scanf("%lld",&a[i]);int x=unique(a,a+n+2)-a;bool flag=1;//flag==1表示要找极小值买入ll ans=0;int cnt=0;for(int i=1;i<x-1;++i){if(flag){if(a[i-1]>=a[i] && a[i]<=a[i+1]){ans-=a[i];flag=0;++cnt;}}else{if(a[i-1]<=a[i] && a[i]>=a[i+1]){ans+=a[i];flag=1;++cnt;}}}printf("%lld %d\n",ans,cnt);}return 0;
}

转载于:https://www.cnblogs.com/wafish/p/10465289.html

2018牛客网暑期ACM多校训练营第二场 D - money(贪心)相关推荐

  1. 牛客网暑期ACM多校训练营(第二场):J. farm(暴力)

    链接:https://www.nowcoder.com/acm/contest/140/J 来源:牛客网 题目描述 White Rabbit has a rectangular farmland of ...

  2. 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)

    链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...

  3. 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)

    链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...

  4. 2018牛客网暑期ACM多校训练营(第十场)J(二分)

    题目描述: 给你n个字符串,要让它们一个一个的合并成一个串.在合并的过程中,要保证被合并的串S是合并后的串r的前缀,模式串t是串r的子序.问你将这n个字符串合并后所得到的字典序最小的串是什么. 题目分 ...

  5. 牛客网暑期ACM多校训练营(第十场)F.Rikka with Line Graph

    牛客网暑期ACM多校训练营(第十场)F.Rikka with Line Graph 做法:\(G'\) 中的对应原图两条边(a,b) (c,d)的最短路为: \[ w[a][b] + w[c][d] ...

  6. 牛客网暑期ACM多校训练营(第九场)

    牛客网暑期ACM多校训练营(第九场) A. Circulant Matrix 做法:看到下标 \(xor\) 这种情况就想 \(FWT\),可是半天没思路,于是放弃了..其实这个 \(n\) 疯狂暗示 ...

  7. 牛客网暑期ACM多校训练营(第五场)

    牛客网暑期ACM多校训练营(第五场) A. gpa 二分答案,然后就转化为是否满足 \(\frac {\sum s[i]c[i]}{\sum s[i]} ≥ D\), \(\sum s[i]c[i] ...

  8. 牛客网暑期ACM多校训练营(第三场)

    牛客网暑期ACM多校训练营(第三场) A. PACM Team 01背包,输出方案,用bool存每种状态下用的哪一个物品,卡内存.官方题解上,说用char或者short就行了.还有一种做法是把用的物品 ...

  9. 牛客网暑期ACM多校训练营(第一场)

    牛客网暑期ACM多校训练营(第一场) A. Monotonic Matrix 考虑0和1的分界线,1和2的分界线,发现问题可以转化为两条不互相穿过的路径的方案数(可重叠),题解的做法就是把一条路径斜着 ...

最新文章

  1. SUSE10中Oracle10g存储中文数据
  2. 10.5 Throwable类
  3. Python 调用pyaudio库录制以及播放wav音频文件
  4. idea中pom.xml文件横线解决办法
  5. Java 8 Features Tutorial – The ULTIMATE Guide
  6. 猪肉价格屡创新高 也许AI养猪是时候提上日程了
  7. [深入React] 8.refs
  8. c语言程序设计第三版何钦铭课后答案_C语言程序设计 (何钦铭 ) 高教版 第2版 课后习题答案.pdf...
  9. 黑客都怎么制作外挂?C++黑客编程收集的源码集合
  10. 常用工具类总结(一) 时间格式转换
  11. 转帖node详细教程
  12. Java学习练习题11:Java习题及代码11
  13. Transformer8
  14. Android中singleTask模式没起作用!!
  15. 最适合家用的洗地机哪个牌子好?最好用的洗地机推荐
  16. javascript基础知识完整笔记--------权威指南基础篇
  17. 美团面试题:Hashmap的结构,1.7和1.8有哪些区别,史上最深入的分析
  18. 安装未签名的activex控件
  19. mcrypt php windows,Windows下php安装mcrypt扩展问题
  20. 【转】 U3D DrawCall优化手记 - 夜阑卧听风吹雨

热门文章

  1. redis 集群搭建_Redis分布式缓存分布式集群搭建
  2. 用计算机制作演示文稿教案博客,制作演示文稿 教学反思
  3. mysql数据库插入图片_向MySql数据库插入与读取图片文件
  4. 省二c语言笔试试卷,2005年春浙省二级C语言笔试试卷.doc
  5. 特征因子是什么意思_生态因子作用的一般特征
  6. 手机号和邮箱正则匹配
  7. Java自学!Java项目面试介绍
  8. 实战Transformer在NLP和医学图像分割领域的应用
  9. 基于Pytorch再次解读NiN现代卷积神经网络和批量归一化
  10. Android移动开发之【通往安卓的神奇之旅】TextView和ImageView 实现跑马灯效果