题目链接: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 —— 思维的更多相关文章

  1. csu 1554: SG Value 思维题

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

  2. 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 ...

  3. math --- CSU 1554: SG Value

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

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

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

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

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

  6. CSUOJ 1554 SG Value

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

  7. CSU 1547: Rectangle (思维题加一点01背包)

    1547: Rectangle Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 256 Mb     Submitted: ...

  8. 51nod 1554 KMP思维题

    题目为中文,因而不再解释题意. 首先遵循如下设定可以有以下几个结论:1,首先谈论下KMP的一个特殊性质:对于某一个特立独行的字符串:例如ABCDEF,在建立有限状态自动机之后,都会有,所有元素的失配边 ...

  9. csu 1551: Longest Increasing Subsequence Again BIT + 思维

    预处理last[i]表示以第i个开始,的合法后缀. pre[i]表示以第i个结尾,的合法前缀. 那么每一个数a[i],肯定是一个合法后缀last[i] + 一个合法前缀,那么合法前缀的数字要小于a[i ...

随机推荐

  1. css查缺补漏1

    css可以写在哪里 1.和要装饰的标签写在一起 2.内部样式表(内嵌式)是写在head头部标签中,并且用style标签定义 3.外部样式表(外链式) <head><link rel= ...

  2. 板子-GOD BLESS ALL OF YOU

    字符串: KMP #include<cstdio> #include<cstring> ; ]; ]; int l1,l2; void get_next() { next[]= ...

  3. Android图片缓存之Bitmap详解(一)

    前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. Bitmap: Bitmap是Android ...

  4. python numpy实现多次循环读取文件 等间隔过滤数据

    numpy的np.fromfile会出现如下的问题,只能一次性读取文件的内容,不能追加读取,连续两次的np.fromfile读到的东西一样 如果数据文件太大(几个G或以上)不能一次性全读进去,需要追加 ...

  5. nopCommerce从无到有01-初探nopCommerce

    nopCommerce框架的基本结构: 该结构可以参考DDD(领域驱动设计)模式. (注:上图源自他人文章,具体出处不祥,在此引用,感谢原创) nopcommerce官方地址:http://www.n ...

  6. Android Studio Ndk 编程

    如今开发Android程序基本都已经从Eclipse转到了Android Studio了, 近期项目需求, 须要用到ndk编程, 于是就折腾了一下. 开发环境 Android Studio 1.5.1 ...

  7. 更新 手淘 flexible 布局 rem 单位适配问题

    详见链接 https://github.com/amfe/lib-flexible

  8. Linux机器间ssh免密登录

    前言 一台Linux机器通过ssh的方式连接别的机器或通过scp的方式传输文件,都需要输入密码. 为了解决每次输入密码的困扰,可采用添加密钥的方式实现. 实现过程 源服务器A,目标服务器B. 1.在源 ...

  9. 解析java.math.BigInteger类——构造函数

    最早由于做作业,结识了java的BigInrger类.读着读着,越来越觉得有趣.后来作业做完,也不忍丢下它,索性把全部代码研究一遍. 开始的时候,一个上午时间最多读懂2个方法.但是还是有滋有味的坚持了 ...

  10. Java中Class.forName()的作用(转载)

    http://www.360doc.com/content/10/0712/10/1720440_38421273.shtml# 使用jdbc方式连接数据库时会使用一句代码Class.forName( ...