csu 1554: SG Value

来源:互联网 发布:java无参构造方法 编辑:程序博客网 时间:2024/05/21 14:43

1554: SG Value

Time Limit: 5 Sec  Memory Limit: 256 MB
Submit: 336  Solved: 105
[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

521 121 12

Sample Output

123

HINT


规律自己可以推

一开始,ans=1

插入x,当且仅当x<=1时,ans才会变,ans>1的话合成不了1

->ans=2

插入x,当且仅当x<=2时,ans才会变,ans>2的话合成不了2

->ans=ans+x

...


#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#define Mod 1000000007#define ll long long#define N 1100#define INF 1010010010using namespace std;int n;int main() {    //freopen("in.txt","r",stdin);    while(~scanf("%d",&n)) {        int op,x;        ll ans=1;        priority_queue<int,vector<int>,greater<int> >q;        while(n--) {            scanf("%d",&op);            if(op==2) {                printf("%lld\n",ans);                continue;            }            scanf("%d",&x);            if(x>ans) {                q.push(x);                continue;            }            q.push(x);            while(q.size()&&ans>=q.top()) {                ans+=q.top();                q.pop();            }        }    }    return 0;}



0 0
原创粉丝点击