UVa10881 Piotr's Ants

  原题传送:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1822

  题目没有给出T的范围,自己自作多情写了个O(nT)的程序,枚举题意后发现T的范围竟然达到了106!!!这样我就思密达了。

  书上这么一个神奇的转换思路:

  1. “调头”等价于“对穿而过”

  2. 所有蚂蚁的相对顺序保持不变 (显然的,因为实际上是不可能穿过的)

View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #define N 10005
 5 int L, T, n;
 6 
 7 struct Ant
 8 {
 9     int id; // 蚂蚁编号
10     int x;  // 蚂蚁位置
11     char dr;// 爬行方向
12     bool st;// 调转状态
13     void operator =(Ant u)
14     {id = u.id, x = u.x, dr = u.dr, st = u.st;}
15 }a[N], b[N];
16 
17 struct node
18 {
19     int id, x;
20 }order[N];
21 
22 bool cmp1(node u, node v)
23 {return u.x < v.x;}
24 
25 bool cmp2(Ant u, Ant v)
26 {return u.x < v.x;}
27 
28 int main()
29 {
30     int k, cas, i;
31     scanf("%d", &k);
32     for(cas = 1; cas <= k; cas ++)
33     {
34         scanf("%d%d%d", &L, &T, &n);
35         for(i = 1; i <= n; i ++)
36         {
37             scanf("%d %c", &a[i].x, &a[i].dr);
38             order[i].x = a[i].x, order[i].id = i;
39         }
40             
41         std::sort(order + 1, order + n + 1, cmp1);
42             
43         for(i = 1; i <= n; i ++)
44             a[i].x += (a[i].dr == 'R' ? T : -T);
45             
46         std::sort(a + 1, a + n + 1, cmp2);
47         for(i = 1; i < n; i ++)
48             if(a[i].x == a[i + 1].x)
49                 a[i].st = a[i + 1].st = true, ++i;
50             else
51                 a[i].st = false;
52         
53         printf("Case #%d:\n", cas);
54         for(i = 1; i <= n; i ++)
55             b[order[i].id] = a[i];
56         for(i = 1; i <= n; i ++)
57         {
58             if(b[i].x < 0 || b[i].x > L)
59                 puts("Fell off");
60             else
61             {
62                 printf("%d", b[i].x);
63                 if(b[i].st)
64                     puts(" Turning");
65                 else
66                     printf(" %c\n", b[i].dr);
67             }
68         }
69         putchar('\n');
70     }
71     return 0;
72 }

 

  

你可能感兴趣的:(ant)