572B - Order Book - CodeForces Solution


data structures greedy implementation sortings *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define loop(n) for(int i = 1; i <= n; i++)
#define rloop(n) for(int i = n; i >= 1; i--)
#define loop0(n) for(int i = 0; i < n; i++)
#define rloop0(n) for(int i = n-1; i >= 0; i--)
#define iloop(s,n,step) for(int i = s; i <= n; i+=step)
#define jloop(s,n,step) for(int j = s; j <= n; j+=step)
#define loopit(x) for(auto it = x.begin(); it!= x.end(); it++)
#define all(x) x.begin(),x.end()
using namespace std;
void fast(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}
int buy[100005],sell[100005];
int main()
{
    fast();
    int tc = 1;
    //cin >> tc;
    while(tc--){
        int n,s,p,x;
        cin >> n >> s;
        char c;
        loop(n){
            cin >> c >> p >> x;
            if(c=='B') buy[p]+=x;
            else sell[p]+=x;
        }
        vector<pair<int,int>> p1,p2;
        loop0(100001){
            if(buy[i])  p1.push_back({i,buy[i]});
            if(sell[i]) p2.push_back({i,sell[i]});
        }
        sort(all(p1),greater<pair<int,int>>());
        sort(all(p2));
        int s1=p1.size(),s2=p2.size();
        for(int i = min(s,s2)-1; i > -1; i--) cout << "S " << p2[i].first << " " << p2[i].second << '\n';
        for(int i = 0; i < min(s,s1); i++) cout << "B " << p1[i].first << " " << p1[i].second << '\n';
    }
    return 0;
}
   		 	 		   		  			   			   	


Comments

Submit
0 Comments
More Questions

131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography