poj -1852 ants (思维题)

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

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>

#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define N 1000005
using namespace std;

int f[N];
int main()
{
   // freopen("a.txt","r",stdin);
    int t,l,n,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&l,&n);
        for(i=0;i<n;i++)
            scanf("%d",&f[i]);
        int minn=0;
        for(i=0;i<n;i++)
        {
            minn=max(minn,min(f[i],l-f[i]));
        }
        int maxn=0;
        for(i=0;i<n;i++)
        {
            maxn=max(maxn,max(f[i],l-f[i]));
        }
        printf("%d %d\n",minn,maxn);
    }
    return 0;
}
时间: 2024-07-30 15:56:25

poj -1852 ants (思维题)的相关文章

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 (思维技巧 + 贪心)

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

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 分析

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

poj 1825 Ants 水题

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

POJ 3347 (思维题 + 简单计算)

题目: 传送门 题意:有 n 个正方形,倾斜 45 度按顺序放在 x 坐标轴上,然后那些正方形要尽可能的靠近,问排放好之后,你从上往下看,不会被遮住的正方形有哪些. 思路:我们可以算出每个正方形斜放的左右端点的 x 坐标.我们可以枚举前面已经放好的正方形,然后让当前这个正方形和它靠在一起算出当前这个点的左端点的 x 坐标,然后对这些 x 取最大值就是当前这个正方形的左端点的 x 坐标,然后右端点坐标就很容易得到了.我们对输入的边长扩大 sqrt(2) 倍这样就都是整数的运算了,就没有精度误差.

poj 1852&amp;3684 题解

poj 1852 3684 这两题思路相似就放在一起. 1852 题意 一块长为L长度单位的板子(从0开始)上有很多只蚂蚁,给出它们的位置,它们的方向不确定,速度为每秒一长度单位,当两只蚂蚁相遇的时候,它们会反向而行,问所有蚂蚁到达终点(到0或者到L)所需要的最短时间和最长时间. 解析 一开始拿到这题觉得还是有点难度的,但一想通就发现这是个大水题.我们可以假设两只蚂蚁它们相向而行,并且它们中间没有别的蚂蚁,此时它们距离为k,经过时间t后它们相遇,此时它们反向,又经过时间t它们再次之间距离还是k.