poj 1852

来源:互联网 发布:标签软件破解版 编辑:程序博客网 时间:2024/05/16 19:02

题目大意:

n只蚂蚁在l长的杆子上爬,各个蚂蚁方向不一样,碰头后会调转方向继续爬,每秒钟一个长度,然后问所有蚂蚁爬下杆子的最长和最短时间;

基本思路:

两只蚂蚁碰头后各自调转方向相当于彼此穿过,所以情况就转化的更简单了;首先各个蚂蚁的方向是不知道的,然后用二分找长度中间值,因为所有蚂蚁都掉下杆子的最小时间情况就取决与这两只蚂蚁,让他们的相对位置在左边的方向向左,右边的方向向右,最长时间情况取决于最左边和最右边蚂蚁,然后让最左边的向右,最右边的向左,这里由于长度的奇偶问题,就用upper——bound来找出下标;

代码如下:
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 10000+10;
int ans[maxn];
int main()
{
    int times;
    scanf("%d",&times);
    while(times--)
    {
        int length,num;
        scanf("%d%d",&length,&num);
        for(int i=0;i<num;i++)
        {
            scanf("%d",&ans[i]);
        }
        sort(ans,ans+num);
        int mid=(length+1)/2;
        int right=upper_bound(ans,ans+num,mid)-ans;
        int left=right-1;
        int minx=max(ans[left],length-ans[right]);
        int maxx=max(length-ans[0],ans[num-1]);
        printf("%d %d\n",minx,maxx);
    }
    return 0;
}