1. 程式人生 > >(第二場)D Money 【dp貪心】

(第二場)D Money 【dp貪心】

收益 mem main cap bsp deb max std 一個

題目: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
5
9 10 7 6 8

輸出:

3 4

大概題意:

有標號為1~N的N間商鋪,小白兔每到一個商鋪可以選擇以a[i]的價格購入或賣出,也可以選擇不買不賣,小白兔只能攜帶一件商品,求最大收益和最小交易次數。

思路:

一、DP

狀態:

dp[i][0] 走了 i 間商鋪之後,手上沒有商品的最大收益

dp[i][1] 走了 i 間商鋪之後,手上有商品的最大收益

g[i][0] 走了 i 間商鋪之後,手上沒有商品的最大收益的最少交易次數

g[i][1] 走了 i 間商鋪之後,手上有商品的最大收益的最少交易次數

狀態轉移方程:

dp[i][0] = max(dp[i-1][0], dp[i-1][1] + a[i]);

dp[i][1] = max(dp[i-1][0] - a[i], dp[i-1][1]);

g隨著dp的更新而更新。

dp數組:

1 2 3 4 5
0 0 1 1 1 3
1 -9 -9 -6 -5 -5

g數組:

1 2 3 4 5
0 0 2 2 2 4
1 1 1 3 3 3

!!!註意數據範圍,dp和g都要定義為long long,第一次dp不夠大卡在了60%,第二次g不夠大卡在了80%!!!

AC code:

 1  #include <bits/stdc++.h>
 2  #define INF 0x3f3f3f3f
 3  using namespace std;
 4 
 5  const int MAXN = 1e5+10;
 6 
 7  int N, T;
 8  long long int a[MAXN];
 9  long long int dp[MAXN][2];
10  long long int g[MAXN][2];
11 
12  void init()
13  {
14      memset(dp, 0, sizeof(dp));
15      memset(g, 0, sizeof(g));
16  }
17 
18  int main()
19  {
20     scanf("%d", &T);
21     while(T--)
22     {
23         init();
24         scanf("%d", &N);
25         for(int i = 1; i <= N; i++)
26         {
27             scanf("%lld", &a[i]);
28         }
29         ///dp
30         dp[1][0] = 0;
31         dp[1][1] = -a[1];
32         g[1][0] = 0;
33         g[1][1] = 1;
34 
35         for(int i = 2; i <= N; i++)
36         {
37             if(dp[i-1][0] >= (dp[i-1][1]+a[i]))
38             {
39                 dp[i][0] = dp[i-1][0];
40                 g[i][0] = g[i-1][0];
41             }
42             else
43             {
44                 dp[i][0] = dp[i-1][1] + a[i];
45                 g[i][0] = (g[i-1][1]+1);
46             }
47 
48             if((dp[i-1][0]-a[i]) > dp[i-1][1])
49             {
50                 dp[i][1] = dp[i-1][0] - a[i];
51                 g[i][1] = (g[i-1][0]+1);
52                 //printf("%d %d %d %d\n", i, dp[i][1], g[i][1], g[i-1][0]);
53             }
54             else
55             {
56                 dp[i][1] = dp[i-1][1];
57                 g[i][1] = g[i-1][1];
58             }
59         }
60 
61         ///debug
62         /*
63         for(int i = 1; i <= N; i++)
64             printf("%d ", g[i][0]);
65         puts("");
66         for(int i = 1; i <= N; i++)
67             printf("%d ", g[i][1]);
68         puts("");
69         */
70 
71         if(dp[N][1] > dp[N][0])
72             printf("%lld %lld\n", dp[N][1], g[N][1]);
73         else if(dp[N][1] < dp[N][0])
74             printf("%lld %lld\n", dp[N][0], g[N][0]);
75         else
76         {
77             if(g[N][1] > g[N][0]) printf("%lld %lld\n", dp[N][0], g[N][0]);
78             else  printf("%lld %lld\n", dp[N][1], g[N][1]);
79         }
80     }
81 
82     return 0;
83 
84  }

(第二場)D Money 【dp\貪心】