MENU

牛客多校 2018 第二场 D Money(贪心/DP)

2018 年 08 月 27 日 • 阅读: 1099 • 练习阅读设置

Money

  • 时间限制:C/C++ 1秒,其他语言2秒
  • 空间限制:C/C++ 131072K,其他语言262144K
  • 64bit IO Format: %lld

题目描述

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

链接

https://www.nowcoder.com/acm/contest/140/D

题意

有n个商店,R可以在第i个商店花p[i]元买一件商品或者以p[i]元卖出一件商品,R在同一时刻最多只能拥有一件商品,问R经过n个商店后能获取的最大收益,并且要求交易次数最少。

题解

低买高卖问题,限制了同时最多只能拥有一件商品。当前商品要是比下一件商品价格更低,那么当前买入下次卖出,不过后面可能会有价格更高的情况出现,其实还是一样的。A B C (A<B<C),A买入B卖出B买入C卖出就等价于A买入C卖出,至于交易次数最少,只需要记录连续上升区间数量。、

详细的题解可以参考博客:传送门

思路:

1、贪心。后面的商品比前面的贵的话,一定要买前面的,卖后面的。

2、在价格曲线上的连续递增的一段上,在开头买,在末尾卖肯定是最优的(利润最多,同时交易次数最少),中间多几次操作,利润是相同的

3、最多的利润,即每条递增段的头尾相减之和

4、利润最多的交易次数即连续递增段数量

标准题解

首先,如果a[i]=a[i+1],则可以删掉第i+1个商店。因为任何在第i+1个商店进行的交易都可以转为在第i个商店进行,且收益不变。之后,如果a[i]<a[i+1],则离开第i个商店时一定要带上一件商品。如果a[i]>a[i+1],则离开第i个商店时一定要空着手。这样,第一问的答案max(a[i+1]-a[i],0)的累加,第二问的答案就为长度>1的极大递增连续段的数量。

标准题解中还有DP思路,f[i][0/1]表示已经访问完了i个商店,你手中是否有商品,此时的最大收益。g[i][0/1]表示当f[i][j]取最大值时最少的交易次数。

代码

  • 答案正确 89ms, 1252kb, length: 758
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 100010;
ll a[maxn];

int main()
{
    int T, n;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        ll ans = 0, cnt = 0;
        bool flag = false;
        for (int i = 0; i < n; ++i)
            scanf("%lld", &a[i]);
        for (int i = 0; i < n - 1; ++i)
        {
            if (a[i + 1] > a[i]) // 低买高卖
            {
                ans += a[i + 1] - a[i];
                if (!flag) // 连续上升区间数量
                    cnt++;
                flag = true;
            }
            else if (a[i + 1] < a[i])
                flag = false;
        }
        printf("%lld %lld\n", ans, cnt * 2);
    }
    return 0;
}

The end.
2018-08-27 星期一
最后编辑于: 2019 年 01 月 26 日