牛客多校第二场 D money (贪心)

链接: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

 

贪心。

对于一个非严格递增的段来说,最佳取法一定是买最小的,然后在最大值的时候卖掉

根据这个原则贪心即可。

#include <bits/stdc++.h>
#define fir first
#define se second
#define pb push_back
#define ll long long
#define mp make_pair
using namespace std;
const int maxn=1e5+10;
const int maxm=1e6+10;
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const double eps=1e-7;
int n;
ll a[maxn];

int main(){
    int t;
    cin>>t;
    while (t--){
        scanf("%d",&n);
        for (int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        ll ans=0;
        ll temp=a[1];
        ll sum=0;
        for (int i=1;i<=n;i++){
            if (a[i-1]>a[i]&&i!=1){
                    ans+=a[i-1]-temp;
                    if (a[i-1]-temp){
                        sum+=2;
                    }
                    temp=a[i];
            }
        }
        if (a[n]>=temp){
            ans+=a[n]-temp;
            if (a[n]-temp){
                sum+=2;
            }
        }
        printf("%lld %lld\n",ans,sum);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值