zoukankan      html  css  js  c++  java
  • (第二场)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数组:

     
      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
    ", 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
    ", dp[N][1], g[N][1]);
    73         else if(dp[N][1] < dp[N][0])
    74             printf("%lld %lld
    ", dp[N][0], g[N][0]);
    75         else
    76         {
    77             if(g[N][1] > g[N][0]) printf("%lld %lld
    ", dp[N][0], g[N][0]);
    78             else  printf("%lld %lld
    ", dp[N][1], g[N][1]);
    79         }
    80     }
    81 
    82     return 0;
    83 
    84  }
    
    
  • 相关阅读:
    全新 D 系列虚拟机型号
    D 系列性能预期
    Azure Backup 入门
    对 Azure Backup 的常见配置问题进行故障排除
    宣布发布长期保留 Azure Backup功能
    宣布 Azure Backup 支持备份 Windows Server 2008
    Azure Backup 简介
    MongoDB中ObjectId的误区,以及引起的一系列问题
    UNIX/Linux_C_程序员需要掌握的七种武器
    Docker企业版安装指南
  • 原文地址:https://www.cnblogs.com/ymzjj/p/9349585.html
Copyright © 2011-2022 走看看