• POJ 1271 Nice Milk (半平面交应用)


    题目:传送门

    题意:有一个凸多形面包,有一罐牛奶,牛奶高度为 h,认为牛奶的宽度无限长,现在你最多可以蘸 k 次牛奶, 问你蘸到牛奶的面包的面积最大是多少。

    3 <= n <= 20, 0 <= k <= 8, 0 <= h <= 10

    思路:dfs 枚举凸多边形蘸牛奶的 K 条边,将这些边向内缩进 h,求新的多边形的半平面交即是不能蘸到牛奶的面积,维护一个最小值,最后答案就是没有蘸牛奶时的面积减去这个最小值。

    注意 k 可能大于 n,要判一下。

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <vector>
    #include <set>
    #include <string>
    #include <math.h>
    
    #define LL long long
    #define mem(i, j) memset(i, j, sizeof(i))
    #define rep(i, j, k) for(int i = j; i <= k; i++)
    #define dep(i, j, k) for(int i = k; i >= j; i--)
    #define pb push_back
    #define make make_pair
    #define INF INT_MAX
    #define inf LLONG_MAX
    #define PI acos(-1)
    #define fir first
    #define sec second
    using namespace std;
    
    const int N = 22;
    const double eps = 1e-6;
    const double maxL = 1000.0;
    
    struct Point {
        double x, y;
        Point(double x = 0, double y = 0) : x(x), y(y) { }
    };
    
    int dcmp(double x) {
        if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
    }
    
    Point operator + (Point A, Point B) { return Point(A.x + B.x, A.y + B.y); }
    Point operator - (Point A, Point B) { return Point(A.x - B.x, A.y - B.y); }
    Point operator * (Point A, double p) { return Point(A.x * p, A.y * p); }
    Point operator / (Point A, double p) { return Point(A.x / p, A.y / p); }
    
    double Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; }
    double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; }
    double Length(Point A) { return sqrt(Dot(A, A)); }
    
    Point Rotate(Point A, double rad) { /// 向量逆时针旋转 rad (弧度)
        return Point(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
    }
    
    
    /* 有向直线,它的左边就是对应的半平面 */
    struct Line {
        Point p; /// 直线任意一点
        Point v; /// 方向向量
        double ang; /// 极角,即从x正半轴旋转到向量v所需要的角(弧度)
        Line() { }
        Line(Point p, Point v) : p(p), v(v) { ang = atan2(v.y, v.x); }
        bool operator < (const Line& L) const {
            return ang < L.ang;
        }
    };
    
    /* 点p在有向直线L的左边 */
    bool OnLeft(Line L, Point p) {
        return dcmp(Cross(L.v, p - L.p)) > 0;
    }
    
    /* 二直线交点,假设交点唯一存在。*/
    Point GLI(Line a, Line b) {
        Point u = a.p - b.p;
        double t = Cross(b.v, u) / Cross(a.v, b.v);
        return a.p + a.v * t;
    }
    
    Point p[22];
    Line q[22];
    
    int HPI(Line* L, int n, Point* Q) {
        sort(L, L + n); /// 极角排序
    
        int st, ed; /// 双端队列的第一个元素和最后一个元素的下标
    
    //    Point *p = new Point[n]; /// p[i]为q[i]和q[i+1]的交点
    //    Line *q = new Line[n]; /// 双端队列
        q[st = ed = 0] = L[0];
    
        rep(i, 1, n - 1) {
            while(st < ed && !OnLeft(L[i], p[ed - 1])) ed--;
            while(st < ed && !OnLeft(L[i], p[st])) st++;
    
            q[++ed] = L[i];
    
            /// 平行取内测那条
            if(fabs(Cross(q[ed].v, q[ed - 1].v)) < eps) {
                ed--;
                if(OnLeft(q[ed], L[i].p)) q[ed] = L[i];
            }
    
            if(st < ed) p[ed - 1] = GLI(q[ed - 1], q[ed]);
    
        }
    
        while(st < ed && !OnLeft(q[st], p[ed - 1])) ed--;
    
        if(ed - st <= 1) return 0;
    
        p[ed] = GLI(q[ed], q[st]);
    
        int m = 0;
        rep(i, st, ed) Q[m++] = p[i];
        return m;
    }
    
    Point P[22], Q[22];
    
    Line L[22], tmp_L[22];
    
    void change(Point A, Point B, Point &C, Point &D, double p) { /// 求A,B两点向里移动d距离后的两点C,D
        double len = Length(B - A);
        double addx = (A.y - B.y) * p / len;
        double addy = (B.x - A.x) * p / len;
        C.x = A.x + addx; C.y = A.y + addy;
        D.x = B.x + addx; D.y = B.y + addy;
    }
    
    int n, k, h;
    double mi = 100000000.0;
    
    void dfs(int coun, int pos) {
    
        if(mi == 0.0) return ;
        if(n - pos < k - coun) return ;
        if(coun > k) return ;
        if(coun == k) {
    
            rep(i, 0, n - 1) tmp_L[i] = L[i];
    
            int cnt = HPI(tmp_L, n, Q);
    
            double res = 0.0;
    
            rep(i, 1, cnt - 2) res += Cross(Q[i] - Q[0], Q[i + 1] - Q[0]);
            if(res < 0) res = -res;
    
            mi = min(mi, res);
            return ;
        }
    
        if(pos >= n) return ;
    
        Point A, B;
        change(P[pos], P[(pos + 1) % n], A, B, h);
        L[pos] = Line(A, B - A);
        dfs(coun + 1, pos + 1);
        L[pos] = Line(P[pos], P[(pos + 1) % n] - P[pos]);
        dfs(coun, pos + 1);
    }
    
    int main() {
    
        while(~scanf("%d %d %d", &n, &k, &h)) {
            if(n == 0 && k == 0 && h == 0) return 0;
            if(k > n) k = n;
            rep(i, 0, n - 1) scanf("%lf %lf", &P[i].x, &P[i].y);
    
            if(k == 0 || h == 0) {
                puts("0.00"); continue;
            }
    
            rep(i, 0, n - 1) {
                L[i] = Line(P[i], P[(i + 1) % n] - P[i]);
                tmp_L[i] = L[i];
            }
            double ans = 0;
    
            int cnt = HPI(tmp_L, n, Q);
    
            rep(i, 1, cnt - 2) ans += Cross(Q[i] - Q[0], Q[i + 1] - Q[0]);
            if(ans < 0) ans = -ans;
    
            mi = 100000000.0;
    
            Point A, B;
            change(P[0], P[1], A, B, h);
            L[0] = Line(A, B - A);
            dfs(1, 1);
            L[0] = Line(P[0], P[1] - P[0]);
            dfs(0, 1);
    
            printf("%.2f
    ", (ans - mi) / 2.0);
    
        }
    
        return 0;
    }
    一步一步,永不停息
  • 相关阅读:
    Docker简介安装与下载
    ActiveMq安装以及简单的测试
    HashMap源码解析<一>put()方法
    SQL语句查询练习题
    珍藏的数据库SQL基础练习题答案
    MySQL习题及答案
    Hadoop学习1
    数据库简单的实际应用
    数据库基础练习选择题
    数据库练习题
  • 原文地址:https://www.cnblogs.com/Willems/p/12481940.html
Copyright © 2020-2023  润新知