(第二场)D Money 【dp\贪心】

题目: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数组:

 
 12345
001113
1-9-9-6-5-5

 

 

 

 

g数组:

 
 12345
002224
111333

 

 

 

 

!!!注意数据范围,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  }

 

 

转载于:https://www.cnblogs.com/ymzjj/p/9349585.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值