poj 2051 Argus (데이터 구조: 우선권 대기 열)

7170 단어 데이터 구조
Argus
Time Limit: 1000MS
 
Memory Limit: 30000K
Total Submissions: 8312
 
Accepted: 3701
Description
A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financial tickers, on-line auctions, and transaction logs such as Web usage logs and telephone call records. Likewise, queries over streams run continuously over a period of time and incrementally return new results as new data arrives. For example, a temperature detection system of a factory warehouse may run queries like the following.
Query-1: "Every five minutes, retrieve the maximum temperature over the past five minutes."
Query-2: "Return the average temperature measured on each floor over the past 10 minutes."
We have developed a Data Stream Management System called Argus, which processes the queries over the data streams. Users can register queries to the Argus. Argus will keep the queries running over the changing data and return the results to the corresponding user with the desired frequency.
For the Argus, we use the following instruction to register a query:
Register Q_num Period
Q_num (0 < Q_num <= 3000) is query ID-number, and Period (0 < Period <= 3000) is the interval between two consecutive returns of the result. After Period seconds of register, the result will be returned for the first time, and after that, the result will be returned every Period seconds.
Here we have several different queries registered in Argus at once. It is confirmed that all the queries have different Q_num. Your task is to tell the first K queries to return the results. If two or more queries are to return the results at the same time, they will return the results one by one in the ascending order of Q_num.
Input
The first part of the input are the register instructions to Argus, one instruction per line. You can assume the number of the instructions will not exceed 1000, and all these instructions are executed at the same time. This part is ended with a line of "#".
The second part is your task. This part contains only one line, which is one positive integer K (<= 10000).
Output
You should output the Q_num of the first K queries to return the results, one number per line.
Sample Input
Register 2004 200
Register 2005 300
#
5

Sample Output
2004
2005
2004
2004
2005

Source
제목 의 이해:
(1) 모든 가입 자 는 하나의 등록 ID 와 시간 간격 이 있다.
(2) 사용 자 를 대상 으로 자신의 시간 간격 으로 ID 를 인쇄 합 니 다.
(3) \ # 설명 은 끝 에 입력 하고 사용자 가 등록 하지 않 았 습 니 다.
(4) 마지막 줄 의 숫자 는 인쇄 횟수 이다.
방법: 여기에 우선권 대기 열 을 사용 하여 모든 등록 사용 자 를 이 대기 열 에 넣 었 습 니 다. 대기 열의 순 서 는 나타 날 시간 에 따라 어 릴 때 부터 큰 순서 로 정렬 되 고 시간 이 충돌 하면 id 로 올 라 갑 니 다.이 사용자 id 가 출력 될 때마다 이 사용 자 를 대기 열 에서 팝 업 한 다음 에 이 사용자 의 출현 시간 에 시간 간격 을 더 한 다음 에 push 를 우선권 대기 열 에 넣 습 니 다. 이 대기 열 은 요구 에 따라 정렬 되 기 때문에 매번 대기 열 에 있 는 그 요 소 는 인쇄 할 ID 입 니 다.
#include <iostream>
#include <queue>
using namespace std;

//          
typedef struct
{
    int id;//  ID
    int time;//          
    int period;//    
}Register;

//            
struct cmp
{
    bool operator()(Register a,Register b)
    {
        if(a.time==b.time)
        return a.id>b.id;//       id    
        return a.time>b.time;//
    }
};

int main()
{
    //       ,          ,     id    
    priority_queue<Register,vector<Register>,cmp > re;
    string cmd;//  
    int id;//  id
    int period;//      
    int count;//      
    while(true)
    {
        cin>>cmd;
        if(cmd.compare("#")==0)break;
        cin>>id>>period;
        Register r;
        r.id = id;
        r.period = period;
        r.time = period;
        re.push(r);
    }
    cin>>count;
    while(count--)
    {
        Register reg = re.top();
        cout<<reg.id<<endl;
        reg.time += reg.period;
        re.pop();
        re.push(reg);
    }
    return 0;
}

좋은 웹페이지 즐겨찾기