POJ 1852 Ants

题目的意思是说一个长度为m的杆,上面有n个蚂蚁,告诉每个蚂蚁的初始位置,每个蚂蚁速度都是一样的,问所有的蚂蚁离开杆的最短和最长时间是多少。

模拟题,所有的蚂蚁看成一样的,可以这样理解,即使相撞按反方向走,但是两只蚂蚁从开始爬到相撞到继续相背爬,这个时间都是一样的。所以直接对每只蚂蚁分别处理,得出他的最短离开时间和最长离开时间,我们从最短离开时间和最长离开时间里面求出最大的。

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int N;
    cin>>N;
    while(N!=0)
    {
        N--;
        int l,n;//l是棒子的长度 n代表蚂蚁数
        cin>>l>>n;
        int max=0,min=0;
        for(int m=0;m<n;m++)
        {
            int temp;//输入的几个整数
            cin>>temp;
            if(temp*2>l) //蚂蚁在棒子右边
            {
              if(max<temp) 
                max=temp;
              if(min<l-temp)
                min=l-temp; 
            }
            else       //蚂蚁在棒子左边
            {
                if(max<l-temp)
                  max=l-temp;
                if(min<temp)
                  min=temp;
            }
        }
        cout<<min<<' '<<max<<endl;
    }
    return 0;
}
#include <stdio.h>  
  
int Min(int a, int b) {  
    return a < b ? a : b;  
}  
  
int Max(int a, int b) {  
    return a > b ? a : b;  
}  
  
int main() {  
    int t, n, i, dis, len, min, max;  
    scanf("%d", &t);  
    while(t--) {  
        scanf("%d%d", &len, &n);  
        min = max = 0;  
        while(n--) {  
            scanf("%d", &dis);  
            min = Max(min, Min(dis, len - dis));  
            max = Max(max, Max(dis, len - dis));  
        }  
        printf("%d %d
", min, max);  
    }  
    return 0;  
}  
View Code
原文地址:https://www.cnblogs.com/wft1990/p/6084311.html