HDU 4033 二分+计算几何

/**********************************************************************
    开始用Java刷题了。。。
**********************************************************************/
import java.io.*;
import java.util.*;

public class Main
{
	static int INF_INT = 0x3f3f3f3f;
	static long INF_LONG = 0x7fffffffffffffffL;
	static double PI = Math.acos(-1.0);
	static double oo = 10e9;
	static double eps = 10e-9;
	
	static Scanner cin = new Scanner(new BufferedInputStream(System.in));
	
	static final int MAXN = 104;
	
	static int test, n;
	static double dis[] = new double[MAXN];
	
	public static double cal(double a, double b, double c)
	{
		return Math.acos((a * a + b * b - c * c) / (2 * a * b));
	}
	public static int judge(double len)
	{
		double th = 0.0;
		for (int i = 0; i < n; ++i)
		{
			if (len > dis[i] + dis[i + 1] + eps)
			{
				return 1;
			}
			if (len + eps < Math.abs(dis[i] - dis[i + 1]))
			{
				return -1;
			}
			th += cal(dis[i], dis[i + 1], len);
		}
		if (Math.abs(th - 2 * PI) < eps)
		{
			return 0;
		}
		if (th > 2 * PI + eps)
		{
			return 1;
		}
		return -1;
	}
	public static double bisearch(double l, double r)
	{
		double mid = (l + r) * 0.5;
		while (l + eps < r)
		{
			mid = (l + r) * 0.5;
			int sign = judge(mid);
			if (0 == sign)
			{
				break ;
			}
			if (1 == sign)
			{
				r = mid;
			}
			else
			{
				l = mid;
			}
		}
		return mid;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		int cas = 1;
		for (test = cin.nextInt(); test-- > 0; ++cas)
		{
			n = cin.nextInt();
			for (int i = 0; i < n; ++i)
			{
				dis[i] = cin.nextDouble();
			}
			dis[n] = dis[0];
			double ans = bisearch(0, 1000000);
			System.out.printf("Case %d: ", cas);
			if (0 != judge(ans))
			{
				System.out.print("impossible");
			}
			else
			{
				System.out.printf("%.3f", ans);
			}
			System.out.println();
		}
		return ;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值