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

题目地址: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;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int a[100010];
int b[100010];
const double pi = acos(-1.0);//一般用公式求,不然会有精度问题,cos(pi)=-1
bool 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值