队列 CLZ 的银行普通队列 问题描述 CLZ 银行只有两个接待窗口,VIPVIP 窗口和普通窗口,VIPVIP 用户进入 VIPVIP 窗口排队,剩下的进入普通窗口排队。现有 MM 次操作,操作有四种类型,如下:
IN name V:表示一名叫 name 的用户到 VIPVIP 窗口排队 OUT V:表示 VIPVIP 窗口队头的用户离开排队 IN name N:表示一名叫 name 的用户到普通窗口排队 OUT N:表示普通窗口队头的用户离开排队 求 MM 次操作结束后 VIPVIP 窗口队列和普通窗口队列中的姓名。
输入描述 第一行是一个整数 M(1\leq M \leq 1000)M(1≤M≤1000),表示一共有 MM 次操作。
第二行到第 M+1M+1 行输入操作,格式如下:
IN name V OUT V IN name N OUT N
输出描述 输出 MM 次操作后 VIPVIP 窗口队列和普通窗口队列中的姓名(从头到尾),先输出 VIPVIP 窗口队列后输出普通窗口队列。
C++解法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 string Vqueue[1000 ]; int vhead=0 ;int vtail=0 ;string Nqueue[1000 ]; int nhead=0 ;int ntail=0 ;void in (string name,string type) { if (type=="V" ){ Vqueue[vtail]=name; vtail++; } else { Nqueue[ntail]=name; ntail++; } } bool out (string type) { if (type=="V" ){ if (vhead==vtail){ return false ; } else { vhead++; return true ; } } else { if (nhead==ntail){ return false ; } else { nhead++; return true ; } } } string gethead (string type) { if (type=="V" ){ return Vqueue[vhead]; } else { return Nqueue[nhead]; } } int main () { int M; cin>>M; for (int i=1 ;i<=M;i++){ string op,name,type; cin>>op; if (op=="IN" ){ cin>>name>>type; in (name,type); } else { cin>>type; out (type); } } string s=gethead ("V" ); while (out ("V" )){ cout<<s<<endl; s=gethead ("V" ); } s=gethead ("N" ); while (out ("N" )){ cout<<s<<endl; s=gethead ("N" ); } return 0 ; }
java解法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 import java.util.Scanner;public class Main { static String Vqueue[] = new String [1000 ]; static int Vhead = 0 ; static int Vtail = 0 ; static String Nqueue[] = new String [1000 ]; static int Nhead = 0 ; static int Ntail = 0 ; static void in (String name, String type) { if (type.contains( "V" )) { Vqueue[Vtail] = name; Vtail++; } else { Nqueue[Ntail] = name; Ntail++; } } static boolean out (String type) { if (type.contains( "V" )) { if (Vhead == Vtail) { return false ; } else { Vhead++; return true ; } } else { if (Nhead == Ntail) { return false ; } else { Nhead++; return true ; } } } static String getHead (String type) { if (type.contains( "V" )) { return Vqueue[Vhead]; } else { return Nqueue[Nhead]; } } public static void main (String[] args) { int M; Scanner in=new Scanner (System.in); M=in.nextInt(); while (M>0 ) { M--; String op,name,type; op=in.next(); if (op.contains("IN" )) { name=in.next(); type=in.next(); in(name,type); } else { type=in.next(); out(type); } } String s=getHead("V" ); while (out("V" )) { System.out.println(s); s=getHead("V" ); } s=getHead("N" ); while (out("N" )) { System.out.println(s); s=getHead("N" ); } } }
Python解法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 Vqueue = [] Vhead = 0 Vtail = 0 Nqueue = [] Nhead = 0 Ntail = 0 def inque (name, type ): global Vhead, Vtail, Nhead, Ntail,Vqueue ,Nqueue if (type == 'V' ): Vqueue.append(name) Vtail += 1 else : Nqueue.append(name) Ntail += 1 def getHead (type ): global Vhead, Vtail, Nhead, Ntail,Vqueue ,Nqueue if (type == 'V' ): return Vqueue[Vhead] else : return Nqueue[Nhead] def outque (type ): global Vhead, Vtail, Nhead, Ntail,Vqueue ,Nqueue if (type == 'V' ): if (Vhead == Vtail): return None else : s = getHead(type ) Vhead += 1 return s else : if (Nhead == Ntail): return None else : s= getHead(type ) Nhead += 1 return s if __name__ == '__main__' : M = 0 M = int (input ()) while M > 0 : M -= 1 op = input ().split() if op[0 ] == 'IN' : inque(op[1 ], op[2 ]) else : outque(op[1 ]) s = outque('V' ) while s!=None : print (s) s = outque('V' ) s = outque('N' ) while s != None : print (s) s = outque('N' )
普通队列说明 在比赛时我们常用 STL 中的 queue 容器,很少会去自己单独定义和使用队列。