Codeforces 868F (codeforces Round #438 F) Yet another minimization problem dp+ division

Source: Internet
Author: User
Tags min

F. Yet another minimization problem
Time limit per test 2 seconds memory limit per test megabytes input standard input output standard output

You is given an array of n integers A1 ... an. The cost of a subsegment are the number of unordered pairs of distinct indices within the subsegment that contain equal ele ments. Split the given array into K non-intersecting non-empty subsegments So, the sum of their costs is minimum possible. Each element should is present in exactly one subsegment. Input

The first line contains integers n and K (2≤n≤105, 2≤k≤min (n))-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
2
1 2 1 2 1 2 1 2 1 2
Output
8
Input
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 is 0, 0 and 1, thus the answer is 1.

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

In the third example it's optimal to split the sequence in the following-in-A-do: [1, 2, 2, 2, 1], [2, 1, 1, 1, 2], [2, 1, 1]. The costs is 4, 4, 1.



Divide the number of n into K sequential sequence. For each sequence, the definition is spent: the number of pairs with the same value for each segment. Minimum cost.


It is easy to list two-dimensional DP equations: Dp[i][j]=min (Dp[i-1][k]+cost (k+1,j) k<j Dp[i][j] indicates that the first J digits are divided by the I sequence.

It is observed that, with every more than one sequence, when we move the right side of the sequence to the right, the left boundary can only be moved to the left in a monotonous way and not to the ieft, otherwise it will break the optimality. In other words, the left boundary has the decision Monotonicity of DP.

Then, with a method similar to CDQ, for each layer defined four values L,r,l,r, respectively, the right and left bounds of the sequence of the new sub-range. Defines m= (L+R)/2, where each layer is exhaustive for the left boundary optimal decision of the sequence of M in the right boundary, and then proceeds to the solution of the recursive solution (L,M-1), (m+1,r) two segments. Each more than one sequence of complexity is O (NLOGN), the total complexity is O (KNLOGN).


#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map > #include <queue> #include <deque> #include <vector> #include <set> #include <algorithm&
Gt #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;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.