1554: SG Value (巧妙的模拟题,也属于思维题)

本文介绍了一种基于multiset的数据结构实现的算法,用于处理集合的插入操作及查询集合中无法通过现有元素组合得到的最小正整数(SG值)。通过不断调整最小可能值并移除已不影响结果的最小元素,确保了算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1554: SG Value

Submit Page    Summary    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。

/*

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值