zoukankan      html  css  js  c++  java
  • hdu 1392 fzu 1333 Surround the Trees 简单凸包

    使用凸包模版即可。

    hdu 1392

    /*
    * hdu1392.c
    *
    * Created on: 2011-9-2
    * Author: 王竹
    */

    #include
    <stdio.h>
    #include
    <math.h>
    #include
    <stdlib.h>
    #define nmax 1001
    #define eps 1e-8
    typedef
    struct point {
    int x, y;
    } point;
    point Point[nmax];
    int stack[nmax];
    double res;
    int cross_product(point p1, point p2, point p0) {
    return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x);
    }
    double pDistans(point p0, point p1) {
    return sqrt((p0.x - p1.x) * (p0.x - p1.x) + (p0.y - p1.y) * (p0.y - p1.y));
    }
    int cmp(const void *a, const void *b) {
    point n
    = *(point *) a;
    point m
    = *(point *) b;
    double temp;
    temp
    = cross_product(n, m, Point[0]);
    if (temp > 0) {
    return 1;
    }
    else if ((fabs(temp) < eps)
    && (pDistans(m, Point[0]) >= pDistans(n, Point[0]))) {
    return 1;
    }
    return -1;
    }
    void solve(int n) {
    int i;
    for (i = 0, res = 0.0; i < n; i++) {
    res
    += pDistans(Point[stack[i]], Point[stack[(i + 1) % n]]);
    }
    printf(
    "%.2lf\n", res);
    }
    void grahamScan(int n) {
    int i, k, top;
    point p;
    p
    = Point[0], k = 0;
    for (i = 1; i < n; i++) {
    if (p.y > Point[i].y || (p.y == Point[i].y && p.x > Point[i].x)) {
    p
    = Point[i], k = i;
    }
    }
    p
    = Point[0], Point[0] = Point[k], Point[k] = p;
    qsort(Point
    + 1, n - 1, sizeof(Point[0]), cmp);
    for (i = 0; i <= 2; i++) {
    stack[i]
    = i;
    }
    for (i = 3, top = 2; i < n; i++) {
    while (cross_product(Point[stack[top - 1]], Point[i], Point[stack[top]])
    <= 0) {
    top
    --;
    if (!top) {
    break;
    }
    }
    top
    ++;
    stack[top]
    = i;
    }
    solve(top
    + 1);
    }
    int main() {
    #ifndef ONLINE_JUDGE
    freopen(
    "data.in", "r", stdin);
    #endif
    int n, i;
    while (scanf("%d", &n) != EOF,n) {
    for (i = 0; i < n; i++) {
    scanf(
    "%d %d", &Point[i].x, &Point[i].y);
    }
    if (n == 1) {
    puts(
    "0.00");
    continue;
    }
    if (n == 2) {
    printf(
    "%.2lf\n", pDistans(Point[0], Point[1]));
    continue;
    }
    grahamScan(n);
    }
    return 0;
    }

    fzu 1333

    /*
    * hdu1392.c
    *
    * Created on: 2011-9-2
    * Author: 王竹
    */

    #include
    <stdio.h>
    #include
    <math.h>
    #include
    <stdlib.h>
    #define nmax 1001
    #define eps 1e-8
    typedef
    struct point {
    int x, y;
    } point;
    point Point[nmax];
    int stack[nmax];
    double res;
    int cross_product(point p1, point p2, point p0) {
    return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x);
    }
    double pDistans(point p0, point p1) {
    return sqrt((p0.x - p1.x) * (p0.x - p1.x) + (p0.y - p1.y) * (p0.y - p1.y));
    }
    int cmp(const void *a, const void *b) {
    point n
    = *(point *) a;
    point m
    = *(point *) b;
    double temp;
    temp
    = cross_product(n, m, Point[0]);
    if (temp > 0) {
    return 1;
    }
    else if ((fabs(temp) < eps)
    && (pDistans(m, Point[0]) >= pDistans(n, Point[0]))) {
    return 1;
    }
    return -1;
    }
    void solve(int n) {
    int i;
    for (i = 0, res = 0.0; i < n; i++) {
    res
    += pDistans(Point[stack[i]], Point[stack[(i + 1) % n]]);
    }
    printf(
    "%.2lf\n", res);
    }
    void grahamScan(int n) {
    int i, k, top;
    point p;
    p
    = Point[0], k = 0;
    for (i = 1; i < n; i++) {
    if (p.y > Point[i].y || (p.y == Point[i].y && p.x > Point[i].x)) {
    p
    = Point[i], k = i;
    }
    }
    p
    = Point[0], Point[0] = Point[k], Point[k] = p;
    qsort(Point
    + 1, n - 1, sizeof(Point[0]), cmp);
    for (i = 0; i <= 2; i++) {
    stack[i]
    = i;
    }
    for (i = 3, top = 2; i < n; i++) {
    while (cross_product(Point[stack[top - 1]], Point[i], Point[stack[top]])
    <= 0) {
    top
    --;
    if (!top) {
    break;
    }
    }
    top
    ++;
    stack[top]
    = i;
    }
    solve(top
    + 1);
    }
    int main() {
    #ifndef ONLINE_JUDGE
    freopen(
    "data.in", "r", stdin);
    #endif
    int n, i;
    while (scanf("%d", &n) != EOF,n) {
    for (i = 0; i < n; i++) {
    scanf(
    "%d %d", &Point[i].x, &Point[i].y);
    }
    if (n == 1) {
    puts(
    "0.00");
    continue;
    }
    if (n == 2) {
    printf(
    "%.2lf\n", pDistans(Point[0], Point[1]) * 2.0);
    continue;
    }
    grahamScan(n);
    }
    return 0;
    }
  • 相关阅读:
    【洛谷P2927 [USACO08DEC]拼图游戏Jigsaw Puzzles】深搜
    【洛谷1219】 八皇后 (搜索)
    【Uva 12558】 Egyptian Fractions (HARD version) (迭代加深搜,IDA*)
    【转】DCX (数独-八皇后问题)
    【2016 11 14】 总结
    【HDU 3038】 How Many Answers Are Wrong (带权并查集)
    【POJ1182】 食物链 (带权并查集)
    【20161111双11模拟赛】总结
    【HDU 5381】 The sum of gcd (子区间的xx和,离线)
    【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/2164610.html
Copyright © 2011-2022 走看看