HDU - 3480 Division
Posted widsom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 3480 Division相关的知识,希望对你有一定的参考价值。
思路:
平行四边形不等式优化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的主要内容,如果未能解决你的问题,请参考以下文章