• HDU


    HDU - 3480

    思路:

    平行四边形不等式优化dp

    同上一篇博客,用滚动数组优化

    代码:

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define y1 y11
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    #define LD long double
    //#define mp make_pair
    #define pb push_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pli pair<LL, int>
    #define pii pair<int, int>
    #define piii pair<int, pii>
    #define pdd pair<long double, long double>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //head
    
    const int N = 1e4 + 5, M = 5e3 + 5;
    int dp[2][N], s[2][N], a[N], n, m, T;
    int main() {
        scanf("%d", &T);
        for (int cs = 1; cs <= T; ++cs) {
            scanf("%d %d", &n, &m);
            for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
            sort(a+1, a+1+n);
            for (int i = 0; i < 2; ++i) for (int j = 0; j <= n; ++j) dp[i][j] = 0x3f3f3f3f, s[i][j] = 0;
            int now = 0;
            dp[now][0] = 0;
            for (int i = 1; i <= m; ++i) {
                s[now^1][n+1] = n-1;
                for (int j = n; j >= 1; --j) {
                    dp[now^1][j] = 0x3f3f3f3f;
                    for (int k = s[now][j]; k <= s[now^1][j+1]; ++k) {
                        if(k+1 <= j && dp[now][k]+(a[j]-a[k+1])*(a[j]-a[k+1]) < dp[now^1][j]) {
                            dp[now^1][j] = dp[now][k]+(a[j]-a[k+1])*(a[j]-a[k+1]);
                            s[now^1][j] = k;
                        }
                    }
                }
                now ^= 1;
            }
            printf("Case %d: %d
    ", cs, dp[now][n]);
        }
        return 0;
    }
  • 相关阅读:
    新组件或功能怎么依附到已有的架构上
    高内聚、低耦合
    软件质量的定义
    软件架构与组件
    架构设计之拥抱着变化而设计(部分讲义分享)
    组件设计原则之概念篇(四)
    抽象类(接口类)的作用
    类,抽象基类,接口类三者间的区别与联系(C++)
    软件的可变性
    软件设计的复杂度
  • 原文地址:https://www.cnblogs.com/widsom/p/10952205.html
Copyright © 2020-2023  润新知