Codeforces 1089D Eels (看题解)

Eels

感觉想不出来这种东西。。 题解讲的很清楚啦。 我好lj啊。

https://codeforces.com/blog/entry/64331

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 5e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

int q, x, n;
char op[3];
multiset<int> Set[31];
int mn[31][2];
LL sum[31];

void Add(int x) {
    int p = 0;
    while((1 << p) <= x) p++;
    Set[p].insert(x);
    sum[p] += x;
    n++;
    auto it = Set[p].begin();
    mn[p][0] = *it;
    if(SZ(Set[p]) > 1) ++it, mn[p][1] = *it;
}

void Del(int x) {
    int p = 0;
    while((1 << p) <= x) p++;
    Set[p].erase(Set[p].lower_bound(x));
    sum[p] -= x;
    n--;
    auto it = Set[p].begin();
    if(SZ(Set[p]) > 0) mn[p][0] = *it;
    if(SZ(Set[p]) > 1) ++it, mn[p][1] = *it;
}

int calc() {
    if(!n) return 0;
    LL prefix = 0;
    int ret = 0;
    for(int i = 1; i < 31; i++) {
        if(!SZ(Set[i])) continue;
        LL nprefix = prefix + sum[i];
        for(int j = 0; j < min(2, SZ(Set[i])); j++) {
            if(prefix && 2 * prefix < mn[i][j]) {
                ret++;
                break;
            }
            prefix += mn[i][j];
        }
        prefix = nprefix;
    }
    return n - ret - 1;
}

int main() {
    scanf("%d", &q);
    while(q--) {
        scanf("%s%d", op, &x);
        if(*op == '+') Add(x);
        else Del(x);
        printf("%d
", calc());
    }
    return 0;
}

/*
*/