【习题 5-14 UVA - 1598】Exchange
生活随笔
收集整理的這篇文章主要介紹了
【习题 5-14 UVA - 1598】Exchange
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【鏈接】 我是鏈接,點我呀:)
【題意】
在這里輸入題意
【題解】
各組數據之間有空行!
且最后一行后面沒有空行!
然后就是用set來模擬就好。
刪除的時候,不著急刪除。
因為并不用時刻輸出集合大小。所以只要遇到了把它刪掉就Ok.
把相同的合并那里。我直接暴力合并了。
因為
150 30
100 30
不能看出一個整體的250 30的。。
要分步輸出Trade信息的.
然后在合并的時候也要注意看里面有沒有已經刪除了的。
已經刪除了的就直接跳過。。
【代碼】
#include <bits/stdc++.h> using namespace std;const int N = 1e4;struct abc {int num, price;abc(int x = 0, int y = 0) :num(x), price(y) {} };abc re[N + 10];struct setcmp1 {bool operator () (const int &a, const int &b){if (re[a].price == re[b].price)return a < b;elsereturn re[a].price > re[b].price;} }; struct setcmp2 {bool operator () (const int &a, const int &b){if (re[a].price == re[b].price)return a < b;elsereturn re[a].price < re[b].price;} };set <int, setcmp1> buyset;//買的價格,越大越好 set <int, setcmp2> sellset;//賣的價格,越小越好 bool dele[N + 10];//用來記錄某個交易是否被刪除了。并不用時刻輸出集合大小。所以只要遇到了把它刪掉就Ok int n;void cl() {bool shan1, shan2;do{shan1 = shan2 = false;while (buyset.size() > 1 && dele[*buyset.begin()]) buyset.erase(buyset.begin()), shan1 = true;while (sellset.size() > 1 && dele[*sellset.begin()]) sellset.erase(sellset.begin()), shan2 = true;} while (shan1 || shan2); }void out() {int num1 = 0, num2 = 0;int price1 = re[*buyset.begin()].price, price2 = re[*sellset.begin()].price;for (auto it : buyset)if (re[it].price == price1){if (!dele[it]) num1 += re[it].num;}else break;for (auto it : sellset)if (re[it].price == price2){if (!dele[it]) num2 += re[it].num;}else break;printf("QUOTE %d %d - %d %d\n", num1, price1, num2, price2); }int main() {//freopen("F:\\rush.txt", "r", stdin);int kk = 0;while (~scanf("%d", &n)){if (kk > 0) puts("");kk++;re[N + 1].num = 0, re[N + 1].price = 0;re[N + 2].num = 0, re[N + 2].price = 99999;memset(dele, 0, sizeof dele);buyset.clear(); sellset.clear();buyset.insert(N + 1); sellset.insert(N + 2);for (int i = 1; i <= n; i++){char s[10];scanf("%s", s);switch (s[0]){case ('C'):{int x;scanf("%d", &x);dele[x] = true;cl();//看看隊首是不是要刪掉 out();break;}case ('B'):{//買進int num, price;scanf("%d%d", &num, &price);while (sellset.size() > 1 && num >0 && price >= re[*sellset.begin()].price){int temp = min(re[*sellset.begin()].num, num), temp2 = re[*sellset.begin()].price;num -= temp;re[*sellset.begin()].num -= temp;if (re[*sellset.begin()].num == 0){sellset.erase(sellset.begin());cl();}printf("TRADE %d %d\n", temp, temp2);}re[i] = abc(num, price);if (num != 0) buyset.insert(i);out();break;}case 'S':{//賣int num, price;scanf("%d%d", &num, &price);while (buyset.size() > 1 && num >0 && price <= re[*buyset.begin()].price){int temp = min(re[*buyset.begin()].num, num), temp2 = re[*buyset.begin()].price;num -= temp;re[*buyset.begin()].num -= temp;if (re[*buyset.begin()].num == 0){buyset.erase(buyset.begin());cl();}printf("TRADE %d %d\n", temp, temp2);}re[i] = abc(num, price);if (num != 0) sellset.insert(i);out();break;}default:break;}}}return 0; }轉載于:https://www.cnblogs.com/AWCXV/p/7686073.html
總結
以上是生活随笔為你收集整理的【习题 5-14 UVA - 1598】Exchange的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QT | QT MSVC 2015 +
- 下一篇: 删除排序数组中的重复数字 II