CSU 1554 SG Value —— 思维
题目链接: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
题解:
设当前的 SG value 为now,插入的值为x。
1.如果x>now, 那么now的值不会改变。把这个数放进集合中;
2.如果x<=now, 那么now += x, 由于此次now的增大,在集合中小于等于now的数,还能够使now继续增大。使得now增大之后,这个数应该从集合中删去。
(可以用优先队列或multiset维护这个集合)
学习之处:
升序的优先队列: priority_queue<LL, vector<LL>, greater<LL> >q;
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 20; priority_queue<LL, vector<LL>, greater<LL> >q;//优先队列的升序写法
LL n, op, x, now; void init()
{
while(!q.empty()) q.pop();
now = 1;
} void solve()
{
while(n--)
{
scanf("%lld",&op);
if(op==1)
{
scanf("%lld",&x);
if(x>now)
{
q.push(x);
continue;
} now += x;
while(!q.empty() && now>=q.top())
{
now += q.top();
q.pop();
}
}
else printf("%lld\n", now);
}
} int main()
{
while(scanf("%lld",&n)!=EOF)
{
init();
solve();
}
return 0;
}
CSU 1554 SG Value —— 思维的更多相关文章
- csu 1554: SG Value 思维题
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554 这题在比赛的时候居然没想出来,然后发现居然是做过的题目的变种!!!! 先不考虑插入操作, ...
- CSU 1554 SG Value (multiset/priority queue 思维题)
题目链接: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: 询问这个集合中的元素任意组合相加所不能得到的最小数的值 这道题总是不断地去找当前所能处的最小值能否被当前的最小值加上其前部的一堆可抵达数到达 ...
- 1554: SG Value (巧妙的模拟题,也属于思维题)
1554: SG Value Submit Page Summary Time Limit: 5 Sec Memory Limit: 256 Mb Submitted: 4 ...
- CSUOJ 1554 SG Value
1554: SG Value Time Limit: 5 Sec Memory Limit: 256 MBSubmit: 140 Solved: 35 Description The SG val ...
- CSU 1547: Rectangle (思维题加一点01背包)
1547: Rectangle Submit Page Summary Time Limit: 1 Sec Memory Limit: 256 Mb Submitted: ...
- 51nod 1554 KMP思维题
题目为中文,因而不再解释题意. 首先遵循如下设定可以有以下几个结论:1,首先谈论下KMP的一个特殊性质:对于某一个特立独行的字符串:例如ABCDEF,在建立有限状态自动机之后,都会有,所有元素的失配边 ...
- csu 1551: Longest Increasing Subsequence Again BIT + 思维
预处理last[i]表示以第i个开始,的合法后缀. pre[i]表示以第i个结尾,的合法前缀. 那么每一个数a[i],肯定是一个合法后缀last[i] + 一个合法前缀,那么合法前缀的数字要小于a[i ...
随机推荐
- UVA 11090 Going in Cycle!! SPFA判断负环+二分
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- JS没有contains方法,可以用indexof实现
我们很多时候会不自觉的在js代码里对一个字符串进行如下操作: str.contains("substr"); 但是js里面没有这个方法去判断字符串str是不是包含substr,而j ...
- 提高速度 history 的利用
history的介绍history是shell的内置命令,其内容在系统默认的shell的man手册中.history是显示在终端输入并执行的过命令,系统默认保留1000条.[root@localhos ...
- Codeforces Round #295 (Div. 1) C. Pluses everywhere
昨天ZZD大神邀请我做一道题,说这题很有趣啊. 哇,然后我被虐了. Orz ZZD 题目大意: 你有一个长度为n的'0-9'串,你要在其中加入k个'+'号,每种方案就会形成一个算式,算式算出来的值记做 ...
- Java泛型中的类型擦除机制简单理解
Java的泛型是JDK1.5时引入的.下面只是简单的介绍,不做深入的分析. Java的泛型是伪泛型.为什么说Java的泛型是伪泛型呢?因为,在编译期间,所有的泛型信息都会被擦除掉.正确理解泛型概念的首 ...
- Caused by: java.io.FileNotFoundException: rmi_keystore.jks (没有那个文件或目录)
解决方法:修改jmeter.properites: server.rmi.ssl.disable=true,关闭ssl功能 参考: https://blog.csdn.net/nielinqi520/ ...
- Toad 使用中遇到的问题
1:智能提示: 视图-->toad选项-->Editor-->Code Assist-->Toad Insight---->sort pick list alphabet ...
- NFC 标签类型
NFC 标签类型 Type 1:Type 1 Tag is based on ISO/IEC 14443A. This tag type is read and re-write capable. T ...
- 关于erlang的向上取整和向下取整
在erlang的API中,erlang:trunc/1 是就近取整,erlang:round/1是四舍五入的, 整理下:对于正数的向上和向下取整, %% 向上取整 ceil(N) -> T = ...
- 04 http协议模拟登陆发帖
<?php require('./http.class.php'); $http = new Http('http://home.verycd.com/cp.php?ac=pm&op=s ...