csu 1554: SG Value 思维题

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554

这题在比赛的时候居然没想出来,然后发现居然是做过的题目的变种!!!!

先不考虑插入操作,就给定一堆数字,求出不能组成的最小的那个正数。

思路是,排序,然后维护一个区间[L, R]表示当前能组合成的数字。比如1、2就能组合成[1, 3]的所有数字。

那么下一个数a_i,我们需要其不能大于R + 1,否则会断,R + 1就是不能组合成的最小数字,比如a_i = 5就GG。

那么这题增加了插入操作。

我们只需要维护其递增排序,查询的时候,按照上面思路查询就好了。

比如1、2、5,那么前两个数可以组合成[1, 3],就可以把那两个数字pop掉了,不用重复计算。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
multiset<LL>ss;
int n;
void work() {
    LL R = 0;
    ss.clear();
    for (int i = 1; i <= n; ++i) {
        int op;
        cin >> op;
        if (op == 2) {
            while (ss.size() && *ss.begin() <= R + 1) {
                R = R + *ss.begin();
                ss.erase(ss.begin());
            }
            cout << R + 1 << endl;
        } else {
            LL val;
            cin >> val;
            ss.insert(val);
        }
    }
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    while (cin >> n) work();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6738258.html