• UVa 10714


      题目大意:有一个长度为lcm的木棍,上面有n只蚂蚁,以1cm/s的速度爬行,当两只蚂蚁相撞时两只蚂蚁各自掉头,蚂蚁走到两端时掉下去。不知道开始时每只蚂蚁各自的朝向,求全部蚂蚁都掉下去的可能最早时间和最晚时间。

      这个和UVa 10881 Piotr's Ants算是一个模型,不过这个简单一点,关键是从整体上看,蚂蚁“相撞掉头”和“擦肩而过”没有区别,在计算蚂蚁位置方面是等价的。所以,最早时间就是离中间最近的蚂蚁走到离它最近的一端的时间,最晚就是两端的蚂蚁走到离它更远的那一端的时间的较大者。

     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cmath>
     4 using namespace std;
     5 #define MAXN 1000000+10
     6 
     7 int ant[MAXN];
     8 
     9 int main()
    10 {
    11 #ifdef LOCAL
    12     freopen("in", "r", stdin);
    13 #endif
    14     int N;
    15     scanf("%d", &N);
    16     while (N--)
    17     {
    18         int l, n;
    19         scanf("%d%d", &l, &n);
    20         for (int i = 0; i < n; i++)
    21             scanf("%d", &ant[i]);
    22         sort(ant, ant+n);
    23         int latest = max (l-ant[0], ant[n-1]);
    24         double mid = 1.0 * l / 2;
    25         double lmin = fabs(ant[0]-mid);;  // lmin save the nearest distance from the half of the pole
    26         int p = 0;
    27         for (int i = 1; i < n; i++)
    28             if (fabs(ant[i]-mid) < lmin)
    29             {
    30                 lmin = fabs(ant[i]-mid);
    31                 p = i;
    32             }
    33         int earliest = min(ant[p], l-ant[p]);
    34         printf("%d %d
    ", earliest, latest);
    35     }
    36     return 0;
    37 }
    View Code
  • 相关阅读:
    我来说说博客评论的事
    SWFUpload+Javascript仿163邮件上传文件
    如何暂停和终止线程
    分享我的数据处理类库,欢迎拍砖
    求数列两两之差,再求和
    poj 1006 中国剩余定理
    Poj算法做题顺序
    poj 1328
    ZOJ 3279
    poj 2352 树状数组
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3205136.html
Copyright © 2020-2023  润新知