牛客第二场Dmoney

时间:2021-09-29 21:41:15
链接:https://www.nowcoder.com/acm/contest/140/D
来源:牛客网 题目描述
White Cloud has built n stores numbered from to n.
White Rabbit wants to visit these stores in the order from 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(<T<=), denoting the number of test cases.
In each test case, there is one integer n(<n<=) in the first line,denoting the number of stores.
For the next line, There are n integers in range [,), denoting a[..n].
输出描述:
For each test case, print a single line containing integers, denoting the maximum profit and the minimum number of transactions.
示例1
输入
复制 输出
复制

贪心。下一家店变贵了手里没有拿就买。下一家店便宜了,手里有货就卖出。

 #include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <stack>
using namespace std;
#define ll long long
int a[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int m;
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d",&a[i]);
}
ll flag=;
ll ans=;
ll mo=;
a[m+]=;
for(int i=;i<=m;i++)
{
if(a[i]<a[i+]&&flag==)
{
flag=;
ans++;
mo=mo-a[i];
}
else
{
if(flag==&&a[i]>a[i+])
{
ans++;
flag=;
mo=mo+a[i];
}
}
}
cout<<mo<<" "<<ans<<endl; }
return ;
}