poj 1852 Ants(贪心)

Ants

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 14061   Accepted: 6134

Description

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.

Input

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.

Output

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

题意:

有n只蚂蚁在长为l的木棒上爬行,当他们在木棒上相遇时,会朝相反的方向爬去,给你他们离起点的距离,当爬行到任意一个端点时,蚂蚁掉落,

求全部蚂蚁掉落的最长和最短时间

做法:

把全部蚂蚁看作一样的,当他们相遇时,看成相互穿过,这样直接计算就行

#include <cstdio>
#include <cstring>
#include <iostream>
#define INF 0x3f3f3f
using namespace std;

int main(){
    int i,t,Min,Max,k,n,l;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&l,&n);
        scanf("%d",&k);
        Min=min(k,l-k);
        Max=l-Min;
        for(i=2;i<=n;i++){
            scanf("%d",&k);
            Min=max(Min,min(k,l-k));
            Max=max(Max,max(k,l-k));
        }
        printf("%d %d\n",Min,Max);
    }
    return 0;
}
时间: 2024-10-02 05:08:43

poj 1852 Ants(贪心)的相关文章

POJ 1852 Ants (思维技巧 + 贪心)

Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10639   Accepted: 4718 Description 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 imm

POJ 1852 Ants 分析

1.暴搜 每只蚂蚁朝向有两种,可以枚举n只蚂蚁的朝向,然后模拟蚂蚁相遇的情景,总共2^n中情况. 2.分析ants相碰的情况: (a->)  (<-b) 变成 (<-a)(b->) 由于每只蚂蚁是相同的,所以等价与(<-b)(a->),这和两只蚂蚁原来的走向是一样的,即把碰撞当作没发生过 所以可以对每一只蚂蚁检查一次就可以了 3.空间优化 存蚂蚁的初始位置O(n),但是我们每次只要比较 a[i] 和 len-a[i] 就可以了,后面不需要这两个值了,所以完全可以用一个x

POJ 1852 Ants

Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15617   Accepted: 6753 Description 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 imm

POJ 1852 Ants 思维题 简单题

Ants Description 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 oppos

水题/poj 1852 Ants

1 /* 2 PROBLEM:poj1852 3 AUTHER:Nicole 4 MEMO:水题 5 */ 6 #include<cstdio> 7 using namespace std; 8 int cmax(int a,int b){return a>b?a:b;} 9 int cmin(int a,int b){return a<b?a:b;} 10 int main() 11 { 12 int cases; 13 scanf("%d",&cas

poj -1852 ants (思维题)

n只蚂蚁以每秒1cm的速度在长为Lcm的杆子上爬行,当蚂蚁爬到杆子的端点时就会掉落,由于杆子太细,两只蚂蚁相遇时,他们不能交错通过,只能各自反向爬回去,对于每只蚂蚁,我们知道它距离杆子左端的距离为x,但不知道它当前的朝向,请计算所有蚂蚁落下杆子所需的最短时间很最长时间. #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring&g

poj 1456 Supermarket (贪心+并查集)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]

POJ 1862 Stripies 贪心+优先队列

http://poj.org/problem?id=1862 题目大意: 有一种生物能两两合并,合并之前的重量分别为m1和m2,合并之后变为2*sqrt(m1*m2),现在给定n个这样的生物,求合并成一个的最小重量 思路: m1+m2 >=  2*sqrt(m1*m2) 所以每次取大的去合并,能变小. 直接优先队列就可以啦. #include<cstdio> #include<cmath> #include<queue> using namespace std;

poj 2431 Expedition 贪心+最大堆

当油量不够时从走过的油站中选最大加油量的 #include<iostream> #include<queue> #include<stdlib.h> #include<algorithm> using namespace std; #define MAX_N 10005 struct node{ int dist,fuel; }t[MAX_N]; bool cmp(const node &a,const node &b) { return a