UVA540 TeamQueue

双队列的思想去实现 TeamQueue

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * Created by 95112 on 10/20/2017.
 */
public class TeamQueueSecond {
    public static void main(String[] args)
    {

        Scanner scanner = new Scanner(System.in);
        ArrayList<ArrayList<String>> answers = new ArrayList<>();
        int count = 1;
        do {

            LinkedList<Integer>[] teamList;
            LinkedList<Integer> mList = new LinkedList<>();
            int t = scanner.nextInt();
            if (t == 0)
                break;

            ArrayList<String> tmpArrayList = new ArrayList<>();
            tmpArrayList.add("Scenario #"+count);


            teamList = new LinkedList[t];
            HashMap<Integer,Integer> map = new HashMap<>();
            for (int i = 0 ; i < t ; i++)
            {
                int tmp = scanner.nextInt();
                while (tmp!=0){
                    map.put(scanner.nextInt(),i);
                    tmp--;
                }

            }
            String input;
            do {
                input = scanner.next();
                if (input.equals("STOP"))
                    break;
                else if (input.equals("ENQUEUE")){
                    String num = scanner.next();
                    int number = Integer.valueOf(num);
                    int key = map.get(number);
                    if (teamList[key] == null){
                        teamList[key] = new LinkedList<>();
                        teamList[key].add(number);
                        mList.add(key);

                    }
                    else {
                        if (teamList[key].isEmpty())
                        {
                            teamList[key].add(number);
                            mList.add(key);

                        }
                        else {
                            teamList[key].add(number);
                        }
                    }
                }
                else {
                    int teamId = mList.getFirst();
                    tmpArrayList.add(teamList[teamId].removeFirst()+"");
                    if (teamList[teamId].isEmpty())
                        mList.removeFirst();
                }
            }while(true);
            answers.add(tmpArrayList);
            count++;
        }while(true);
        for (ArrayList<String> answer : answers){
            for (String a : answer)
                System.out.println(a);
            System.out.println("");
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值