BNU 51640 Training Plan (similar interval DP) (North Division 16 school race)

Source: Internet
Author: User



Small q classmate in order to prepare for this year's ICPC regional, plan in days to brush off the problem, each problem has a difficulty value, the difficulty of the first question is.

However in the semi-decadent state of the small Q students do not want to do in the same day difficult gap between the problem, the definition of the number of days to brush the difficulty of the maximum value minus the minimum (if the day without a brush, then), then the whole plan is difficult.

Small q students can be in any order to brush the question, and the day can be brushed any number of questions, but each problem only need to do once, now small Q students want to know how to complete the total difficulty of this plan is the minimum value is how much.

Input

The first line is a positive integer that represents the number of groups of test data,

For each set of test data,

The first line is two integers and represents the number of questions and the number of days,

The second line is an integer that represents the difficulty value of each problem.

Output

For each set of test data, output an integer that represents the minimum difficulty for the entire plan.

Sample Input
23 31 2 33 21 2 3
Sample Output
01
Hint

For the first set of examples, the best solution is to brush a question one day.

For the second set of samples, an optimal scheme is the first day of the brush difficulty value of 1 and 2, the next day brush difficulty value of 3.

Main topic:

N Days Brush M-Problem, M-question can be arbitrarily assigned, the difficulty of the day is the maximum minimum difficulty of the day of the sum of the square, the difficulty of all days to add the minimum value

Problem Solving Ideas:

The difficulty of these problems from small to large order (from large to small can also), you can conclude that every day do the problem must be here continuous interval, because the assumption jumped N, then exchange this and the current interval of the most difficult problems, and the next day exchange, the answer must be smaller.

Then use Dp[i][j] for the first day to brush to the minimum value of J, then Dp[i][j]=min (dp[i-1][k]+ (a[j]-a[k]) ^2) (k=1...j-1), Dp[n][m] is the request.

Note: The INF must be large enough to open, otherwise it will WA

#include <iostream> #include <stdio.h> #include <algorithm> #include <map> #include < string.h> #include <string> #include <vector> #include <stack> #include <queue> #include < Set> #define C (a) memset (a,0,sizeof a) #define C_1 (a) memset (a,-1,sizeof a) #define C_I (a) memset (a,0x3f,sizeof a) using Namespace Std;typedef long Long ll;const ll INF = 1000000000000000000ll;const int MAXN = 1e6 + 20;int t[maxn];ll a[maxn];i NT Ca;ll dp[520][520];int N, m;int main () {int T; CIN >> T;while (t--) {C (T); C (DP); C (a); CA = 0;scanf ("%d%d", &n, &m); int x;for (int i = 0; i<n; i++) {scanf ("%d", &x); if (t[x] = = 0) a[ca++] = X ; t[x]++;} Sort (A, a + CA); if (ca <= m) {printf ("0\n"); continue;} for (int j = 0; j<ca; j + +) Dp[1][j] = (A[j]-a[0]) * (A[j]-a[0]); for (int i = 2; I <= m; i++) {dp[i][0] = 0;for (int j = 1; j<ca; J + +) {Dp[i][j] = inf;for (int k = 0; k<j; k++) dp[i][j] = min (Dp[i][j], dp[i-1][k] + (A[j]-a[k + 1]) * (A[j]-a[k + 1] ));}} CoUT << dp[m][ca-1] << Endl;} return 0;}


BNU 51640 Training Plan (similar interval DP) (North Division 16 school race)

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.