Cake ZOJ - 3537

凸包处理后,枚举注意切开后至少是个三角形所以R-L> 2

    #include <ctime>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    using namespace std;
    #define mem(a,b) memset(a,b,sizeof a)
    #define sci(num) scanf("%d",&num)
    #define scl(num) scanf("%lld",&num)
    #define scd(num) scanf("%lf",&num)
    typedef long long LL;
    const int MAX_N = 310;
    const double EPS = 1e-10;
    LL N,MOD;
    LL cost[MAX_N][MAX_N];
    LL add(LL a,LL b)
    {
        return a + b;
    }
    struct P
    {
        LL x,y;
        P() {}
        P(double x,double y) : x(x),y(y){
        }
        P operator + (P p)
        {
            return P(add(x,p.x),add(y,p.y));
        }
        P operator - (P p)
        {
            return P(add(x,-p.x),add(y,-p.y));
        }
        P operator * (double d)
        {
            return P(x * d,y * d);
        }
        LL dot (P p)
        {
            return add(x * p.x ,y * p.y);
        }
        LL det(P p)
        {
            return add(x * p.y , -y * p.x);
        }
    } points[MAX_N];
    vector<P> convex;
    bool cmp_x(const P& p,const P& q)
    {
        if (p.x != q.x) return p.x < q.x;
        return p.y < q.y;
    }
    vector<P> convex_hull(P* ps,int n)
    {
        sort(ps,ps + N,cmp_x);
        int k = 0;
        vector<P> qs(n * 2);
        for (int i = 0;i < n;i++)
        {
            while (k > 1 && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) k--;
            qs[k++] = ps[i];
        }
        for (int i = n - 2,t = k;i >= 0;i--)
        {
            while (k > t && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) k--;
            qs[k++] = ps[i];
        }
        qs.resize( k - 1);
        return qs;
    }
    LL dp[MAX_N][MAX_N];
    LL cal(int l,int r) {
        if (dp[l][r] != -1 )return dp[l][r];
        if (r - l <= 2) return 0;
        dp[l][r] = 1LL<<60;
        for (int i = l+1;i < r;i++) {
            dp[l][r] = min(dp[l][r],cal(l,i) +cal(i,r) + cost[l][i] + cost[i][r]);
        }
        return dp[l][r];
    
    }
    int main()
    {
        while (~scanf("%lld%lld",&N,&MOD)) {
    
            for (int i = 0;i < N;i++) {
                scl(points[i].x); scl(points[i].y);
            }
            convex = convex_hull(points,N);
            int nc = convex.size();
            if (nc != N) {
                printf("I can't cut.\n");
                continue;
            }
            mem(cost,0);
            mem(dp,-1);
            for (int i = 0;i < nc;i++) {
                for (int j = i + 2;j < nc;j++) {
                    P plus = convex[i] + convex[j];
                    cost[i][j] = cost[j][i] = abs((LL)plus.x) * abs((LL)plus.y) % MOD;
                }
            }
            printf("%lld\n",cal(0,N - 1));
        }
        return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值