HDU 3402 Ants run!(比较好的贪心)

来源:互联网 发布:卢松松博客php版下载 编辑:程序博客网 时间:2024/06/17 13:14

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3402

思路:还是有坑的,蚂蚁不是提前放好的,要自己放一下,放的时候肯定是将速度差不多的放在一起这样才能保证时间最长,pi要用公式求出来,不然会有精度问题

放的时候还有2种方法

速度最快   速度第二快 速度第三快  ... 最慢

最慢...... 速度第三快  速度第二快 速度最快

选择上面的方法,下面的方法最快的很快就会追上最慢的,时间不是最短

AC代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cstring>#include <climits>#include <cmath>#include <cctype>const int inf = 0x3f3f3f3f;//1061109567typedef long long LL;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int a[100010];int b[100010];const double pi = acos(-1.0);//一般用公式求,不然会有精度问题,cos(pi)=-1bool cmp(int a,int b){    return a > b;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n;        double r;        scanf("%d%lf",&n,&r);        bool flag = true;        for(int i=0; i<n; i++)        {             scanf("%d",&a[i]);             if(i != 0)             {                 if(a[i] != a[i-1])                    flag = false;             }        }        if(n == 1)        {            printf("Inf\n");            continue;        }        if(flag)        {            printf("Inf\n");            continue;        }        sort(a,a+n,cmp);        int max1 = 0;        for(int i=0; i<n-1; i++)        {            max1 = max(max1,a[i]-a[i+1]);        }        double l = 2 * pi * r / n / max1 * 1.0;        printf("%.3lf\n",l);    }}
大神写的比较短,可以看一下:http://www.acmerblog.com/hdu-3402-ants-run-5428.html?replytocom=58486

0 0