UVA 10714-Ants(求花费的最大最小时间)

Ants

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

Description

Problem B: 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.

The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing
on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated
by whitespace.

For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately)
and the second number is the latest possible such time.

Sample input

2
10 3
2 6 7
214 7
11 12 7 13 176 23 191

Output for sample input

4 8
38 207

题意:一根棍子上有蚂蚁, 这些蚂蚁朝着一个方向以相同的速度(每秒一个单位长度)一直走(可能是往棍子的任意一端走),一旦蚂蚁碰到迎面走来的一直蚂蚁,那么这两只蚂蚁都会马上往相反方向走。求当所有蚂蚁都会走下棍子的,花费的最小的和最大时间。

思路:求最大时间即找出离两个端点最远的蚂蚁的最远位置,求最小时间求离两个端点最近的位置的蚂蚁的最远位置

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int a[1000010];
int main(){
    int T,n,m,i,j;
    int maxx,minn;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        maxx=minn=-inf;
        for(i=0;i<m;i++){
            scanf("%d",&a[i]);
            int left=a[i];
            int right=n-a[i];
            maxx = max(maxx, max(left,right));
            minn = max(minn, min(left,right));
        }
        printf("%d %d\n", minn,maxx);
    }
    return 0;
  }
时间: 2024-08-28 23:42:50

UVA 10714-Ants(求花费的最大最小时间)的相关文章

UVA - 10714 Ants

最多时间就是每只蚂蚁选择最久的爬行方式 最少时间就是每只蚂蚁选择最快地爬行方式 #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include&

uva 714 - Copying Books(贪心 最大值最小化 二分)

题目描述开头一大堆屁话,我还仔细看了半天..其实就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,如果最小值相同情况下有多种分配方案,输出前面份数小的,就像字典序输出从小到大一样的意思. 这里用到贪心的方法,定义f(x)为真的条件是满足x为最大值使n本书分成k份,那么就是求x的最小值.如何确定这个x就是用的二分法,x一定大于0小于所有值的合,不断的二分再判断是否成立,成立就取左半边,不成立说明太小了就取右半边,写的时候还是没有把二分法理解透彻,我还怕会丢失

10714 - Ants(贪心)

题目:10714 - Ants 题目大意:一个长度为l的板上,分布着许多的蚂蚁,每只蚂蚁的位置都会给出但是方向不缺定,如果两只蚂蚁碰上了,就会朝各自相反的方向前进.问这样所有的蚂蚁都跌落木板的最短时间和最长时间. 解题思路:最短时间的话就是每只蚂蚁都朝着各自离两端最近的方向前进,最后取这些最近的位置的最大值,就是最短时间.这样的话两只蚂蚁是不会碰到的,因为某只蚂蚁的前方那只蚂蚁要么和这只同向,要么反向:后面的蚂蚁也是一样的情况.所以不会碰到. 最长的时间的话,其实就是离两端的其中一端最远的那只蚂

UVA 11419 SAM I AM (二分图,最小割)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2414 Problem C SAM I AM Input: Standard Input Output: Standard Output The world is in great danger!! Mental's forces have returned to Earth to eradi

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

【C语言】求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素

//求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素 #include <stdio.h> #include <string.h> int find_min(int arr[],int len) { int i = 0; for (i = 1; i < len; i++) { if (arr[i] < arr[0]) return arr[i]; } return arr[0]; } int main() { int i; int arr1[] =

UVA 1411 - Ants(二分图完美匹配)

UVA 1411 - Ants 题目链接 题意:给定一些黑点白点,要求一个黑点连接一个白点,而且全部线段都不相交 思路:二分图完美匹配,权值存负的欧几里得距离,这种话,相交肯定比不相交权值小,所以做一次完美匹配就能够了 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MAXNODE = 1

求给定数据中最小的K个数

public class MinHeap { /* * * Top K个问题,求给定数据中最小的K个数 * * 最小堆解决:堆顶元素为堆中最大元素 * * * */ private int MAX_DATA = 10;//最小10个数 private int[] data;//存储数据 private int len;//当前存储长度,考虑到元素个数可能没有10个,这个时候全部输出 private MinHeap() { data = new int[MAX_DATA]; len=0; } pr

hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割

view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std; typedef long long ll; const ll INF = 1LL<<59; const ll E = 100001; const int N = 10