zoukankan      html  css  js  c++  java
  • 2018中国大学生程序设计竞赛

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438

    Buy and Resell

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1233    Accepted Submission(s): 407

    Problem Description

    The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,,n where allowed to trade it. The trading price of the Power Cube in the i-th city is ai dollars per cube. Noswal is a foxy businessman and wants to quietly make a fortune by buying and reselling Power Cubes. To avoid being discovered by the police, Noswal will go to the i-th city and choose exactly one of the following three options on the i-th day:

    1. spend ai dollars to buy a Power Cube
    2. resell a Power Cube and get ai dollars if he has at least one Power Cube
    3. do nothing

    Obviously, Noswal can own more than one Power Cubes at the same time. After going to the n cities, he will go back home and stay away from the cops. He wants to know the maximum profit he can earn. In the meanwhile, to lower the risks, he wants to minimize the times of trading (include buy and sell) to get the maximum profit. Noswal is a foxy and successful businessman so you can assume that he has infinity money at the beginning.

    Input

    There are multiple test cases. The first line of input contains a positive integer T (T250), indicating the number of test cases. For each test case:
    The first line has an integer n. (1n105)
    The second line has n integers a1,a2,,an where ai means the trading price (buy or sell) of the Power Cube in the i-th city. (1ai109)
    It is guaranteed that the sum of all n is no more than 5×105.

    Output

    For each case, print one line with two integers —— the maximum profit and the minimum times of trading to get the maximum profit.
     
    Sample Input
    3 4 1 2 10 9 5 9 5 9 10 5 2 2 1
     
    Sample Output
    16 4 5 2 0 0
    Hint
    In the first case, he will buy in 1, 2 and resell in 3, 4. profit = - 1 - 2 + 10 + 9 = 16 In the second case, he will buy in 2 and resell in 4. profit = - 5 + 10 = 5 In the third case, he will do nothing and earn nothing. profit = 0
     
    Source

    题意概括:

    有 N 个商店,小商按顺序走过每个商店(不能回头),在每个商店它可以选择在那个商店以那个商店的价格买入或者卖出一件物品,当然他也可以选择不买不卖。小商的背包容量无限大。我需要做的是使得小商走过所有商店之后获得最大的利润以及他所花费的交易次数。

    解题思路:

    之前牛客暑假多校的一个变形,不只是能拿一个了,可以拿很多个。这道题的解法也是贪心,但是与之前那道很不一样。

    首先他可以买多个商品,也就是说,他可能有反悔的机会,也就是说虽然他前面已经把那件商品卖掉了,但如果后面有比前面更优的选择,他其实可以反悔,之前不卖不出去而是等到更优的时候再卖。

    而我们要做的就是为他提供一个可以反悔的机会。

    这时候就需要用 STL 里的优先队列来维护一个最小堆了,而最小堆里面的是买入的商品(这里指的买入的商品其实更确切的说是加上交换得来的商品,但交换而来的商品要区别于直接花钱买的商品,所以要打上标记),堆顶的值是这些商品中价格最小的。

    有了这个反悔神器,小商每到一家商店就可以把商店的交易价和堆顶的值做比较,如果比堆顶的值大的则买入(拿便宜的换贵的嘛),盈利值就是贵的和便宜的价钱之差,并把当前这家商店的商品丢进堆里,那么原本堆顶用于交换的那件呢怎么办呢?这就要看它是当时花钱买来的还是换来的了,如果是花钱买来的就直接不要啦并且操作数+1;如果是交换而来的,那么就去掉标记重新入堆(这种情况下我们发现其实它相当于一个中转物品了,这件贵的物品其实是跟交换它的那件物品进行交换了,而不是跟它,所以操作数不变并且要去掉标记)。如果当前商店的交易价比堆顶的要小呢,当然丢进堆里啦(反正现在丢进堆里又不用钱),如果后面有机会用高价换掉它的话又赚一波。所以有了这个时光机,商人可是稳赚不赔的啊。

    AC code:

     1 #include <bits/stdc++.h>
     2 #define INF 0x3f3f3f3f
     3 #define ll long long int
     4 using namespace std;
     5 const int MAXN = 1e5+10;
     6 int N;
     7 struct tp{
     8     int x, v;
     9     bool sell;
    10     bool friend operator<(tp a, tp b)
    11     {
    12         if(a.v == b.v) return !a.sell;
    13         return a.v > b.v;
    14     }
    15 };
    16 int main()
    17 {
    18     int T_case, a;
    19     scanf("%d", &T_case);
    20     while(T_case--)
    21     {
    22         priority_queue<tp> Q;
    23         scanf("%d", &N);
    24         scanf("%d", &a);
    25         tp temp;
    26         temp.x = 1;
    27         temp.v = a;
    28         temp.sell = false;
    29         Q.push(temp);
    30         ll ans = 0, time = 0;
    31         for(int i = 2; i <= N; i++)
    32         {
    33             scanf("%d", &a);
    34             temp.sell = false;
    35             if(a > Q.top().v)
    36             {
    37                 tp top = Q.top();
    38                 Q.pop();
    39                 ans+=a-top.v;
    40                 if(top.sell)
    41                 {
    42                     top.sell = false;
    43                     Q.push(top);
    44                 }
    45                 else time++;
    46                 temp.sell = true;
    47             }
    48             temp.x = i;
    49             temp.v = a;
    50             Q.push(temp);
    51         }
    52         printf("%lld %lld
    ", ans, time*2);
    53     }
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    创建授权SQL mysql数据库命令
    [mysql] 无法通过insert 创建用户ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
    MySql 用户管理 中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个;表示一个命令语句结束):
    mysql 导出数据到文件数据异常 ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
    tab切换 js制作 index和this的应用
    mysql中 where in 用法详解
    Mysql Join语法解析与性能分析
    Java 导入导出Excle表格 两种方式
    SQLyog 快捷键
    DnCNN-Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising
  • 原文地址:https://www.cnblogs.com/ymzjj/p/9538286.html
Copyright © 2011-2022 走看看