题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554

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 是在这个集合里任意组合,不能组合成的最小的数。例:{1, 2}可以组成[1,3]的数,所以SG Value 是4。

  有两个操作。

  1)添加一个数进入集合

  2)找到当前集合中的SG Value。

题解:

  给你一个区间[L, R], 那么我们的sg value 就是 R+1。

  一开始我们的集合是[0,0], sg value 是 1。如果我们加入2,sg value 还是1。所以我们要加入的数要小于等于 sg value 区间范围才会改变。我们就需要排个序,从小到到找。

  而这题加入了一个添加操作,只需要用优先队列或者 multiset 来维护。

  {1, 2, 5},前两个来说可以组成[1,3], 就可以把他们删除掉。

优先队列  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = ;
int n;
void solve()
{
priority_queue<LL, vector<LL>, greater<LL> >q;
LL R = ;
for(int i=;i<n;i++){
int x;
scanf("%d", &x);
if(x==){
while(!q.empty() && q.top() <= R+1LL){
R= R+q.top();
q.pop();
}
printf("%lld\n", R+1LL);
}
else{
int v;
scanf("%lld", &v);
q.push(v);
}
}
}
int main()
{
#ifdef LOCAL
freopen("jumping.in", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
while(~scanf("%d", &n)){
solve();
}
return ;
}

multiset

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = ;
int n;
void solve()
{
multiset <LL> q;
LL R = ;
for(int i=;i<n;i++){
int x;
scanf("%d", &x);
if(x==){
while(!q.empty() && *q.begin() <= R+1LL){
R= R+ *q.begin();
q.erase(q.begin());
}
printf("%lld\n", R+1LL);
}
else{
LL v;
scanf("%lld", &v);
q.insert(v);
}
}
}
int main()
{
#ifdef LOCAL
freopen("jumping.in", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
while(~scanf("%d", &n)){
solve();
}
return ;
}

总结:

  学到了一个multiset。是一个可以出现重复key的set。而且它的第一个数是最小值。(通过红黑二叉树实现的)

你努力的时候,比你厉害的人也在努力

CSU 1554 SG Value (multiset/priority queue 思维题)的更多相关文章

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

    1554: SG Value Submit Page    Summary    Time Limit: 5 Sec     Memory Limit: 256 Mb     Submitted: 4 ...

  2. find-median-from-data-stream & multiset priority queue 堆

    https://leetcode.com/problems/find-median-from-data-stream/ 这道题目实在是不错,所以单独拎出来. https://discuss.leetc ...

  3. csu 1554: SG Value 思维题

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554 这题在比赛的时候居然没想出来,然后发现居然是做过的题目的变种!!!! 先不考虑插入操作, ...

  4. CSU 1554 SG Value —— 思维

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554 Description The SG value of a set (mult ...

  5. math --- CSU 1554: SG Value

    SG Value Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1554 Mean: 一个可重集合,初始为空,每 ...

  6. CSU 1554 SG Value (集合类的学习)

    题目大意: 2种操作 1 a:往集合中添加一个元素a 2: 询问这个集合中的元素任意组合相加所不能得到的最小数的值 这道题总是不断地去找当前所能处的最小值能否被当前的最小值加上其前部的一堆可抵达数到达 ...

  7. CSUOJ 1554 SG Value

    1554: SG Value Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 140  Solved: 35 Description The SG val ...

  8. STL-<queue>-priority queue的使用

    简介: 优先队列是一种容器适配器,优先队列的第一个元素总是最大或最小的(自定义的数据类型需要重载运算符).它是以堆为基础实现的一种数据结构. 成员函数(Member functions) (const ...

  9. 优先队列(Priority Queue)

    优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...

随机推荐

  1. day15生成器send方法,递归,匿名函数,max结合匿名工作原理,常用的内置函数

    复习 ''' 1.带参装饰器 - 自定义 | wraps def wrap(info) def outer1(func): from functools import wraps @wraps(fun ...

  2. lvs三种负载均衡模式

    lvs模式 lvs nat模式 地址转换 nat模式 地址转发 ,数据全部集中在lvs处理,lvs压力大 lvs ip-tun模式 ip隧道 与nat模式差不多,差别有了隧道封装,轮询调度给后端服务器 ...

  3. GitHub Pages建立第一个静态页面

    1.创建仓库 创建仓库点击右上角的加号,选择newrepository.然后对仓库信息进行设置.注意箭头标识的几个地方.仓库名必须是http://username.github.io的形式.必须勾选p ...

  4. [Markdown] 01 简单应用 第一弹

    目录 0. "调用函数前必先声明" 0.1 Table of Content 0.2 分割线 0.3 引用 0.4 标记 0.5 关于 html 0.6 代码块 用法 1 用法 2 ...

  5. jdk与eclipse安装注意事项

    1.安装这两个软件,一定要注意使得两个软件的版本是一致的: 1.1编写小程序测试jdk的版本号: public class Text{ public static void main(String[] ...

  6. jfinal+H5的websocket 实现同一账户在不同地点不同电脑只能登陆一个(互相踢下线)

    jfinal+H5的websocket 实现同一账户在不同地点不同电脑只能登陆一个(互相踢下线):https://blog.csdn.net/liuyifeng1920/article/details ...

  7. postgresql 口令: psql: 致命错误: 用户 认证失败

    在Windows环境下使用psql 命令出现认证失败. 网上找了很多,都没讲到要点 D:\program\PostgreSql\bin>psql口令: psql: 致命错误: 用户 " ...

  8. ex2、逻辑回归

    介绍: 在本练习中,您将实现逻辑回归,并将其应用于两个不同的数据集.在开始编程练习之前,我们强烈要求建议观看视频讲座并完成相关主题的问题.要开始练习,您需要下载起始代码并将其内容解压缩到要完成练习的目 ...

  9. 【前端芝士树】Vue.js面试题整理 / 知识点梳理

    [前端芝士树] Vue.js 面试题整理 MVVM是什么? MVVM 是 Model-View-ViewModel 的缩写. Model代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑. ...

  10. CentOS安装Python3.x

    安装python3.6可能使用的依赖# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqli ...