思维题 UVA 10881 Piotr's Ants

 

题目传送门

 1 /*
 2     题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态
 3     思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了
 4                 关键2:蚂蚁的相对位置不变    关键3:order数组记录顺序
 5 */
 6 #include <cstdio>
 7 #include <algorithm>
 8 #include <iostream>
 9 #include <cstring>
10 #include <string>
11 #include <cmath>
12 using namespace std;
13 
14 const int MAXN = 1e4 + 10;
15 const int INF = 0x3f3f3f3f;
16 const char dir_name[][10] = {"L", "Turning", "R"};
17 struct Ant
18 {
19     int pos, dir, id;
20     bool operator < (const Ant &a)    const
21     {
22         return pos < a.pos;
23     }
24 }pre[MAXN], now[MAXN];
25 int order[MAXN];
26 
27 int main(void)        //UVA 10881 Piotr's Ants
28 {
29 //    freopen ("UVA_10881.in", "r", stdin);
30 
31     int T;    int cas = 0;    int len, t, n;
32     scanf ("%d", &T);
33     while (T--)
34     {
35         scanf ("%d%d%d", &len, &t, &n);
36         char ch;
37         for (int i=1; i<=n; ++i)
38         {
39             int p, d;    char ch;
40             scanf ("%d %c", &p, &ch);
41             d = ((ch=='L') ? -1 : 1);
42             pre[i] = (Ant) {p, d, i};
43             now[i] = (Ant) {p+t*d, d, 0};
44         }
45 
46         sort (pre+1, pre+1+n);        //计算相对位置
47         for (int i=1; i<=n; ++i)    order[pre[i].id] = i;    //输入(输出)的顺序
48 
49         sort (now+1, now+1+n);
50         for (int i=1; i<n; ++i)
51         {
52             if (now[i].pos == now[i+1].pos)
53                 now[i].dir = now[i+1].dir = 0;
54         }
55 
56         printf ("Case #%d:\n", ++cas);
57         for (int i=1; i<=n; ++i)
58         {
59             int x = order[i];
60             if (now[x].pos < 0 || now[x].pos > len)    puts ("Fell off");
61             else
62             {
63                 printf ("%d %s\n", now[x].pos, dir_name[now[x].dir+1]);
64             }
65         }
66 
67         puts ("");
68     }
69 
70     return 0;
71 }
72 
73 /*
74 Case #1:
75 2 Turning
76 6 R
77 2 Turning
78 Fell off
79 
80 Case #2:
81 3 L
82 6 R
83 10 R
84 */

 

posted @ 2015-07-14 10:16  Running_Time  阅读(167)  评论(0编辑  收藏  举报