zoukankan      html  css  js  c++  java
  • UVA 1598 Exchange

    题目链接:https://vjudge.net/problem/UVA-1598

    题目大意

    分析

      巨烦的模拟题,debug一天。。。
      要点:
    1. 由于交易量可以取到 99999,所以交易量上限设 100000 比较好。
    2. 每次交易只可以交易一个条目,不能多个条目合并在一起交易。
    3. CANCEL 语句会有重复。
    4. 如果来的是 SELL 条目,就是卖方主动;如果来的是 BUY 条目,就是买方主动。会影响实际售价。

    代码如下

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3  
      4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
      5 #define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
      6 #define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
      7 #define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
      8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
      9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
     10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
     11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
     12  
     13 #define pr(x) cout << #x << " = " << x << "  "
     14 #define prln(x) cout << #x << " = " << x << endl
     15  
     16 #define LOWBIT(x) ((x)&(-x))
     17  
     18 #define ALL(x) x.begin(),x.end()
     19 #define INS(x) inserter(x,x.begin())
     20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
     21 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 
     22 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
     23 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
     24  
     25 #define ms0(a) memset(a,0,sizeof(a))
     26 #define msI(a) memset(a,0x3f,sizeof(a))
     27 #define msM(a) memset(a,-1,sizeof(a))
     28 
     29 #define MP make_pair
     30 #define PB push_back
     31 #define ft first
     32 #define sd second
     33  
     34 template<typename T1, typename T2>
     35 istream &operator>>(istream &in, pair<T1, T2> &p) {
     36     in >> p.first >> p.second;
     37     return in;
     38 }
     39  
     40 template<typename T>
     41 istream &operator>>(istream &in, vector<T> &v) {
     42     for (auto &x: v)
     43         in >> x;
     44     return in;
     45 }
     46 
     47 template<typename T>
     48 ostream &operator<<(ostream &out, vector<T> &v) {
     49     Rep(i, v.size()) out << v[i] << " 
    "[i == v.size() - 1];
     50     return out;
     51 }
     52  
     53 template<typename T1, typename T2>
     54 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
     55     out << "[" << p.first << ", " << p.second << "]" << "
    ";
     56     return out;
     57 }
     58 
     59 inline int gc(){
     60     static const int BUF = 1e7;
     61     static char buf[BUF], *bg = buf + BUF, *ed = bg;
     62     
     63     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
     64     return *bg++;
     65 } 
     66 
     67 inline int ri(){
     68     int x = 0, f = 1, c = gc();
     69     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
     70     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
     71     return x*f;
     72 }
     73 
     74 template<class T>
     75 inline string toString(T x) {
     76     ostringstream sout;
     77     sout << x;
     78     return sout.str();
     79 }
     80 
     81 inline int toInt(string s) {
     82     int v;
     83     istringstream sin(s);
     84     sin >> v;
     85     return v;
     86 }
     87 
     88 //min <= aim <= max
     89 template<typename T>
     90 inline bool BETWEEN(const T aim, const T min, const T max) {
     91     return min <= aim && aim <= max;
     92 }
     93 
     94 typedef unsigned int uI;
     95 typedef long long LL;
     96 typedef unsigned long long uLL;
     97 typedef vector< int > VI;
     98 typedef vector< bool > VB;
     99 typedef vector< char > VC;
    100 typedef vector< double > VD;
    101 typedef vector< string > VS;
    102 typedef vector< LL > VL;
    103 typedef vector< VI > VVI;
    104 typedef vector< VB > VVB;
    105 typedef vector< VS > VVS;
    106 typedef vector< VL > VVL;
    107 typedef vector< VVI > VVVI;
    108 typedef vector< VVL > VVVL;
    109 typedef pair< int, int > PII;
    110 typedef pair< LL, LL > PLL;
    111 typedef pair< int, string > PIS;
    112 typedef pair< string, int > PSI;
    113 typedef pair< string, string > PSS;
    114 typedef pair< double, double > PDD;
    115 typedef vector< PII > VPII;
    116 typedef vector< PLL > VPLL;
    117 typedef vector< VPII > VVPII;
    118 typedef vector< VPLL > VVPLL;
    119 typedef vector< VS > VVS;
    120 typedef map< int, int > MII;
    121 typedef unordered_map< int, int > uMII;
    122 typedef map< LL, LL > MLL;
    123 typedef map< string, int > MSI;
    124 typedef map< int, string > MIS;
    125 typedef multiset< int > mSI;
    126 typedef set< int > SI;
    127 typedef stack< int > SKI;
    128 typedef deque< int > DQI;
    129 typedef queue< int > QI;
    130 typedef priority_queue< int > PQIMax;
    131 typedef priority_queue< int, VI, greater< int > > PQIMin;
    132 const double EPS = 1e-8;
    133 const LL inf = 0x7fffffff;
    134 const LL infLL = 0x7fffffffffffffffLL;
    135 const LL mod = 1e9 + 7;
    136 const int maxN = 1e4 + 7;
    137 const LL ONE = 1;
    138 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
    139 const LL oddBits = 0x5555555555555555;
    140 
    141 struct Order {
    142     int id;
    143     int price;
    144     int num;
    145     
    146     Order(int x, int y, int z) {
    147         id = x;
    148         num = y;
    149         price = z;
    150     }
    151 };
    152 
    153 int N, T; 
    154 vector< Order > orders; // 存放指令 
    155 uMII orderMap; // 指令序号->数组下标,序号唯一 
    156 
    157 struct BuyOrderCompare {
    158     bool operator() (int x, int y) {
    159         //assert(orderMap.find(x) != orderMap.end());
    160         //assert(orderMap.find(y) != orderMap.end());
    161         Order &X = orders[orderMap[x]];
    162         Order &Y = orders[orderMap[y]];
    163         
    164         return X.price > Y.price || X.price == Y.price && x < y;
    165     }
    166 };
    167 
    168 struct SellOrderCompare {
    169     bool operator() (int x, int y) {
    170         //assert(orderMap.find(x) != orderMap.end());
    171         //assert(orderMap.find(y) != orderMap.end());
    172         Order &X = orders[orderMap[x]];
    173         Order &Y = orders[orderMap[y]];
    174         
    175         return X.price < Y.price || X.price == Y.price && x < y;
    176     }
    177 };
    178 
    179 set< int, BuyOrderCompare > buyQueue;     // 购买队列 
    180 set< int, SellOrderCompare > sellQueue;   // 售卖队列 
    181 map< int, int, greater< int > > maxBuy;   // key为价格,value为对应数量,设这两个map是为了方便输出Quote 
    182 MII minSell;
    183 
    184 void init() {
    185     orders.clear();
    186     orders.PB(Order(-1, 0, 0));
    187     orders.PB(Order(-2, 0, 100000));
    188     
    189     orderMap.clear();
    190     orderMap[-1] = 0;
    191     orderMap[-2] = 1;
    192     
    193     buyQueue.clear();
    194     buyQueue.insert(-1);
    195     sellQueue.clear();
    196     sellQueue.insert(-2);
    197     
    198     maxBuy.clear();
    199     maxBuy[0] = 0;
    200     minSell.clear();
    201     minSell[100000] = 0;
    202 }
    203 
    204 void cancel(int x) {
    205     //assert(x != -1 && x != -2);
    206     if(orderMap.find(x) != orderMap.end()) {
    207         auto &X = orders[orderMap[x]];
    208         
    209         if(buyQueue.find(x) != buyQueue.end()) {
    210             maxBuy[X.price] -= X.num;
    211             if(!maxBuy[X.price]) maxBuy.erase(X.price); // 删掉后如果 X.price 所对应的数量为0,则删去X.price 
    212             
    213             buyQueue.erase(x);
    214         }
    215         else {
    216             minSell[X.price] -= X.num;
    217             if(!minSell[X.price]) minSell.erase(X.price);
    218             
    219             sellQueue.erase(x);
    220         }
    221         orderMap.erase(x); // 这个要最后删 
    222     }
    223 }
    224 
    225 bool Trade(bool isbuy) {
    226     PII ret(0, 0);
    227     Order &X = orders[orderMap[*buyQueue.begin()]];
    228     Order &Y = orders[orderMap[*sellQueue.begin()]];
    229     
    230     if(!X.num || !Y.num || X.price < Y.price) return false;
    231     
    232     if(!isbuy) ret.sd = X.price;
    233     else ret.sd = Y.price;
    234     ret.ft = min(X.num, Y.num);
    235     
    236     if(X.num > Y.num) {
    237         X.num -= Y.num;
    238         maxBuy[X.price] -= Y.num; // 更新 
    239         cancel(Y.id);             // 没货了可以删掉,不影响答案 
    240     }
    241     else if(X.num < Y.num) {
    242         Y.num -= X.num;
    243         minSell[Y.price] -= X.num; // 更新 
    244         cancel(X.id);              // 没货了可以删掉,不影响答案 
    245     }
    246     else {
    247         cancel(X.id);
    248         cancel(Y.id);
    249     }
    250     
    251     printf("TRADE %d %d
    ", ret.ft, ret.sd);
    252     return true;
    253 }
    254 
    255 void printQuote() {
    256     auto itBuy = maxBuy.begin();
    257     auto itSell = minSell.begin();
    258     //assert(itBuy != maxBuy.end() && itSell != minSell.end());
    259     printf("QUOTE %d %d - %d %d
    ", itBuy->sd, itBuy->ft, itSell->sd, itSell->ft == 100000 ? itSell->ft - 1 : itSell->ft);
    260 }
    261 
    262 int main() {
    263     //freopen("MyOutput.txt","w",stdout);
    264     //freopen("input.txt","r",stdin);
    265     //INIT();
    266     char ch[6];
    267     while(~scanf("%d", &N)) {
    268         if(T++) cout << endl;
    269         init(); // 初始化 
    270         For(i, 1, N) {
    271             scanf("%s", ch);
    272             if(ch[0] == 'B') {
    273                 int sz, price;
    274                 scanf("%d%d", &sz, &price);
    275                 
    276                 orders.PB(Order(i, sz, price));
    277                 orderMap[i] = orders.size() - 1;
    278                 buyQueue.insert(i);
    279                 
    280                 maxBuy[price] += sz;
    281                 
    282                 while(Trade(true)) ;
    283                 
    284                 printQuote();
    285             }
    286             else if(ch[0] == 'S') {
    287                 int sz, price;
    288                 scanf("%d%d", &sz, &price);
    289                 
    290                 orders.PB(Order(i, sz, price));
    291                 orderMap[i] = orders.size() - 1;
    292                 sellQueue.insert(i);
    293                 
    294                 minSell[price] += sz;
    295                 
    296                 while(Trade(false)) ;
    297                 
    298                 printQuote();
    299             }
    300             else {
    301                 int id;
    302                 scanf("%d", &id);
    303                 
    304                 cancel(id);
    305                 
    306                 printQuote();
    307             }
    308         }
    309     }
    310     return 0;
    311 }
    View Code
  • 相关阅读:
    Github开源人脸识别项目face_recognition
    yolo源码解析(3):进行简单跳帧
    [转]详细解读TrueSkill 排名系统
    yolo源码解析(3):视频检测流程
    Java中replace和replaceall的区别
    对input输入框日期显示格式化
    Eclipse修改编码字体
    Eclipse修改控制台字体
    Eclipse文档注释导出doc
    数组的定义方式及初始化
  • 原文地址:https://www.cnblogs.com/zaq19970105/p/11384264.html
Copyright © 2011-2022 走看看