【CSU1554】SG Value(优先队列)

题目链接

1554: SG Value

Submit Page    Summary    Time Limit: 5 Sec     Memory Limit: 256 Mb     Submitted: 484     Solved: 162    


Description

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

Hint

Source

 

解题思路:

设当前的 SG value 为ans,插入的值为x。

1.如果x>ans, 那么now的值不会改变。把这个数放进集合中(简单模拟一下就能知道了);

2.如果x<=ans, 那么ans += x, 由于此次ans的增大,在集合中小于等于ans的数,还能够使ans继续增大。使得ans增大之后,这个数应该从集合中删去。

然后用优先队列维护这个集合即可。

 

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
priority_queue<LL, vector<LL>, greater<LL> >q;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        LL ans=1;
        while(!q.empty())
            q.pop();
        while(n--)
        {
            LL t,x;
            scanf("%lld",&t);
            if(t==1)
            {
                scanf("%lld",&x);
                if(x>ans)
                {
                    q.push(x);
                    continue;
                }
                ans+=x;
                while(!q.empty() && q.top()<=ans)
                {
                    ans+=q.top();
                    q.pop();
                }
            }
            if(t==2)
                printf("%lld\n",ans);
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值