zoukankan      html  css  js  c++  java
  • 凸包模板

    我必须要说的是输出时printf("%f", a);而不是%lf那样会wa这是我付出了n个小时得到的教训

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    const int maxa = 1005;
    struct edge{
        int x, y;
    }e[maxa], q[maxa];
    int cmp(edge a, edge b){
        if(a.x != b.x)
            return a.x < b.x;
        return a.y < b.y;
    }
    int det(int x1, int y1, int x2, int y2){
        return x1*y2 - x2*y1;
    }
    int cross(edge a, edge b, edge c, edge d){
        return det(b.x - a.x, b.y -a.y, d.x - c.x, d.y - c.y);
    }
    int make_tubao(int n){
        sort(e, e+n, cmp);
        int m = 0;
        for(int i = 0; i < n; i++){
            while(m >= 2 && cross(q[m-2], q[m-1], q[m-1], e[i])>= 0){
                m--;
            }
            q[m++] = e[i];
        }
        int k = m;
        for(int i = n-2; i >= 0; i--){
            while(m > k && cross(q[m-2], q[m-1], q[m-1], e[i])>= 0){
                m--;
            }
            q[m++] = e[i];
        }
        return m;
    }
    double dis(edge a, edge b){
        return sqrt((b.x - a.x)*(b.x - a.x) + (b.y-a.y)*(b.y-a.y));
    }
    int print(int n, int m){
        n = make_tubao(n);
        double ans = 0;
        for(int i = 0;i < n-1; i++){
           // printf("%d %d ", q[i].x, q[i].y);
            ans += dis(q[i], q[i+1]);
        }
        printf("%.0f ", ans + 3.1415926*2*m);
    }
    int main(){
        int n, m;
        while(scanf("%d%d", &n, &m)!=EOF){
            for(int i = 0 ; i < n; i++){
                scanf("%d%d", &e[i].x, &e[i].y);
            }
            print(n, m);
        }
    }

  • 相关阅读:
    Python-异常处理
    进程及其状态
    操作系统基础
    计算机组成基础
    Java wait()、notify()、notifyAll()方法
    Java 死锁
    线程同步
    Java 创建多线程
    Java 接口
    抽象类和抽象方法
  • 原文地址:https://www.cnblogs.com/icodefive/p/4319163.html
Copyright © 2011-2022 走看看