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 ...
随机推荐
- Arduino可穿戴教程保存源文件与打开已经存在的源文件
Arduino可穿戴教程保存源文件与打开已经存在的源文件 Arduino IDE保存源文件 保存源文件可以通过“文件”菜单的“保存”或者快捷键Ctrl+S完成,如图2.28所示. 图2.28 保 ...
- UVALive 5135 Mining Your Own Business 双连通分量
据说这是一道Word Final的题,Orz... 原题链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&a ...
- PHP平均小数红包算法
<?php function RandMoney( $money,$num ){ $arr = array();//存放金额 $total_money = 0;//红包总金额 $thisMone ...
- 邁向IT專家成功之路的三十則鐵律 鐵律十一:IT人應對之道-靈活
身為一位優秀的IT專家,不能夠只是在技術面的應對能力強,而必須是在人事的應對能力上也要能夠靈活與彈性,否則就算一天給你48小時,你也會把自己的身心弄垮,再強的專業.技術.能力也會瞬間化為泡影. 坦白說 ...
- match excel test search replace 用法
1 test:测试string是否包含有匹配结果,包含返回true,不包含返回false. 2 reg.test(str) 3 <script type="text/javascrip ...
- 邻接表的使用及和vector的比較
这几天碰到一些对建边要求挺高的题目.而vector不好建边,所以学习了邻接表.. 以下是我对邻接表的一些看法. 邻接表的储存方式 邻接表就是就是每一个节点的一个链表,而且是头插法建的链表,这里我们首先 ...
- javascript 高级编程系列 - 继承
1. 原型链继承 (缺点:子类继承父类的引用类型的属性值会在各个实例中共享,创建子类实例时无法向父类构造函数传递参数) // 定义父类构造函数 function SuperClass(father, ...
- ARM Holdings
http://en.wikipedia.org/wiki/Advanced_RISC_Machines ARM Holdings (Redirected from Advanced RISC Mac ...
- fping - send ICMP ECHO_REQUEST packets to network hosts
NAME fping - send ICMP ECHO_REQUEST packets to network hosts SYNOPSIS fping [ options ] [ systems... ...
- Centos7安装配置ansible运维自动化工具
准备至少两台机器 Centos7,这两台机器都关闭 selinux IP:106.13.118.132 服务端(ansible) masterIP:148.70.60.244 节点 slaver 服务 ...