Alex_tz307

Auction Bidding

Oct 25th, 2020 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     string s;
  8.     cin >> s;
  9.     int p = 0, price = 0;
  10.     while(isdigit(s[p]))
  11.         price = price * 10 + (s[p++] - '0');
  12.     ++p;
  13.     int buy_now = 0;
  14.     while(isdigit(s[p]))
  15.         buy_now = buy_now * 10 + (s[p++] - '0');
  16.     if(buy_now == 0)
  17.         buy_now = INF;
  18.     int N = s.size();
  19.     cout << "-," << price;
  20.     string last = "-";
  21.     int highest = price;
  22.     unordered_map < string , int > M;
  23.     for(int i = p + 1; i < N; ++i) {
  24.         int j = i;
  25.         string word;
  26.         while(isalpha(s[j]) || isdigit(s[j]))
  27.             word.push_back(s[j++]);
  28.         ++j;
  29.         int bid = 0;
  30.         while(isdigit(s[j]))
  31.             bid = bid * 10 + (s[j++] - '0');
  32.         bool ok = true;
  33.         if(bid > highest) {
  34.             highest = bid;
  35.             if(word == last)
  36.                 ok = false;
  37.             last = word;
  38.         }
  39.         if(last == "-")
  40.             ok = false;
  41.         if(ok)
  42.             cout << ',' << last << ',';
  43.         M[word] = bid;
  44.         int mx1 = 0, mx2 = 0;
  45.         for(auto it : M)
  46.             if(it.second > mx1) {
  47.                 mx2 = mx1;
  48.                 mx1 = it.second;
  49.             }
  50.             else
  51.                 if(it.second > mx2)
  52.                     mx2 = it.second;
  53.         int val = mx2;
  54.         if(val != mx1)
  55.             ++val;
  56.         if(price < val)
  57.             price = val;
  58.         if(ok) {
  59.             bool flag = false;
  60.             if(price >= buy_now) {
  61.                 flag = true;
  62.                 price = buy_now;
  63.             }
  64.             cout << price;
  65.             if(flag)
  66.                 break;
  67.         }
  68.         i = j;
  69.     }
  70. }
  71.  
Add Comment
Please, Sign In to add comment