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中。

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();
                }
            }
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值