Codeforces 868F (Codeforces Round #438 F) Yet Another Minimization Problem DP+分治

F. Yet Another Minimization Problem

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array of n integers a1... an. The cost of a subsegment is the number of unordered pairs of distinct indices within the subsegment that contain equal elements. Split the given array into k non-intersecting non-empty subsegments so that the sum of their costs is minimum possible. Each element should be present in exactly one subsegment.

Input

The first line contains two integers n and k (2 ≤ n ≤ 1052 ≤ k ≤ min (n, 20))  — the length of the array and the number of segments you need to split the array into.

The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the array.

Output

Print single integer: the minimum possible total cost of resulting subsegments.

Examples
input
7 3
1 1 3 3 3 2 1
output
1
input
10 2
1 2 1 2 1 2 1 2 1 2
output
8
input
13 3
1 2 2 2 1 2 1 1 1 2 2 1 1
output
9
Note

In the first example it's optimal to split the sequence into the following three subsegments: [1][1, 3][3, 3, 2, 1]. The costs are 00 and 1, thus the answer is 1.

In the second example it's optimal to split the sequence in two equal halves. The cost for each half is 4.

In the third example it's optimal to split the sequence in the following way: [1, 2, 2, 2, 1][2, 1, 1, 1, 2][2, 1, 1]. The costs are 441.




把n个数分成k个连续序列。对于每个序列,定义花费为:每段值相同的数对个数。求最小花费。


很容易列出二维DP方程:dp[i][j]=min(dp[i-1][k]+cost(k+1,j) k<j dp[i][j]表示前 j 个数字分了 i 个序列。

观察发现,每多分一个序列,当我们向右移动多出来的这个序列的右界时,左界只能单调地向右移动而不能向左,否则将破坏最优性。也就是说,左界具有DP的决策单调性。

那么,用类似于cdq分治的方法,对于每层定义四个值l,r,L,R,分别表示新分的序列的右界范围和左界范围。定义m=(l+r)/2,在每层穷举对于右界为m的序列的左界最优决策是哪里,之后继续递归求解(l,m-1),(m+1,r)两段的解。每多分一个序列复杂度为O(nlogn),总的复杂度是O(knlogn)。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn = 100005, inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const ld pi = acos(-1.0L);
ll a[maxn], dp[maxn][2], t[maxn];

void solve(int l, int r, int L, int R, int u, ll sum) {
	if (l > r) return;
	int m = (l + r) / 2, M = -1, i;
	ll mc = llinf;
	int p = min(m, R);
	for (i = l; i <= m; i++) sum += t[a[i]]++;
	for (i = L; i <= p; i++) {
		sum -= --t[a[i]];
		if (sum + dp[i][u ^ 1] < dp[m][u]) {
			M = i; dp[m][u] = sum + dp[i][u ^ 1];
		}
	}
	for (i = l; i <= m; i++) sum -= --t[a[i]];
	for (i = L; i <= p; i++) sum += t[a[i]]++;
	solve(l, m - 1, L, M, u, sum);
	for (i = L; i < M; i++) sum -= --t[a[i]];
	for (i = l; i <= m; i++) sum += t[a[i]]++;
	solve(m + 1, r, M, R, u, sum);
	for (i = L; i < M; i++) t[a[i]]++;
	for (i = l; i <= m; i++) t[a[i]]--;
}

int main() {
	int n, k, i, j;
	scanf("%d%d", &n, &k);
	mem0(t);
	dp[0][0] = 0;
	for (i = 1; i <= n; i++) {
		scanf("%I64d", &a[i]);
		dp[i][0] = dp[i-1][0] + t[a[i]]++;
	}
	for (i = 1; i < k; i++) {
		mem0(t);
		for (j = 1; j <= n; j++) dp[j][i % 2] = llinf;
		solve(1, n, 1, n, i%2, 0);
	}
	printf("%I64d\n", dp[n][(k + 1) % 2]);
//	system("pause");
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值