White Cloud has built nn stores numbered from 11 to nn.
White Rabbit wants to visit these stores in the order from 11 to nn.
The store numbered ii has a price a[i]a[i] representing that White Rabbit can spend a[i]a[i] dollars to buy a product or sell a product to get a[i]a[i] dollars when it is in the ii-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.
Input
The first line contains an integer T(0<T≤5)T(0<T≤5), denoting the number of test cases.
In each test case, there is one integer n(0<n≤100000)n(0<n≤100000) in the first line,denoting the number of stores.
For the next line, There are nn integers in range [0,2147483648)[0,2147483648), denoting a[1⋯n]a[1⋯n].
Output
For each test case, print a single line containing 22 integers, denoting the maximum profit and the minimum number of transactions.
Example
Input
1
5
9 10 7 6 8
Output
3 4
mistakes:
unique的使用,在找关系的时候,a[1]前面需要一个比它大的,来购入a[1],a[n]后面需要一个比它小的,来售出a[n]
在过程中要去除重复元素,使用unique函数来进行数组去重
unique(a+1,a+n+1)-a;wa掉了
unique(a,a+n+2)-a;ac了
wa代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 101000;
#define inf 2147483678
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
ll a[maxn];
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
a[0]=inf;
a[n+1]=-inf;
int sz=unique(a+1,a+n)-a;
int buy=0;
ll profit=0;
int trans=0;
for(int i=1;i<=sz;i++)
{
if(buy==0)
{
if(a[i-1]>a[i]&&a[i]<a[i+1])
{
profit-=a[i];
//cout<<profit<<" "<<i<<endl;
trans++;
buy=1;
}
}
else if(buy==1)
{
if(a[i-1]<a[i]&&a[i]>a[i+1])
{
profit+=a[i];
//cout<<profit<<" "<<i<<endl;
trans++;
buy=0;
}
}
}
cout<<profit<<" "<<trans<<endl;
}
return 0;
}
ac代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 101000;
#define inf 2147483678
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
ll a[maxn];
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
a[0]=inf;
a[n+1]=-inf;
int sz=unique(a,a+n+2)-a;
int buy=0;
ll profit=0;
int trans=0;
for(int i=1;i<sz-1;i++)
{
if(buy==0)
{
if(a[i-1]>a[i]&&a[i]<a[i+1])
{
profit-=a[i];
//cout<<profit<<" "<<i<<endl;
trans++;
buy=1;
}
}
else if(buy==1)
{
if(a[i-1]<a[i]&&a[i]>a[i+1])
{
profit+=a[i];
//cout<<profit<<" "<<i<<endl;
trans++;
buy=0;
}
}
}
cout<<profit<<" "<<trans<<endl;
}
return 0;
}