codeforces 493A. Vasya and Football 解题报告

题目链接:http://codeforces.com/contest/493/problem/A

题目意思:给出两个字符串,分别代表 home 和 away。然后有 t 个player,每个player偶四个属性描述:分钟,所属的队名(即上面的两个字符串的其中一个),该player的num,得到的card颜色(y/r)。 当一个人得到两个y card 时会自动转为 r card。最终需要按时间先后的顺序输出player第一次获得red card 的时间。

  由于数据是按时间先后顺序排列的,那么对于某个player,如果得到 r card,就可以直接输出答案了。然后得到 y card,要先用vis数组记录,如果再次遇到该player 且 vis数组已经被标记,那么这个player 符合条件,直接输出答案。注意,输出答案之后要标记这个player已经被处理。

  输入搞了好长时间,看来几天不写代码,确实容易退化啊~~~~

  (1)124ms  版本(这个可以忽略)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 50 + 5;
 8 const int N = 99 + 10;
 9 
10 char home[maxn], away[maxn];
11 int vish[N], visa[N];
12 bool non_delth[N], non_delta[N];
13 
14 int main()
15 {
16     char belong, card;
17     int t, minute, num;
18     while (cin >> home >> away)
19     {
20         memset(vish, 0, sizeof(vish));
21         memset(visa, 0, sizeof(visa));
22 
23         memset(non_delta, false, sizeof(non_delta));
24         memset(non_delth, false, sizeof(non_delth));
25 
26         cin >> t;
27         for (int i = 0; i < t; i++)
28         {
29             cin >> minute >> belong >> num >> card;
30             if (belong == 'h')
31             {
32                 if (!non_delth[num])
33                 {
34                     if (card == 'r' || vish[num])
35                     {
36                         printf("%s %d %d
", home, num, minute);
37                         non_delth[num] = true;
38                     }
39                     else
40                         vish[num] = 1;
41                 }
42             }
43             else if (belong == 'a')
44             {
45                 if (!non_delta[num])
46                 {
47                     if (card == 'r'|| visa[num])
48                     {
49                         printf("%s %d %d
", away, num, minute);
50                         non_delta[num] = true;
51                     }
52                     else
53                         visa[num] = 1;
54                 }
55             }
56         }
57     }
58     return 0;
59 }

   (2)15 ms版本(简单 + 方便 + 短小 + 容易理解)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int N = 99 + 5;
 8 string s[2];
 9 int f[2][N];
10 
11 int main()
12 {
13     #ifndef ONLINE_JUDGE
14         freopen("in.txt", "r", stdin);
15     #endif
16     int minute, num, n;
17     char belong, card;
18     while (cin >> s[0] >> s[1] >> n)
19     {
20         memset(f, 0, sizeof(f));
21         for (int i = 0; i < n; i++)
22         {
23             cin >> minute >> belong >> num >> card;
24             int c1, c2;
25             c1 = (belong == 'h' ? 0 : 1);
26             c2 = (card == 'y' ? 1 : 2);
27 
28             if (f[c1][num] < 2)
29             {
30                 f[c1][num] += c2;
31                 if (f[c1][num] >= 2)
32                     cout << s[c1] << " " << num << " " << minute << endl;
33             }
34         }
35     }
36     return 0;
37 }

   

 注:最巧妙的一句在

 if (f[c1][num] < 2)

这句话能够排除 在一个队里面同一个人当遇到多次(四次以上) yellow card 时只输出一次,符合题目要求!