牛客网暑期ACM多校训练营(第二场) D money(dp)

Description:

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.

Input:

The first line contains an integer T(0 In each test case, there is one integer n(0 For the next line, There are n integers in range [0,2147483648), denoting a[1…n].

Output:

For each test case, print a single line containing 2 integers, denoting the maximum profit and the minimum number of transactions.

Sample Input:

1
5
9 10 7 6 8

Sample Output:

3 4

题目链接

White Rabbit从编号 1 → n 1 \rightarrow n 1n遍历商店,每个商店都有一个价格 a [ i ] a[i] a[i],White Rabbit抵达一个商店时可以选择以价格 a [ i ] a[i] a[i]买商店里的商品或者以价格 a [ i ] a[i] a[i]卖掉手中的物品,求White Rabbit遍历完商店之后的最大利润和买卖操作次数。

开一个dp数组 d p [ m a x n ] [ 4 ] dp[maxn][4] dp[maxn][4] d p [ i ] [ 0 ] dp[i][0] dp[i][0]代表在第 i i i个商店以价格 a [ i ] a[i] a[i]买入商品后的最大利润, d p [ i ] [ 1 ] dp[i][1] dp[i][1]代表在第 i i i个商店以价格 a [ i ] a[i] a[i]卖出手中物品后的最大利润, d p [ i ] [ 2 ] dp[i][2] dp[i][2]在第 i i i个商店以价格 a [ i ] a[i] a[i]买入商品后的买卖操作次数, d p [ i ] [ 3 ] dp[i][3] dp[i][3]代表在第 i i i个商店以价格 a [ i ] a[i] a[i]卖出手中物品后的买卖操作次数。在遍历更新当前 d p [ i ] [ x ] dp[i][x] dp[i][x] d p [ i ] [ 0 ] dp[i][0] dp[i][0]就等于 j ∈ [ 1 , i − 1 ] j \in [1,i-1] j[1i1] d p [ j ] [ 1 ] dp[j][1] dp[j][1]的最大值减 a [ i ] a[i] a[i] d p [ i ] [ 1 ] dp[i][1] dp[i][1]等于 j ∈ [ 1 , i − 1 ] j \in [1,i-1] j[1i1] d p [ j ] [ 0 ] dp[j][0] dp[j][0]的最大值加 a [ i ] a[i] a[i],实时用变量维护更新两个最大值及其索引位置和 d p [ i ] [ 2 ] dp[i][2] dp[i][2] d p [ i ] [ 3 ] dp[i][3] dp[i][3]

在题目所给样例中流程为:

i i i i个商店的价格 a [ i ] a[i] a[i] 以价格 a [ i ] a[i] a[i]卖出手中物品后的最大利润 以价格 a [ i ] a[i] a[i]买入商品后的最大利润 j ∈ [ 1 , i − 1 ] j \in [1,i-1] j[1i1] d p [ j ] [ 1 ] dp[j][1] dp[j][1]的最大值 j ∈ [ 1 , i − 1 ] j \in [1,i-1] j[1i1] d p [ j ] [ 0 ] dp[j][0] dp[j][0]的最大值
1 9 -9 0 -9 0
2 10 -10 1 -9 1
3 7 -6 -2 -6 1
4 6 -5 0 -5 1
5 8 -7 3 -5 3

最后 j ∈ [ 1 , i − 1 ] j \in [1,i-1] j[1i1] d p [ j ] [ 0 ] dp[j][0] dp[j][0]的最大值即为最大利润,其索引位置的 d p [ i n d e x ] [ 3 ] dp[index][3] dp[index][3]即为买卖操作次数。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include 
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
const int mod = 1e9+7;
const double eps = 1e-8;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
 
int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t;
    read(t);
    while (t--) {
        int n;
        read(n);
        vector<int> a(n + 1);
        read(a[1]);
        ll dp[maxn][4];
        mem(dp, 0);
        dp[1][0] = -a[1];
        dp[1][2] = 1;
        dp[1][1] = 0;
        ll bmax = -a[1], smax = 0;
        int bindex = 1, sindex = 1;
        for (int i = 2; i <= n; ++i) {
            read(a[i]);
            dp[i][0] = smax - a[i];
            dp[i][1] = bmax + a[i];
            if (dp[i][0] > bmax) {
                bmax = dp[i][0];
                dp[i][2] = dp[sindex][3] + 1;
                bindex = i;
            }
            if (dp[i][1] > smax) {
                smax = dp[i][1];
                dp[i][3] = dp[bindex][2] + 1;
                sindex = i;
            }
        }
        printf("%lld %lld\n", smax, dp[sindex][3]);
    }
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("gedit out.txt");
#endif
    return 0;
}

你可能感兴趣的:(牛客网暑期ACM多校训练营(第二场) D money(dp))