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 ...
随机推荐
- vue-router路由如何实现传参
tip: 用params传参,F5强制刷新参数会被清空,用query,由于参数适用路径传参的所以F5强制刷新也不会被清空.(传参强烈建议适用string) 也可以选用sessionstorage/lo ...
- MySQL-第十篇多表连接查询
1.SQL92规范.SQL99规范 2.广义笛卡尔积,多表之间没有任何连接条件,得到的结果将是N x M条记录. 3.SQL92中的左外连接.右外连接,连接符有(+或*),放在连接条件那一边就叫做左或 ...
- Codeforces 429E(欧拉回路)
题面 传送门 题目大意: 有n条线段,每条线段染红色或蓝色,使得数轴上每个点被红色线段覆盖的次数与被蓝色线段覆盖数差的绝对值小于等于1.输出染色方案. 分析 题意其实可以这样理解: 一段初始全为0 的 ...
- SCUT - 297 - 狂符「幻视调律(Visionary Tuning)」 - 重链剖分
https://scut.online/p/297 一般的树剖是关于点权的,但是突发奇想好像边权也是一样的.做一些小改动. #include<bits/stdc++.h> #define ...
- python字符串的运算有哪些
python字符串的运算有哪些 1,链接符号 + 2,判断字符串是否在某个字符串中 ‘s’ in ‘this’ 返回bool 3,字符串索引 a="this a my" a[0], ...
- git Windows终端安装教程
1.下载网址:https://gitforwindows.org/ 2.双击压缩包出现: 3.点击下一步后,选择安装路径: 根据自己的需求选择路径 4.选择安装的组件,建议全选 [每一条解析:] Ad ...
- HTML拖放元素
实现来回拖放图片 <!DOCTYPE HTML> <html> <title>来回拖放元素</title> <meta charset=" ...
- 行人重识别(ReID) ——概述
什么是Re-ID? 行人重识别(Person re-identification,简称Re-ID)也称行人再识别,是利用计算机视觉技术判断图像或者视频序列中是否存在特定行人的技术.广泛被认为是一个图像 ...
- Python2视频教程
目录 1. 说明 1.1. 马哥视频_修复v1 1.2. 马哥视频_修复v2 2. 目录 3. 下载链接 1. 说明 Python从入门到精通视频(全60集)马哥教育视频(已修复部分视频无声音的问题+ ...
- css 伪类选择器:checked实例讲解
css :checked伪类选择器介绍 css :checked伪类选择器用于选择匹配所有被选中的单选按钮(radio)或复选框(checkbox),你可以结合:checked伪类选择器和:not选择 ...