UVa 10881 Piotr's Ants

    原来也在OJ上做了一些题,做的都是一些水题,后来又去干别的事来,就把ACM放下了,前两天做腾讯马拉松,竟然一道题也没做出来。。。好水。。。然后决定好好做一段时间,提升一下水平,就参考《算法竞赛入门经典——训练指南》开始练习。曾经不想做记录、贴代码,认为没有必要,不过想想还是记录一下吧,最起码还是个代码备份呢,偶尔也能回顾一下。

下面就是照书上敲定代码了:

View Code
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int maxn = 10000 + 10;
 6 
 7 struct Ant
 8 {
 9     int id;
10     int p;
11     int d;   //-1: L; 0, Turning; 1, R.
12     bool operator < (const Ant& a) const
13     {
14         return p < a.p;
15     }
16 } ant[maxn];
17 
18 const char stat[][10] = {"L", "Turning", "R"};
19 int order[maxn];
20 
21 int main()
22 {
23 #ifdef LOCAL
24     freopen("in", "r", stdin);
25 #endif
26     int N;
27     scanf("%d", &N);
28     int Case;
29     for(Case = 1; Case <= N; Case++)
30     {
31         int L, T, n;
32         scanf("%d%d%d", &L, &T, &n);
33         for(int i = 0; i < n; i++)
34         {
35             int p, d;
36             char c;
37             scanf("%d %c", &p, &c);
38             d = (c == 'L') ? -1 : 1;
39             ant[i] = (Ant){i, p, d};
40         }
41         sort(ant, ant+n);
42         for(int i = 0; i < n; i++)
43             order[ant[i].id] = i;
44         for(int i = 0; i < n; i++)
45         {
46             ant[i].p += T*ant[i].d;
47         }
48         sort(ant, ant+n);
49         for(int i = 0; i < n-1; i++)
50             if(ant[i].p == ant[i+1].p)
51                 ant[i].d = ant[i+1].d = 0;
52         printf("Case #%d:\n", Case);
53         for(int i = 0; i < n; i++)
54         {
55             int t  = order[i];
56             if(ant[t].p < 0 || ant[t].p > L)   printf("Fell off\n");
57             else printf("%d %s\n", ant[t].p, stat[ant[t].d+1]);
58         }
59         printf("\n");
60     }
61     return 0;
62 }

ps:刚开始用vim,用的有点艰难,多多练习就好了,算是一个新的开始吧

posted @ 2013-03-27 16:43  xiaobaibuhei  阅读(186)  评论(0编辑  收藏  举报