简单思维题 poj 1852 Ants

题目链接:

http://poj.org/problem?id=1852

题目意思:

有n只蚂蚁,在长为l的杆子上,开始方向未知,各自沿着初始方向以单位速度爬,当两蚂蚁相遇时两只蚂蚁都向相反的方向爬,求所有蚂蚁都离开杆子的最短时间和最长时间。

解题思路:

思维转换,当两只蚂蚁相遇都掉头时,可以等价处理为蚂蚁不改变方向交错的继续往前爬。问题就等价为max(蚂蚁到两段的距离最大或最小)问题喽。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   int t,l,n;

   scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d",&l,&n);
       int Min=0,Max=0;
       for(int i=1;i<=n;i++)
       {
           int cur;

           scanf("%d",&cur);
           if(l-cur>cur)
           {
               Min=max(Min,cur);
               Max=max(Max,l-cur);
           }
           else
           {
               Min=max(Min,l-cur);
               Max=max(Max,cur);
           }
       }
       printf("%d %d\n",Min,Max);
   }
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值