2015年山東省第六屆ACM大學生程序設計競賽-B-Lowest Unique Price

Link:

http://www.sdutacm.org/sdutoj/problem.php?action=showproblem&problemid=3252

Lowest Unique Price

Time Limit: 1000ms   Memory limit:65536K  

題目描述

  Recently mybuddies and I came across an idea! We want to build a website to sell things ina new way.
  For each product, everyone could bid at a price, orcancel 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 thelowest price that was called only once.
  So we need aprogram to find the "lowest unique price", We'd like to write aprogram to process the customers' bids and answer the 
  query of what's thecurrent lowest unique price.
  All what we neednow is merely a programmer. We will give you an "Accepted" as long asyou help us to write the program.

輸入

  The first line ofinput 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": acustomer has canceled his bid at price x.
  "q" :means "Query". You should print the current lowest unique price.
  Our customers arehonest, they won\'t cancel the price they didn't bid at.

輸出

  Please print the current lowest unique price forevery query ("q"). Print "none" (without quotes) if thereis 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

提示

   這個題的大意是,有很多顧客投標和取消投標,每次查詢給出不重複的投標的最小值。
   比如投標有(1,1,2,2,3,4,4,5),那麼查詢之後就顯示3,如果進行取消比如取消一個2,那麼投標有(1,1,2,3,4,4,5),那麼查詢之後
   就是2,如果再增加2,3,5那麼就沒有不重複的最小值,那麼就顯示”none”.這樣我們想到利用STL裏面的priority_queue進行一定的修改。

代碼

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
using namespace std;
const int maxx(1e6+10);/****所有可能出現的投標值得最大值***/
int now[maxx];/***標記某個值在pq這個優先隊列中的個數***/
int _minus[maxx];/***標記某個在pq這個優先隊列中,還要取消的次數***/
priority_queue <int > pq;
priority_queue <int > tmp;
void init()/***僅作初始化***/
{
    memset(now,0,sizeof(now));
    memset(_minus,0,sizeof(_minus));
    while(!pq.empty())
        pq.pop();
    while(!tmp.empty())
        tmp.pop();
}
void query()
{
    int _now;
    while(!pq.empty())
    {
        _now=pq.top();
        if(_minus[-_now]>0)/****如果還有要進行取消,那麼就把他彈出去,並且這一定不是求的點****/
        {
            now[-_now]--;
            _minus[-_now]--;
            pq.pop();
        }
        else
        {
            if(now[_now]==1)/****如果在隊列中只有一個,並且不需要取消,那麼這必然是要求的點****/
            {
                cout<<-_now<<endl;
                break;
            }
            else
            {
                tmp.push(_now);/****此時證明要求的點如果存在的話,應該在當前值的後面,把當前值彈出,
                然後存在tmp這個優先隊列中****/
                pq.pop();
            }
        }
    }
    if(pq.empty())/***如果已經彈空了還是沒有找到,則證明不存在***/
    {
        puts("none");
    }
    while(!tmp.empty())/****把存在優先隊列裏的節點全部彈回pq這個優先隊列******/
    {
        _now=tmp.top();
        tmp.pop();
        pq.push(_now);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    char cmd;
    int a;
    while(t--)
    {
        int n;
        scanf("%d",&n);
        init();
        while(n--)
        {
            scanf(" %c",&cmd);
            switch(cmd)
            {
            case 'b':/****投標時,把這個數放入優先隊列pq,在優先隊列pq中的個數++****/
                scanf("%d",&a);
                now[a]++;
                pq.push(-a);/****因爲優先隊列是從大到小排的,每次摘出來的都是最大的,因此我們取一個相反數****/
                break;
            case 'c':
                scanf("%d",&a);
                _minus[a]++;/****記錄要取消還沒有取消的次數****/
                break;
            default :
                query();
                break;
            }
        }
    }
    return 0;
}
/************************************** 
 Problem id    : SDUT OJ 3252  
 User name     : crawl  
  Result       : Accepted  
  Take Memory  : 8540K  
  Take Time    : 720MS  
  Submit Time  : 2016-05-26 20:13:46  
**************************************/  
發佈了37 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章