poj 1852 Ants_贪心
题目大意:很多的蚂蚁都在长度为L(cm)的膀子上爬行,它们的速度都是1cm/s,到了棒子终端的时候,蚂蚁就会掉下去。如果在爬行途中遇到其他蚂蚁,两只蚂蚁的方向都会逆转。已知蚂蚁在棒子的最初位置坐标,但是我们不知道他们会往哪一个方向爬。请求出所有蚂蚁掉下去的最短时间和最长时间。
思路:当蚂蚁碰撞时,两只蚂蚁相反方向走,换个思路就是把他们的身份互换,相当于直接穿过,所以不用考虑蚂蚁的碰撞问题。
#include <iostream> #include<cstdio> #include<cmath> using namespace std; int main(int argc, char** argv) { int t,n,len,x,y,ans1,ans2; scanf("%d",&t); while(t--){ scanf("%d%d",&len,&n); ans1=0; ans2=0; for(int i=0;i<n;i++){ scanf("%d",&x); y=len-x; if(x>y)swap(x,y); if(x>ans1) ans1=x; if(y>ans2) ans2=y; } printf("%d %d\n",ans1,ans2); } return 0; }
不怕路长,只怕心老.