zoukankan      html  css  js  c++  java
  • CCPC2018-A-Buy and Resell

    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
    题意是有n个城市,给出一个商品在这n个城市的价格,商人从第一个城市到第n个城市,在每个城市可以选择买入、卖出或者不操作,可以携带多个商品,问最大收益和最小操作次数
    
    先考虑贪心进行选择,到达一个城市之后,如果之前有城市的价格比他低我就在前面的城市买入在这里卖出
    但这样显然是有问题的,比如第一组样例1 2 10 9,这样操作的话答案是 2-1=1,但实际应该是 10-1+9-2(或者10-2+9-1)
    我们考虑用优先队列来维护出现过的价格,当我们在某个城市选择以a价格买入b价格卖出时,我们向队列里加入两个b,并且把b-a先加进答案中
    那么如果在之后的操作中,我们想以a买入以c卖出,我们的实际操作是买入队列中的b以c卖出,这时我们的收益是c-b+b-a=c-a,而且还有一个b在队列中
    
    至于操作次数,如果是买入加入队列两次的那个物品操作次数不变,否则加二
    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >q;
    map<int,int>mp;
    int T,n,x;
    int main()
    {
        scanf("%d",&T);
        while (T--)
        {
            scanf("%d",&n);
            ll ans=0;
            int cnt=0;
            mp.clear();
            for (int i=1;i<=n;i++)
            {
                scanf("%d",&x);
                if (!q.empty() && x>q.top())
                {
                    ans+=x-q.top();
                    if (mp[q.top()]) mp[q.top()]--;
                    else cnt+=2;
                    q.pop();
                    q.push(x);
                    mp[x]++;
                }
                q.push(x);
            }
            printf("%lld %d
    ",ans,cnt);
            while (!q.empty()) q.pop();
        }
        return 0;
    }
    View Code
  • 相关阅读:
    范式的理解
    org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException
    RStudio Server安装后无法登录问题
    CSAPP =2= 信息的表示和处理
    【通知】博客迁移到知乎和公众号 20200828
    如何破解zip密码,以及了解8图片(8tupian.com)加密图片
    斐讯K2路由器刷华硕固件后指示灯颜色显示修改(脚本修改)
    关于GBK 编码
    KEIL uVision,KEIL MDK,KEIL For ARM,RealView MDK,KEIL C51,KEIL C166,KEIL C251等的区别
    Python串口通信助手
  • 原文地址:https://www.cnblogs.com/tetew/p/9641635.html
Copyright © 2011-2022 走看看