zoukankan      html  css  js  c++  java
  • 傻逼题做了一晚上~第一发凸包(套模板)~区间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
       */
    

      

  • 相关阅读:
    回调函数
    TFS(Team Foundation Server)介绍和入门
    android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
    非常多人不愿意承认汉澳sinox已经超过windows
    vim: 自己定义颜色主题(colorscheme)
    银行家算法
    一道百度架构题
    select poll使用
    LDAPserver的安装
    在eclipse中启动tomcat时报错:could not create java virtual machine... a fatal error...
  • 原文地址:https://www.cnblogs.com/acmood/p/4471343.html
Copyright © 2011-2022 走看看