1. 程式人生 > >2015年山東省第六屆ACM大學生程式設計競賽-B-Lowest Unique Price

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  
**************************************/  

相關推薦

2015山東省ACM大學生程式設計競賽-B-Lowest Unique Price

Link: http://www.sdutacm.org/sdutoj/problem.php?action=showproblem&problemid=3252 Lowest Unique Price Time Limit: 1000ms

SDUT 3258 Square Number(2015山東省ACM大學生程式設計競賽)

Square Number Time Limit: 1000ms   Memory limit: 65536K  有疑問?點這裡^_^ 題目描述 In mathematics,

“浪潮杯”山東省ACM大學生程式設計競賽I

Fascinated with the computer games, Gabriel even forgets to study. Now she needs to finish her homework, and there is an easy problem: f(n)= She is requir

“浪潮杯”山東省ACM大學生程式設計競賽 J

sell goods whose price with order as -1, 5, 6, 6, the total benefit would be -1*1 + 5*2 + 6*3 + 6*4 = 51. 01 #include <iostream>

山東省ACM大學生程式設計競賽 總結

很惋惜的一場比賽,比賽前我想了很多個結局,可能是銀牌中等,銀牌末尾,要是運氣好的可能能混一個金牌回來。。但是銀首這個真的是超乎我意料之外。。。 開始比賽的不久我就看到了K題,一個作為簽到題的存在,開始習慣性的先敲輸入輸出。寫完輸入輸出之後感覺沒有比較好寫的思路

Fruit Ninja II(山東省ACM大學生程式設計競賽

Have you ever played a popular game named "Fruit Ninja"? Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for Nvidia Tegra 2 based A

山東省ACM大學生程式設計競賽 Fruit Ninja I(01揹包)

01揹包 加上vector的運用,貪心sort排序 #include <stdio.h> #include <vector> #include <string.h>

2017河南省ACM大學生程式設計競賽總結

河南第十屆大學生程式設計競賽總結             比賽過程隊友部落格有記錄:http://blog.csdn.net/dreamNYC/article/details/71417450             主要就總結下這次比賽的感受:                     我們隊雖然和

[2012山東省ACM大學生程序設計競賽]——Mine Number

== memory cat chan get iostream sts 設計 target Mine Number 題目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&proble

沒有什麼不可能—記山東省ACM程式設計競賽(退役總結帖)

          大一下學期,第一次聽說了ACM這個詞,當時每週六也開設了培訓課,但我好像一次也沒有去過,當時對這個詞並沒有什麼太大的印象。後來學院裡引進了自己的OJ,那時候我連基本的輸入輸出格式都不懂,當經歷了一

2017山東省acm省賽總結

        作為一個即將退役的大三隊員,這屆省賽對我來說就是最後一次比賽了,同時也是最重要的一次比賽。在去年的省賽中,我們隊發揮的不理想,沒拿到獎牌,因此今年就是最後的機會,我們隊在這一年中也準備了很多,其中也有過放棄,失望等時期,不過好在最後都堅持下來了。其中對我來說

山東省ACM省賽 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 ea

2010山東省第一ACM大學生程式設計競賽——Greatest Number

Greatest Number 題目意思:給你一串數,任取四個,可以重複,求最大值,但不能超過m。 可以先將任意兩個數相加,然後再二分查詢求出符合題意的最大值即可。 #include<i

山東省第一ACM大學生程式設計競賽 Phone Number 字典樹

Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑問?點這裡^_^ 題目描述 We know that if a phone number A is another phone number B’s

[2010山東省第一ACM大學生程式設計競賽]——Hello World!

Hello World!題目描述 We know that Ivan gives Saya three problems to solve (Problem F), and this is the first problem. “We need a programmer t

福建省大學生程式設計競賽-FZU 2280 HASH處理+暴力搜尋

題目:Problem 2280 Magic  Problem Description Kim is a magician, he can use n kinds of magic, number from 1 to n. We use string Si to describe m

福建省大學生程式設計競賽-FZU 2277 DFS +線段樹+讀入掛

FZU 2277  Problem 2277 Change Accept: 245    Submit: 1186 Time Limit: 2000 mSec    Memory Limit : 262

2018山東省ACM大學生程式設計競賽B.Bullet

In GGO, a world dominated by gun and steel, players are fighting for the honor of being the strongest gunmen. Player Shino is a sniper, and her aimed shot

ACM趣味程式設計競賽四場(正式賽)A B C

Yitong_Qin and Xiaoyu_Chen are playing a game.There are $N$ stones placed on the ground,forming a sequence. Thr stones are labeled from 1 to N. Yitong_Qin

ACM趣味程式設計競賽三場(正式賽)官方題解

Source:LinPC 題解:首先預處理統計a-z字元的數量,根據漸變字串的定義,由於總字元數較小,所以可以列舉漸變字串的起點,然後找到由該起點起始的最大漸變字串,然後從統計的字元中刪除所用字元,如此迴圈操作至剩餘字元數為0即可 題解: 易得,只要最長的那條