• 【UVA 10881】 经典模拟题


    题目链接:http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=25979

    题目大意:一根长L厘米的木棍上有n只蚂蚁,每只蚂蚁有个开始的位置和爬行方向,速度为1.当两只蚂蚁相撞后,两者同时掉头继续爬行,求按输入顺序给出每只蚂蚁T秒后的位置后朝向。

    解题思路:

      1.每只蚂蚁相撞后同时掉头可以看做对穿而过,关键的问题就在于求位置的变化。

      2.按位置从小到大排序,可以惊奇的发现排序后(befor数组和after数组)所有的蚂蚁相对位置并没有变化,改变的只是朝向。

     

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int maxn=10005;
     8 
     9 struct node
    10 {
    11     int   id, p, d;   //d表示朝向,-1表示左,0表示碰撞中,1表示右。
    12     bool operator < (const node& S)const
    13     {
    14         return p<S.p;
    15     }
    16 } befor[maxn],after[maxn];
    17 
    18 char dirname[][10]={"L","Turning","R"};
    19 int  order[maxn];
    20 
    21 int main()
    22 {
    23     int  T, n, L, t, tcase=0;
    24     int  ss[10005];
    25     cin >> T;
    26     while(T--)
    27     {
    28         scanf("%d%d%d",&L, &t, &n);
    29         printf("Case #%d:\n",++tcase);
    30         for(int i=0; i<n; i++)
    31         {
    32             int  p, d;
    33             char c;
    34             scanf("%d %c",&p,&c);
    35             d=(c=='L'?-1:1);
    36             befor[i]=(node){i,p,d};
    37             after[i]=(node){0,p+t*d,d};  //所以的蚂蚁相撞后可以看做对穿而过
    38         }
    39         sort(befor,befor+n);
    40         for(int i=0; i<n; i++)  //最巧妙的地方在这里,第一次从左到右所有的蚂蚁的相对位置没有变化
    41             order[befor[i].id]=i;
    42         sort(after,after+n);
    43         for(int i=0; i<n-1; i++)
    44             if(after[i].p==after[i+1].p)  after[i].d=after[i+1].d=0;
    45         for(int i=0; i<n; i++)
    46         {
    47             int a=order[i];
    48             if(after[a].p>=0&&after[a].p<=L)
    49             {
    50                 printf("%d %s\n",after[a].p,dirname[after[a].d+1]);
    51             }
    52             else
    53                 printf("Fell off\n");
    54         }
    55         puts("");
    56     }
    57     return 0;
    58 }

     

  • 相关阅读:
    Linux进程管理工具Supervisor
    RSA加密传输代码示例
    静态网站创建工具Docusaurus
    Proactor和Reactor模型
    机器学习中的七宗罪
    Tokio internals: Understanding Rust's asynchronous I/O framework from the bottom up
    开源软件创建SOC的一份清单
    How to setup SOC using open-source tools
    彼得定律
    深入浅出通信原理连载
  • 原文地址:https://www.cnblogs.com/kane0526/p/2766597.html
Copyright © 2020-2023  润新知