码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 3480 Division

时间:2019-05-30 22:05:05      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:visio   pre   double   lse   blank   back   bit   href   name   

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\n", cs, dp[now][n]);
    }
    return 0;
}

 

HDU - 3480 Division

标签:visio   pre   double   lse   blank   back   bit   href   name   

原文地址:https://www.cnblogs.com/widsom/p/10952205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!