Lowest Unique Price
Problem Description
Recently my buddies and I came across an idea! We want to build a website to sell things in a new way.
For each product, everyone could bid at a price, or cancel his previous bid, finally we sale the product to the one who offered the "lowest unique price". The lowest unique price is defined to be the lowest price that was called only once.
So we need a program to find the "lowest unique price", We'd like to write a program to process the customers' bids and answer the query of what's the current lowest unique price.
All what we need now is merely a programmer. We will give you an "Accepted" as long as you help us to write the program.
Input
The first line of input contains an integer T, indicating the number of test cases (T ≤ 60).
Each test case begins with a integer N (1 ≤ N ≤ 200000) indicating the number of operations.
Next N lines each represents an operation.
There are three kinds of operations:
"b x": x (1 ≤ x ≤ 106) is an integer, this means a customer bids at price x.
"c x": a customer has canceled his bid at price x.
"q" : means "Query". You should print the current lowest unique price.
Our customers are honest, they won\'t cancel the price they didn't bid at.
Output
Please print the current lowest unique price for every query ("q"). Print "none" (without quotes) if there is no lowest unique price.
Example Input
2 3 b 2 b 2 q 12 b 2 b 2 b 3 b 3 q b 4 q c 4 c 3 q c 2 q
Example Output
none none 4 3 2
依次出价,随时可能撤销出价,随时可能询问当前最低且为唯一的价格
b:添加元素,c:删除指定元素(若有多个相同元素只删除一个),q:询问当前最小且唯一的元素
思路:
利用映射记录当前元素出现的次数(b:就增加一次,c:就减少一次),当出现次数为一时就将此元素丢进Set,否则将该元素擦除。最后询问时利用set的有序性输出set的第一个元素即可。
他既然问的是是1的价格,那么我们就不管其余的价格,只把都是1的价格维护起来就好了。。。如果把其余的也弄进来,找为1的时候 会超时。。。
#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<cstring>
using namespace std;
int main(void)
{
int t, n;
cin >> t;
while(t--)
{
scanf("%d", &n);
set<int> s;
map<int ,int> m;
s.clear();
m.clear();
while(n--)
{
char ch;
int x;
scanf(" %c", &ch);
if(ch == 'b')
{
scanf("%d", &x);
if(m[x] == 0) //是0说明即将是1 把他插入
{
m[x] = 1;
s.insert(x);
}
else
{
if(m[x] == 1) s.erase(x); //如果即将不是1 扔掉
m[x]++;
}
}
else if(ch == 'c')
{
scanf("%d", &x);
if(m[x] == 1)
{
m[x] = 0;
s.erase(x);
}
else
{
m[x]--;
if(m[x] == 1) s.insert(x);
}
}
else
{
if(s.size() == 0) puts("none");
else printf("%d\n", *(s.begin()));
}
}
}
return 0;
}