• 【codeforces 572B】Order Book


    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    In this task you need to process a set of stock exchange orders and use them to create order book.

    An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order.

    All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders.

    An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order.

    An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book.

    You are given n stock exhange orders. Your task is to print order book of depth s for these orders.

    Input
    The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth.

    Next n lines contains a letter di (either ‘B’ or ‘S’), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter ‘B’ means buy, ‘S’ means sell. The price of any sell order is higher than the price of any buy order.

    Output
    Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input.

    Examples
    input
    6 2
    B 10 3
    S 50 2
    S 40 1
    S 50 6
    B 20 4
    B 25 10
    output
    S 50 8
    S 40 1
    B 25 10
    B 20 4
    Note
    Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample.

    You need to print no more than two best orders for each direction, so you shouldn’t print the order (10 3) having the worst price among buy orders.

    【题目链接】:http://codeforces.com/contest/572/problem/B

    【题解】

    让你把价格相同的且目的(要买还是卖)也相同的需求并在一起;
    卖的需要价格最小,买的价格要最高;
    但是!!最后都要降序排;
    代码1是直接用sort;
    代码2用的计数排序;
    后者显然更优。

    【代码1】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int MAXN = 1e3+10;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    struct abc
    {
        int p,q;
    };
    
    int n,s;
    vector <abc> B,S,tB,tS;
    
    bool cmp1(abc a,abc b)
    {
        return a.p > b.p;
    }
    
    bool cmp2(abc a,abc b)
    {
        return a.p < b.p;
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);rei(s);
        rep1(i,1,n)
        {
            char t;
            int x,y;
            cin >> t >> x >> y;
            if (t=='B')
                B.pb({x,y});
            else
                S.pb({x,y});
        }
        sort(B.begin(),B.end(),cmp1);
        int lenb = B.size();
        rep1(i,0,lenb-1)
        {
            int j = i;
            while (j+1<=lenb-1 && B[j+1].p == B[j].p)
            {
                j++;
                B[i].q+=B[j].q;
            }
            tB.pb({B[i].p,B[i].q});
            i = j;
        }
        sort(tB.begin(),tB.end(),cmp1);
    
        sort(S.begin(),S.end(),cmp1);
        int lens = S.size();
        rep1(i,0,lens-1)
        {
            int j = i;
            while (j+1<=lens-1 && S[j+1].p == S[j].p)
            {
                j++;
                S[i].q+=S[j].q;
            }
            tS.pb({S[i].p,S[i].q});
            i = j;
        }
        sort(tS.begin(),tS.end(),cmp2);
    
        //Sell first
        int le = tS.size();
        int len = min(s,le);
        rep2(i,len-1,0)
            printf("S %d %d
    ",tS[i].p,tS[i].q);
    
        //buy after
        le = tB.size();
        len = min(s,le);
        rep1(i,0,len-1)
            printf("B %d %d
    ",tB[i].p,tB[i].q);
        return 0;
    }

    【代码2】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int MAXN = 1e5+10;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int n,s;
    int B[MAXN],S[MAXN];
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);rei(s);
        rep1(i,1,n)
        {
            char d;int p,q;
            cin >> d >> p >> q;
            if (d=='B')
                B[p] += q;
            else
                S[p] += q;
        }
        int c = 0,i = 0;
        for ( ; i <= 100000 && c < s ;i++)
            if (S[i]>0)
                c++;
        rep2(j,i-1,0)
        {
            if (S[j] > 0)
                printf("S %d %d
    ",j,S[j]);
        }
        i = 100000,c = 0;
        for ( ; i >=0 && c < s;i--)
            if (B[i]>0)
                c++;
        rep2(j,100000,i+1)
            {
                if (B[j]>0)
                    printf("B %d %d
    ",j,B[j]);
            }
        return 0;
    }
    
  • 相关阅读:
    删除maven本地库中下载不完全的jar包
    nginx负载均衡
    对字符串中的中英文进行统计
    springboot部分常用注解
    Scala 泛型
    Scala 递归举例
    Kafka(v0.11)笔记
    Scala 匿名函数与参数类型推断(简写)
    Scala 高阶函数
    Scala 偏函数 PartialFunction
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626778.html
Copyright © 2020-2023  润新知