CSP 201412-3 集合竞价

排序+双指针,还ok

#include <bits/stdc++.h>
#define int long long
using namespace std;
struct pii {
  pair<double, int> pi;
  int type;
};
vector<pair<double, int>> buy;
vector<pair<double, int>> sell;
vector<pii> vec;
vector<bool> cac(5010, false);
signed main(){
  char op[10];
  double p;
  int fc;
  int buy_tot = 0;
  while (scanf("%s", op) != EOF) {
    if (op[0] == 'b') {
      scanf("%lf%lld",&p,&fc);
      vec.push_back({make_pair(p, fc), 0});
    } else if (op[0] == 's') {
      scanf("%lf%lld",&p,&fc);
      vec.push_back({make_pair(p, fc), 1});
    } else {
      scanf("%lld",&fc);
      cac[fc - 1] = !cac[fc - 1];
      vec.push_back({make_pair(-1.0, fc), 2});
    }
  }
  for (int i = 0; i < (int)vec.size(); i++) {
    if (!cac[i]) {
      if (vec[i].type == 0) {
        buy_tot += vec[i].pi.second;
        buy.push_back(vec[i].pi);
      } else if (vec[i].type == 1) {
        sell.push_back(vec[i].pi);
      }
    }
  }
  sort(buy.begin(), buy.end());
  sort(sell.begin(), sell.end());

  double ret = 0;
  int tot_cnt = 0;
  int cnt_buy = 0, cnt_sell = 0;
  int r = 0;
  for (int i = 0; i < (int)buy.size(); i++) {
    while (r < (int)sell.size() && sell[r].first <= buy[i].first) {
      cnt_sell += sell[r].second;
      r++;
    }
    if (r == 0) {
      cnt_buy += buy[i].second;
      continue;
    }
    if (min(cnt_sell, buy_tot - cnt_buy) == tot_cnt) {
      ret = max(ret, buy[i].first);
    } else if (min(cnt_sell, buy_tot - cnt_buy) > tot_cnt) {
      tot_cnt = min(cnt_sell , buy_tot - cnt_buy);
      ret = max(ret, buy[i].first);
    }
    cnt_buy += buy[i].second;
  }
  printf("%.2lf %lld", ret, tot_cnt);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值