• 傻逼题做了一晚上~第一发凸包(套模板)~区间dp~zju3537


    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    using namespace std;
    
    #define LL long long
    const LL inf = 1234567899ll * 1234567890;
    int mod;
    int n;
    struct point{
        int x, y;
        point(){}
        point(int _x, int _y) : x(_x), y(_y){}
        bool operator < (const point &cmp) const{
            if(x == cmp.x) return y < cmp.y;
            return x < cmp.x;
        }
    } p[345], ch[345];
    int f[333][333];
    LL dp[333][333];
    point operator - (point a, point b) {return point(a.x-b.x, a.y-b.y);}
    int Cross(point a, point b){
        return a.x*b.y-b.x*a.y;
    }
    int ConvexHull(){
        sort(p, p+n);
        int ret = 0;
        for(int i = 0; i < n; i ++){
            while(ret > 1 && Cross(ch[ret-1]-ch[ret-2], p[i]-ch[ret-2]) <= 0) ret --;
            ch[ret++] = p[i];
        }
        int k = ret;
        for(int i = n-2; i >= 0; i --){
            while(ret > k && Cross(ch[ret-1]-ch[ret-2], p[i]-ch[ret-2]) <= 0) ret --;
            ch[ret++] = p[i];
        }
        if(n > 1) ret --;
        return ret;
    }
    int Cost(point a, point b){
        return abs(a.x+b.x) * abs(a.y+b.y) % mod;
    }
    LL minl(LL a, LL b) {return a < b ? a : b;}
    int main(){
        while(scanf("%d%d", &n, &mod) != EOF){
            for(int i = 0; i < n; i ++){
                scanf("%d%d", &p[i].x, &p[i].y);
            }
            int ret = ConvexHull();
            if(ret != n) printf("I can't cut.
    ");
            else{
                for(int i = 0; i < n; i ++){
                    for(int j = i+1; j < n; j ++) f[j][i] = f[i][j] = Cost(ch[i], ch[j]);
                }
                for(int i = 0; i < n; i ++) for(int j = 0; j < n; j ++) dp[i][j] = inf;
                for(int i = 0; i < 2; i ++) for(int j = 0; j < n; j ++) dp[j][(j+i)%n] = 0;
                for(int i = 2; i < n-1; i ++){
                    for(int j = 0; j < n; j ++){
                        dp[j][(j+i)%n] = minl(dp[j][(j+i)%n], minl(dp[j][(j+i-1)%n] + f[j][(j+i)%n], dp[(j+1)%n][(j+i)%n] + f[j][(j+i)%n]));
                    }
                }
                //for(int i = 0; i < n; i ++) {for(int j = 0; j < n; j ++) printf("%I64d ", dp[i][j]); printf("
    ");}
                for(int i = 0; i < n; i ++){
                    for(int j = 2; j < n; j ++){
                        for(int o = j+2; o < n; o ++){
                            int jj = (i+j)%n ; int oo = (i+o)%n;
                            if(o == n-1)
                                dp[i][oo] = minl(dp[i][oo], dp[i][jj] + dp[jj][oo]);
                            else dp[i][oo] = minl(dp[i][oo], dp[i][jj] + dp[jj][oo] + f[i][oo]);
                //            printf("%d %d %d
    ", i, oo, dp[i][oo]);
                        }
                    }
                }
                LL mn = inf;
                for(int i = 0; i < n; i ++) mn = minl(dp[i][(i+n-2)%n], mn);
                printf("%lld
    ", mn);
            }
        }
        return 0;
    }
    
    /*
    6 123
    2 2 3 2 2 4 3 4 1 3 4 3
       */
    

      

  • 相关阅读:
    一次完整的HTTP事务的过程
    移动端事件
    canvas 在视频中的用法
    理解Vue
    改变console.log的输出样式
    百度下拉搜索案例
    Vue总结
    字符串、正则
    .Net Mvc框架知识点
    Lowest Common Ancestor of a Binary Tree -- LeetCode
  • 原文地址:https://www.cnblogs.com/acmood/p/4471343.html
Copyright © 2020-2023  润新知