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

Codeforces 490B Queue【模拟】

时间:2014-12-27 22:56:27      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

题意还是很好理解的,根据题目给出描述条件然后求出这串QUEUE

 

我的做法就是用两个数组 before[] 和 after[] 表示 ai 前面的前面的人的学号 和 ai 后面的后面的人的学号

ex[] 表示 ai 这个人在输入的时候出现的次数,这个数组用于当人数为奇数的时候,寻找第1个人学号,只要遍历一遍1 - 10^6即可

 

具体还是看代码吧 QAQ ,感觉代码还是比较好理解的。

【这道题目 RE 起码5次,真正原因是数组没有开到位 = = ,也真是觉得自己最近没有写代码了好弱..........

 

 1 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
 2 #include <stdio.h>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <stack>
 7 #include <queue>
 8 #include <vector>
 9 #include <algorithm>
10 #define ll long long
11 #define Max(a,b) (((a) > (b)) ? (a) : (b))
12 #define Min(a,b) (((a) < (b)) ? (a) : (b))
13 #define Abs(x) (((x) > 0) ? (x) : (-(x)))
14 
15 using namespace std;
16 
17 const int INF = 0x3f3f3f3f;
18 int a[200011], ex[1000011];
19 int before[1000011], after[1000011];
20 
21 int main(){
22     int i, j ,k, t, n, m;
23     int uu, vv, cur, pos;
24     while(EOF != scanf("%d",&n)){
25         memset(ex, 0, sizeof(ex));
26         memset(before, -1, sizeof(before));
27         memset(after, -1, sizeof(after));
28         for(i = 1; i <= n; ++i){    
29             scanf("%d%d",&uu,&vv);
30             after[uu] = vv;
31             before[vv] = uu;
32             if(uu == 0){
33                 a[2] = vv;
34             } else if(vv == 0){
35                 a[n - 1] = uu;
36             } else{
37                 ++ex[uu];
38                 ++ex[vv];
39             }
40         }
41         if(n % 2 == 0){
42             cur = a[2];
43             pos = 2;
44             while(pos != n){
45                 pos += 2;
46                 cur = a[pos] = after[cur];
47             }
48             cur = a[n - 1];
49             pos = n - 1;
50             while(pos != 1){
51                 pos -= 2;
52                 cur = a[pos] = before[cur];
53             }
54         } else{
55             cur = a[2];
56             pos = 2;
57             while(pos != n - 1){
58                 pos += 2;
59                 cur = a[pos] = after[cur];
60             }
61             for(i = 1; i <= 1000001; ++i){  //2 nums
62                 if(ex[i] == 1 && before[i] == -1){
63                     break;
64                 }
65             }
66             a[1] = i;
67             cur = a[1];
68             pos = 1;
69             while(pos != n){
70                 pos += 2;
71                 cur = a[pos] = after[cur];
72             }
73         }
74 
75         for(i = 1; i <= n; ++i){
76             printf("%d ",a[i]);
77         }
78         printf("\n");
79     }
80     return 0;
81 }
82 /*
83 8
84 3 0
85 5 4
86 1 5
87 0 7
88 11 9
89 9 6
90 6 3
91 7 1
92 5
93 3 0
94 5 3
95 0 5
96 6 4
97 4 2
98 */

 

Codeforces 490B Queue【模拟】

标签:

原文地址:http://www.cnblogs.com/wushuaiyi/p/4189290.html

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