TJU 1168. Team Queue

Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 265   Accepted Runs: 108

本题总的来说不难,却困扰了我不少时间,因为我不知道原来数列竟然可以定义成数组队列。如果知道这个就很容易了。

思路:首先定义一个总的队列用来存储入队的那些TEAM的编号,然后定义一个数组队列,每一个队列用来记录本TEAM的入队顺序。首先是入队时判断所在TEAM的队列是否为空如若为空将TEAM的编号加入总队列中,然后再将本数加入到所在TEAM的队列中。出队时,判断所在TEAM的队列是否为空,如果为空总队列也要输出。

本题我本来是用链表做的,本以为链表会很快,但是后来发现题意是进队和出对都是常数时间,所以超时是理所当然的

链表代码:(超时)

 

代码
   
     
1 #include < stdio.h >
2 #include < string .h >
3 #include < stdlib.h >
4   struct node
5 {
6 int set ;
7 }s[ 1000000 ];
8 typedef struct t
9 {
10 int set ;
11 int data;
12 struct t * next;
13 } * T;
14   int main()
15 {
16 T head,p;
17 int count = 0 ,t,i,j,n,a;
18 char b[ 10 ];
19 while (scanf( " %d " , & t) != EOF)
20 {
21 count ++ ;
22 if (t == 0 )
23 break ;
24 for (i = 1 ;i <= t;i ++ )
25 {
26 scanf( " %d " , & n);
27 for (j = 1 ;j <= n;j ++ )
28 {
29 scanf( " %d " , & a);
30 s[a]. set = i;
31 }
32 }
33 head = (T)malloc( sizeof (t));
34 head -> next = NULL;
35 p = (T)malloc( sizeof (t));
36 // p->next=NULL;
37   printf( " Scenario #%d\n " ,count);
38 while (scanf( " %s " ,b) != EOF)
39 {
40 if (strcmp(b, " STOP " ) == 0 )
41 break ;
42 else if (b[ 0 ] == ' E ' )
43 {
44 scanf( " %d " , & a);
45 T t = (T)malloc( sizeof (t));
46 t -> next = NULL;
47 t -> set = s[a]. set ;
48 t -> data = a;
49 p = head;
50 while (p -> next != NULL)
51 {
52 if (p -> set == s[a]. set && p -> next -> set != s[a]. set )
53 break ;
54 p = p -> next;
55 }
56 t -> next = p -> next;
57 p -> next = t;
58 }
59 else if (b[ 0 ] == ' D ' )
60 {
61 p = head -> next;
62 printf( " %d\n " ,p -> data);
63 head -> next = head -> next -> next;
64 }
65 }
66
67 }
68 return 0 ;
69 }

 

 

代码:

 

  
    
1 #include < stdio.h >
2 #include < queue >
3   using namespace std;
4   struct node
5 {
6 int data;
7 int set ;
8 }s[ 1000000 ];
9   int main()
10 {
11 int count = 0 ,t,i,j,a,n;
12 char b[ 10 ];node cur1;
13 while (scanf( " %d " , & t) != EOF)
14 {
15 if (t == 0 )
16 break ;
17 count ++ ;
18 int cur;
19 for (i = 1 ;i <= t;i ++ )
20 {
21 scanf( " %d " , & n);
22 for (j = 1 ;j <= n;j ++ )
23 {
24 scanf( " %d " , & a);
25 s[a]. set = i;
26 s[a].data = a;
27 }
28 }
29 printf( " Scenario #%d\n " ,count);
30 queue < int > q;
31 queue < node > qu[ 1000 ];
32 while (scanf( " %s " ,b) != EOF)
33 {
34 if (strcmp(b, " STOP " ) == 0 )
35 break ;
36 else if (b[ 0 ] == ' E ' )
37 {
38 scanf( " %d " , & a);
39 if (qu[s[a]. set ].empty ())
40 q.push(s[a]. set );
41 qu[s[a]. set ].push(s[a]);
42 }
43 else if (b[ 0 ] == ' D ' )
44 {
45 cur = q.front ();
46 cur1 = qu[cur].front();
47 qu[cur].pop();
48 if (qu[cur].empty ())
49 q.pop();
50 printf( " %d\n " ,cur1.data);
51 }
52 }
53 printf( " \n " );
54 }
55 return 0 ;
56 }
57

 

 

 

你可能感兴趣的:(Queue)