tyvj1191 Dance of the three-person spring party

Source: Internet
Author: User

Background HNSDFZ students in order to celebrate the Spring Festival, ready to rehearse a dance description n individuals elected 3*m, ranked in M group, each group of 3 people.
Station formation-a shorter 2 people stand on both sides, the highest station in the middle.
From the perspective of symmetry, the closer the height of the two people, the lower the disability level of this group.
The formula is h= (A-B) ^2 (the height of the shorter 2 people)
So here's the problem.
Now the candidates have n individuals, to choose from among them 3*m personal row dance, the overall "degree of disability" is the lowest. The first row of the input format is m,n.
The second row of n digits, guaranteed in ascending order. Output format output minimum "degree of disability". Test Sample 1 input
9 40
1 8 10 16 19 22 27 33 36 40 47 52 56 61 63 71 72 75 81 81 84 88 96 98 103 110 113 118 124 128 129 134 134 139 148 157 157 162 164
Output
at
Remark m<=1000,n<=5000
Data assurance 3*m<=n from UVA 10271 chopsticks

For this problem, you should first think of greed: each time the difference between the smallest pair. But such a greedy strategy is easy to find counter-examples, and n=5000 data size, very likely is O (n^2) algorithm.
Then consider dynamic planning. If it is DP, it is easy to think of the following state definition: D[i][j] represents the minimum consumption of the number pairs of I (x, y, z) using the number of pre-J.
Another thing that is easy to notice is that for any number pair (x, Y, z) in an optimal decision, there must be two neighbors in the ordered A[i]. It is easy to note that this is not difficult to prove with contradiction.
The question of how z should be made in (x, Y, z) is always confusing to me, which should be the most difficult problem to solve.

Consider status D[i][j]:
For x and Y, consider the following:
For A[j], if you do not use A[j], then d[i][j]=d[i][j-1];
If using a[j], then use with A[j-1], d[i][j]=d[i-1][j-2]+w (a[i],a[i-1]);
Then there is the total state transfer equation: D[i][j]=min{d[i][j-1],d[i-1][j-2]+w (a[i],a[i-1])};
This should not be difficult to understand, but for the z decision?
If you put a[i] in descending order, then the effect of Z can be ignored! This can still be used in the above equation.

Consider status D[i][j]:
If j<3i, at this time the current strategy is not feasible, d[i][j]=inf;
If j>=3i, i.e. j>=3 (i-1) +3,j>3 (i-1) +2, the current state is valid
Transfer to d[i-1][j-2], at least one surplus a[k]
Because sequence descending, a[k] can be paired with A[j], a[j-1]
While D[i-1][j-2] is valid, recursion can continue.
Transfer to d[i][j-1]
If D[I][J-1] is invalid state, D[i][j-1]==inf, certainly will not be superior to the above kind of transfer way;
If D[I][J-1] is active, it can continue recursively.
#include <iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>using namespacestd;intm,n,a[5005];Long Longdp[1005][5005];intMain () {CIN>>m>>N;  for(inti = N;i >=1; i--) {scanf ("%d",&A[i]); }     for(inti =1; I <= m;i++){         for(intj =1; J <= n;j++){            if(J < I *3) Dp[i][j] =9876543212345L; ElseDp[i][j] = min (dp[i][j-1],dp[i-1][j-2] + (A[j]-a[j-1]) * (A[j]-a[j-1])); }} cout<<Dp[m][n]; return 0;}

tyvj1191 Dance of the three-person spring party

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.