第六届山东省ACM竞赛 B题 Lowest Unique Price

Lowest Unique Price

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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.
 

输入

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 ≤ 10 6) 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.
 

输出

 Please print the current lowest unique price for every query ("q"). Print "none" (without quotes) if there is no lowest unique price.

示例输入

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

示例输出

none 
none 
4 
3 
2

使用set和map求解

题意:执行三个操作,添加竞价,删除竞价,输出个数为1的最小竞价



#include <cstdio>
#include <set>
#include <map>
using namespace std;

int main()
{
    set<int> myset;
    map<int, int> mymap;
    char c;
    int t, n, x;
    scanf("%d", &t);
    while (t--){
        myset.clear();   //删除set容器中的所有元素
        mymap.clear();
        scanf("%d", &n);
        for (int i = 0; i < n; i++){
            scanf(" %c", &c);
            if (c == 'b'){
                scanf("%d", &x);
                myset.insert(x);   //将值插入到set容器中
                mymap[x]++;
                if (mymap[x] > 1)
                    myset.erase(x);  //多于一个,删除此元素
            }
            else if (c == 'c'){
                scanf("%d", &x);
                mymap[x]--;
                if (mymap[x] == 1)
                    myset.insert(x);
                else if (mymap[x] == 0)
                    myset.erase(x);
            }
            else{
                if (!myset.empty())
                    printf("%d\n", *myset.begin());  //如果不为空,输出第一个元素
                else
                    printf("none\n");
            }
        }
    }
    return 0;
}


你可能感兴趣的:(第六届山东省ACM竞赛 B题 Lowest Unique Price)