1. 程式人生 > >1554: SG Value (巧妙的模擬題,也屬於思維題)

1554: SG Value (巧妙的模擬題,也屬於思維題)

而是 剔除 define bsp ear mit imu 不能 最小數

1554: SG Value

Time Limit: 5 Sec Memory Limit: 256 Mb Submitted: 497 Solved: 167


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


題目意思:
兩種操作
1 a :往集合添加一個元素a
2:詢問這個集合中任意元素相加不能得到的最小數的值 這道題總是不斷地去找當前所能處的最小值能否被當前的最小值加上其前部的一堆可抵達數到達當前位置
也就是 minn < *s.begin() , 說明此時內部最小的元素是不影響這個值的,否則 minn+=*s.begin(),然後剔除最小值,不斷往下訪問

在這裏因為相同數據也可以同時保存在集合內,所以不采用set(會刪除重復元素),而是使用multiset
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define max_v 10005
using namespace std;
typedef long long LL;
multiset<int> s;
int main()
{
    int n;
    while(cin>>n)
    {
        s.clear();
        LL minv=1;
        for(int i=0;i<n;i++)
        {
            int op;
            cin>>op;
            if(op==1)
            {
                int v;
                cin>>v;
                s.insert(v);
            }else
            {
                while(!s.empty()&&minv>=*s.begin())
                {
                    minv+=*s.begin();
                    s.erase(s.begin());
                }
                printf("%lld\n",minv);
            }
        }
    }
    return 0;
}
/*

題目意思:
兩種操作
1 a :往集合添加一個元素a
2:詢問這個集合中任意元素相加不能得到的最小數的值

這道題總是不斷地去找當前所能處的最小值能否被當前的最小值加上其前部的一堆可抵達數到達當前位置
也就是 minn < *s.begin() , 說明此時內部最小的元素是不影響這個值的,否則 minn+=*s.begin(),然後剔除最小值,不斷往下訪問
在這裏因為相同數據也可以同時保存在集合內,所以不采用set(會刪除重復元素),而是使用multiset。

/*



1554: SG Value (巧妙的模擬題,也屬於思維題)