csu 1554: SG Value

The SG value of a set (multiset) is the minimum positive integer that could not be constituted of the number in this set.
You will be start with an empty set, now there are two opertions:
1. insert a number x into the set;
2. query the SG value of current set.

Input

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1e5) -- the total number of opertions.
The next N lines contain one opertion each.
1 x means insert a namber x into the set;
2 means query the SG value of current set.

Output

For each query output the SG value of current set.

Sample Input

5
2
1 1
2
1 1
2

Sample Output

1
2
3

两种操作,1 x 向可重复集合里插入x,2  查询最小的集合里的元素相加无法

得到的数。

当集合为空是,sg的值为1;

x>sg,那么x和集合里任意的数相加的结果都大于sg,此时

插入的值对ans没有影响,直接将x放入set中。

扫描二维码关注公众号,回复: 2851735 查看本文章

x<=sg,set中比sg小的元素都对sg的成长有效果,即

sg要加上所有比sg小的数,然后将x插入set中

#include<bits/stdc++.h>
#define Mod 1000000007
#define ll long long
#define N 1100
#define INF 1010010010

using namespace std;

int n;

int main()
{
    while(~scanf("%d",&n))
    {
        priority_queue<long long,vector<long long>,greater<long long> >q;
        int op,x;long long ans=1;
        for(int i=0;i<n;i++)
        {
            cin>>op;
            if(op==2){cout<<ans<<endl;continue;}
            else{
                scanf("%d",&x);
                if(x>ans){
                    q.push(x);continue;
                }
                q.push(x);
                while(q.size()&&q.top()<=ans){
                    ans+=q.top();q.pop();
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Cworld2017/article/details/81811889
sg