码迷,mamicode.com
首页 > 其他好文 > 详细

uva540-小团体队列

时间:2016-08-08 00:58:27      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

小白书里数据结构基础线性表的训练参考

 

题目链接 http://acm.hust.edu.cn/vjudge/problem/19518

 

解题思路

用到队列。大队列表示团体顺序,小队列表示团体内部顺序。

题目提示入队出队要常数时间。。。

于是用到两个映射

  成员映射到团体序号。只需开个数组存每个成员的团体编号。

  团体映射大队列里的编号。又要开个数组。最多有1000个团体,还好。。。

读入数据的时候记得忽略空格。

 

代码

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAX_LEN = 1005;
queue<int> Q[MAX_LEN];
int where[MAX_LEN]; 
int team[1000000];
int front, rear;
void enqueue(int index)
{
    int i = where[team[index]];
    if(i == -1) { 
        rear = (rear+1)%MAX_LEN;
        i = rear;
    }
    Q[i].push(index);
    where[team[index]] = i;
}
void dequeue()
{
    int temp;
    temp = Q[(front+1)%MAX_LEN].front();
    cout << temp << endl;
    Q[(front+1)%MAX_LEN].pop();
    if(Q[(front+1)%MAX_LEN].empty()) { 
        where[team[temp]] = -1;
        front = (front+1)%MAX_LEN;
    }
}
void Print(int now)
{
    cout << "Scenario #" << now << endl;
}
int main()
{
    int t, now = 1;
    cin >> t;
    front = rear = MAX_LEN-1;
    while(t != 0) {
        memset(where, -1, sizeof(where));
        for(int i=0; i<t; i++) {
            int n;
            cin >> n;
            for(int j=0; j<n; j++) {
                int number;
                cin >> number;
                team[number] = i;
            }
        }
        char c;
        while(c=getchar()!=\n) ;
        Print(now);
        char s[20];
        gets(s);
        while(strstr(s, "STOP") == NULL) {
            if(strstr(s, "ENQUEUE") != NULL) {
                int index;
                sscanf(s, "ENQUEUE %d", &index);
                enqueue(index);
            }
            else dequeue();
            gets(s);
        }
        now++;
        cout << endl;
        cin >> t;
        for(int i=0; i<MAX_LEN; i++) while(!Q[i].empty()) Q[i].pop();
        front = rear = MAX_LEN-1;
    }
}

 

uva540-小团体队列

标签:

原文地址:http://www.cnblogs.com/ZengWangli/p/5747807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!