本文主要是介绍uva 10714 Ants,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原题:
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole,unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
大意:
有一堆蚂蚁在一个棍子上爬,你不知道这些蚂蚁的爬行方向,现在给你这些蚂蚁在棍子上的初始位置。问你最快全部蚂蚁掉下去和最慢全部蚂蚁掉下去的时间是多少。
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iomanip>
#include<set>
#include<fstream>
#include <climits>
using namespace std;
//fstream input,output;int l,n,le,ll;
int early,last;
int main()
{ios::sync_with_stdio(false);int t,a;cin>>t;while(t--){cin>>l>>n;le=INT_MAX;ll=-1;for(int i=0;i<n;i++){cin>>a;if(abs(a-l/2)<le){le=abs(a-l/2);early=a;}if(abs(a-l/2)>ll){ll=abs(a-l/2);last=a;}}early=min(early,l-early);last=max(last,l-last);cout<<early<<" "<<last<<endl;}
// input.close();
// output.close();return 0;
}
解答:
很经典的一个问题,之前貌似在poj上做过。所以这个很容易就做出来了,只要理解了两只蚂蚁碰头后调转方向相当于穿行而过就能做出这道题了
这篇关于uva 10714 Ants的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!