SG Value

SG Value

Time Limit: 5 Sec Memory Limit: 256 MB
Submit: 163 Solved: 45
[ Submit][ Status][ Web Board]

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



题意:给你一个集合,动态插入 ,动态询问,然后问你这个集合的sg值(这个集合用加法运算不能产生的那个最小正整数)是多少.

解题思路:假设我们现在的这个SG值是 x  

1)现在插入集合里面一个数v   如果这个v > x ,那么显然  sg值x不变,  把v放进从小到大的优先队列中

2)如果这个 v <= x 那么sg值x肯定就会变成  x + v, 每更新一次 sg值,就去看优先队列top元素是否是 小于等于 x的 ,如果小于等于,其实就等于把这个top元素进行1操作,这样就不会错了。


#include<cstdio> 
#include<iostream> 
#include<queue> 
#include<functional> 
#define LL long long 
using namespace std; 
  
int main() 
{ 
    int N,a; 
    LL x; 
    while(scanf("%d",&N)!=EOF) 
    { 
        priority_queue<LL,vector<LL>,greater<LL> > Q; 
        while(!Q.empty()) Q.pop(); 
//       Q.push(); 
        LL v=0; 
        while(N--) 
        { 
            scanf("%d",&a); 
  
            if(a==1) 
            { 
                scanf("%lld",&x); 
                if(x<=v+1) 
                { 
                    v+=x; 
                    while(!Q.empty()) 
                    { 
                        int tp=Q.top(); 
                        if(tp<=v+1) 
                        { 
                            v+=tp; 
                            Q.pop(); 
                        } 
                        else
                            break; 
                    } 
                } 
                else
                    Q.push(x); 
            } 
            else
                printf("%lld\n",v+1); 
        } 
    } 
    return 0; 
} 
/* 
50 
1 9 
1 1 
1 3 
1 2 
2 
  
*/
  
/************************************************************** 
    Problem: 1554 
    User: aking2015 
    Language: C++ 
    Result: Accepted 
    Time:524 ms 
    Memory:2008 kb 
****************************************************************/


或者


#include<cstdio> 
#include<iostream> 
#include<queue> 
#include<functional> 
#define LL long long 
using namespace std; 
  
struct node 
{ 
    LL x; 
    friend bool operator<(node a,node b) 
    { 
         return a.x>b.x; 
    } 
}; 
  
int main() 
{ 
    int N,a; 
    LL x; 
    node ss; 
    while(scanf("%d",&N)!=EOF) 
    { 
 //       priority_queue<LL,vector<LL>,greater<LL> > Q; 
        priority_queue<node> Q; 
        while(!Q.empty()) Q.pop(); 
//       Q.push(); 
        LL v=0; 
        while(N--) 
        { 
            scanf("%d",&a); 
  
            if(a==1) 
            { 
                scanf("%lld",&x); 
                ss.x=x; 
                if(x<=v+1) 
                { 
                    v+=x; 
                    while(!Q.empty()) 
                    { 
                        ss=Q.top(); 
                        if(ss.x<=v+1) 
                        { 
                            v+=ss.x; 
                            Q.pop(); 
                        } 
                        else
                            break; 
                    } 
                } 
                else
                    Q.push(ss); 
            } 
            else
                printf("%lld\n",v+1); 
        } 
    } 
    return 0; 
} 
/* 
50 
1 9 
1 1 
1 3 
1 2 
2 
  
*/
  
/************************************************************** 
    Problem: 1554 
    User: aking2015 
    Language: C++ 
    Result: Accepted 
    Time:424 ms 
    Memory:2008 kb 
****************************************************************/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值