CSU 1554 SG Value (multiset/priority queue 思维题)
题目链接: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 思维题)的更多相关文章
- 1554: SG Value (巧妙的模拟题,也属于思维题)
1554: SG Value Submit Page Summary Time Limit: 5 Sec Memory Limit: 256 Mb Submitted: 4 ...
- find-median-from-data-stream & multiset priority queue 堆
https://leetcode.com/problems/find-median-from-data-stream/ 这道题目实在是不错,所以单独拎出来. https://discuss.leetc ...
- csu 1554: SG Value 思维题
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554 这题在比赛的时候居然没想出来,然后发现居然是做过的题目的变种!!!! 先不考虑插入操作, ...
- CSU 1554 SG Value —— 思维
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554 Description The SG value of a set (mult ...
- math --- CSU 1554: SG Value
SG Value Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1554 Mean: 一个可重集合,初始为空,每 ...
- CSU 1554 SG Value (集合类的学习)
题目大意: 2种操作 1 a:往集合中添加一个元素a 2: 询问这个集合中的元素任意组合相加所不能得到的最小数的值 这道题总是不断地去找当前所能处的最小值能否被当前的最小值加上其前部的一堆可抵达数到达 ...
- CSUOJ 1554 SG Value
1554: SG Value Time Limit: 5 Sec Memory Limit: 256 MBSubmit: 140 Solved: 35 Description The SG val ...
- STL-<queue>-priority queue的使用
简介: 优先队列是一种容器适配器,优先队列的第一个元素总是最大或最小的(自定义的数据类型需要重载运算符).它是以堆为基础实现的一种数据结构. 成员函数(Member functions) (const ...
- 优先队列(Priority Queue)
优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...
随机推荐
- 001/Docker入门(Mooc)
docker官网:https://www.docker.com/ 1.什么是docker 2.Docker思想 ==> [1].集装箱:保证程序完整(不缺东西,如配置文件等). [2]. ...
- Django报错[WinError 123] 文件名、目录名或卷标语法不正确。: '<frozen importlib._bootstrap
当你在项目文件中删除app对应的文件 却没有在项目url中删除之前配置的路径 也没有删除setting中配置的app 那么就会报错[WinError 123] 文件名.目录名或卷标语法不正确.: '& ...
- SQLServer中的Merge使用
Merge DML 作用: 数据同步 数据转换 基于源表对目标表做Insert,Update,Delete操作 Merge关键字的一些限制 使用Merge关键字只能更新一个表 源表中不能有重复的记录 ...
- POJ-1064.Cablemaster.(二分法枚举值求最优值)
题目链接 本题大意:给你n个长度为value[ i ]的长木板,让你切割成为等长的k份,问你切割的最大长度是多少. 本题思路:其实很容易可以想到先找到一个上界和一个下界,开始枚举里面的所有长度,取最长 ...
- 一份完整的 MySQL 开发规范,进大厂必看!
作者:听风 https://www.cnblogs.com/huchong/p/10219318.html 一.数据库命令规范 1.所有数据库对象名称必须使用小写字母并用下划线分割 2.所有数据库对象 ...
- [2019杭电多校第一场][hdu6578]Blank(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6578 计数问题想到dp不过分吧... dp[i][j][k][w]为第1-i位置中4个数最后一次出现的 ...
- Kotlin学习(3)类
声明类和接口: //类 class A{ } //接口,接口中的方法可以有默认实现 interface B{ fun show(){ print("i'm B") } } //用冒 ...
- 《死磕 Elasticsearch 方法论》:普通程序员高效精进的 10 大狠招!(完整版)
原文:<死磕 Elasticsearch 方法论>:普通程序员高效精进的 10 大狠招!(完整版) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链 ...
- 浅谈使用canvas绘制多边形
本文主要使用坐标轴的使用来绘制多边形,点位则都是在y轴上寻找,这种方法能够更好的理解图形与修改. //id为html里canvas标签的属性id: //x,y为坐标轴的起始位置,因为canvas默认坐 ...
- 2018-8-27-C#-powshell-调用
title author date CreateTime categories C# powshell 调用 lindexi 2018-8-27 16:20:4 +0800 2018-06-18 20 ...