标签:优先队列 sg value
SG Value
Time Limit: 5 Sec Memory Limit:
256 MB
Submit: 163 Solved: 45
[Submit][Status][Web
Board]
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值(这个集合用加法运算不能产生的那个最小正整数)是多少.
解题思路:假设我们现在的这个SG值是 x
1)现在插入集合里面一个数v 如果这个v > x ,那么显然 sg值x不变, 把v放进从小到大的优先队列中
2)如果这个 v <= x 那么sg值x肯定就会变成 x + v, 每更新一次 sg值,就去看优先队列top元素是否是 小于等于 x的 ,如果小于等于,其实就等于把这个top元素进行1操作,这样就不会错了。
#include<cstdio>
#include<iostream>
#include<queue>
#include<functional>
#define LL long long
using namespace std;
int main()
{
int N,a;
LL x;
while(scanf("%d",&N)!=EOF)
{
priority_queue<LL,vector<LL>,greater<LL> > Q;
while(!Q.empty()) Q.pop();
// Q.push();
LL v=0;
while(N--)
{
scanf("%d",&a);
if(a==1)
{
scanf("%lld",&x);
if(x<=v+1)
{
v+=x;
while(!Q.empty())
{
int tp=Q.top();
if(tp<=v+1)
{
v+=tp;
Q.pop();
}
else
break;
}
}
else
Q.push(x);
}
else
printf("%lld\n",v+1);
}
}
return 0;
}
/*
50
1 9
1 1
1 3
1 2
2
*/
/**************************************************************
Problem: 1554
User: aking2015
Language: C++
Result: Accepted
Time:524 ms
Memory:2008 kb
****************************************************************/
或者
#include<cstdio>
#include<iostream>
#include<queue>
#include<functional>
#define LL long long
using namespace std;
struct node
{
LL x;
friend bool operator<(node a,node b)
{
return a.x>b.x;
}
};
int main()
{
int N,a;
LL x;
node ss;
while(scanf("%d",&N)!=EOF)
{
// priority_queue<LL,vector<LL>,greater<LL> > Q;
priority_queue<node> Q;
while(!Q.empty()) Q.pop();
// Q.push();
LL v=0;
while(N--)
{
scanf("%d",&a);
if(a==1)
{
scanf("%lld",&x);
ss.x=x;
if(x<=v+1)
{
v+=x;
while(!Q.empty())
{
ss=Q.top();
if(ss.x<=v+1)
{
v+=ss.x;
Q.pop();
}
else
break;
}
}
else
Q.push(ss);
}
else
printf("%lld\n",v+1);
}
}
return 0;
}
/*
50
1 9
1 1
1 3
1 2
2
*/
/**************************************************************
Problem: 1554
User: aking2015
Language: C++
Result: Accepted
Time:424 ms
Memory:2008 kb
****************************************************************/
SG Value
标签:优先队列 sg value
原文地址:http://blog.csdn.net/u010579068/article/details/44876419