• TZOJ--5112: Fencing the Cows(凸包)


    5112: Fencing the Cows 

    时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte

    描述

    Farmer John wishes to build a fence to contain his cows, but he's a bit short on cash right. Any fence he builds must contain all of the favorite grazing spots for his cows. Given the location of these spots, determine the length of the shortest fence which encloses them.

    输入

    The first line of the input file contains one integer, N. N (0 <= N <= 10,000) is the number of grazing spots that Farmer john wishes to enclose. The next N line consists of two real numbers, Xi and Yi, corresponding to the location of the grazing spots in the plane (-1,000,000 <= Xi,Yi <= 1,000,000). The numbers will be in decimal format.

    输出

    The output should consists of one real number, the length of fence required. The output should be accurate to two decimal places.

    样例输入

    4
    4 8
    4 12
    5 9.3
    7 8

    样例输出

     12.00

    题目来源

    USACO

     

    题目链接:http://tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=5112

     

    题目大意,求凸包的周长。

    #include <bits/stdc++.h>
    using namespace std;
    struct Point{
        double x,y;
    };
    double dis(Point p1,Point p2)
    {
        return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
    }
    double xmulti(Point p1,Point p2,Point p0) 
    {
        return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
    }
    double graham(Point p[],int n)
    {
        int pl[10005];
        int t = 1;
        for(int i=1;i<=n;i++)
            if(p[i].y < p[t].y)
                t = i;
        pl[1] = t;
        int num = 1;
        do{
            num++;
            t = pl[num-1]+1;
            if(t>n) t = 1;
            for(int i=1;i<=n;i++){
                double x = xmulti(p[i],p[t],p[pl[num-1]]);
                if(x<0) t = i;
            }
            pl[num] = t;
        } while(pl[num]!=pl[1]);
        double sum = 0;
        for(int i=1;i<num;i++)
            sum += dis(p[pl[i]],p[pl[i+1]]);
        return sum;
    }
    int main()
    {
    	Point a[11111];
    	int n;
    	while(cin>>n){
    		for(int i=1;i<=n;i++)
    		    scanf("%lf %lf",&a[i].x,&a[i].y);
    		printf("%.2f
    ",graham(a,n));
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Pycaret 安装问题
    关于ThreadLocal最直白的解释
    每日一题(2022524):经典搜索算法
    我使用Spring AOP实现了用户操作日志功能
    使用 CompeletedFuture 实现异步调用
    MVCC(多版本并发控制)详解
    【ACM程序设计】动态规划 第二篇 LCS&LIS问题
    服务器定时备份数据库
    mysql 慢查询
    js数组复制(浅拷贝,深拷贝)
  • 原文地址:https://www.cnblogs.com/Anidlebrain/p/10032321.html
Copyright © 2020-2023  润新知