链接:https://www.nowcoder.com/acm/contest/140/D
来源:牛客网

White Cloud has built n stores numbered from 1 to n.
White Rabbit wants to visit these stores in the order from 1 to n.
The store numbered i has a price a[i] representing that White Rabbit can spend a[i] dollars to buy a product or sell a product to get a[i] dollars when it is in the i-th store.
The product is too heavy so that White Rabbit can only take one product at the same time.
White Rabbit wants to know the maximum profit after visiting all stores.
Also, White Rabbit wants to know the minimum number of transactions while geting the maximum profit.
Notice that White Rabbit has infinite money initially.

输入描述:

The first line contains an integer T(0<T<=5), denoting the number of test cases.
In each test case, there is one integer n(0<n<=100000) 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.示例1

输入

复制

1
5
9 10 7 6 8

输出

复制

3 4

分析:我每次只能每一个,然后必须卖掉才能买下一个,中间的利润要最大,交易次数最小
我们可以在坐标系里标出每个点,横坐标点的位置,纵坐标点的值
然后我们发现我们所要求得就是每条非递减线段的值
如下图中,我们要求的就是AB+DE

注意点值相同的时候要加上这条非递减数列的最后的值,如果不判相等中间断了结果就错了

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e5 + 10;
const int mod = 10000007;
typedef long long ll;
int t,n;
ll a[maxn],ans,s;
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
ans=s=0;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
if(n<2) {
cout<<"0 0"<<endl;
continue;
}
int fg=0;
for(int i=2;i<=n;i++) {
if(a[i]==a[i-1]) continue;
if(a[i]>=a[i-1]) ans+=(a[i]-a[i-1]),fg=1;
else if(fg) s+=2,fg=0;
}
if(a[n]>=a[n-1]&&fg) s+=2;
cout<<ans<<" "<<s<<endl;
}
return 0;
}

  

 

最新文章

  1. 基于HTML5的Web SCADA工控移动应用
  2. iOS开发--JSON
  3. JavaScript调试技巧之console.log()详解
  4. 打开SDK Manager检查Android SDK下载和更新失败的解决方法
  5. django 的mysql数据配置
  6. jdbc(1)(一)
  7. window.open和window.close的使用详解
  8. freemarker报错之三
  9. jquery 实现图片无缝向左滚动
  10. css 四周边框角加粗效果
  11. C++面向对象程序设计之C++的初步知识
  12. LabVIEW---vi图标和符号的制作
  13. PairProject 电梯调度 【附加题】
  14. django学习之——创建项目
  15. delphi 获取webbrowser的cookies给Idhttp用
  16. 安全运维之:网络实时流量监测工具iftop
  17. PHP 高精度比较bccomp
  18. Kafka Java 客户端开发
  19. php将两张身份证图片合并到一张图
  20. 【Cf #449 C】Willem, Chtholly and Seniorious(set维护线段)

热门文章

  1. LinkedList源码分析(jdk1.8)
  2. Ubuntu中修改默认开机项
  3. 如何在Vue项目中使用vw实现移动端适配
  4. HTML 第5章CSS3美化网页元素
  5. elk系列教程:docker中安装配置elk
  6. echarts legend 限制规定显示个数,显示省略号,修改默认样式
  7. cs224d---词向量表示
  8. 构建企业级数据湖?Azure Data Lake Storage Gen2不容错过(上)
  9. react父组件调用子组件中方法
  10. 关于Python虚拟环境与包管理你应该知道的事