Meaning
Liu Rugia invited K guests to his home for dinner, plus his family: his wife, son, daughter, mother, father, father-in-law, mother-in-law,
So this dinner has a k+8 individual. Because it is to eat Chinese food, so need chopsticks, his family a total of n root chopsticks, and different lengths,
At this time Liu Rugia's acmer nature again exposed, he does not follow the normal everyone to give two chopsticks, but everyone points 3 chopsticks,
One of the longest is used to fork the larger pieces of food, while the other two shorter chopsticks as normal use. To get everyone to use
More comfortable, obviously, to make the short length of two chopsticks as close as possible, set the length of the three chopsticks A,b,c (a<=b<=c), so small
Two will have a squared difference (a-b) ^ 2. Teacher Liu wants you to help him calculate, how to assign, all the squares of the difference and the smallest?
Ideas
Long ago, this problem has been put for many days, it may be two weeks.
During the period would like to discuss with Nothi, the result he said in high school did, well, did noip the topic is to do more ...
Then went to the next Noi problem (Njupt 1581), looked at the original problem is very simple.
The original problem is that each person as long as two chopsticks, to let all the chopsticks length square difference and the smallest. So obviously sort of from small to large,
F (I, j) represents the sum of the minimum squared difference between the first I and the chopstick, and the J individual. Because it makes sense to know that you have to choose two adjacent
Chopsticks can make the square difference as little as possible, so get state transfer
F (i, j) = min{F (i-1, J), F (i-2, j-1) + (Len[i]-len[i-1]) ^2}
Perhaps it is because the first AC the original title, so has been limited my thinking, has been according to the above similar ideas, from small to large sort, and then state transfer,
But because the third one is the longest, it's hard to think.
If a small to large order, the state will have a problem, F (i, j) of the first I, must be the largest one, so he certainly will not take, so
has brought difficulties to state transfer.
Then one day, finally thought: what if from big to small sort? Then the first I of F (i, j) must be the smallest, so he can
Take it!
And then there is a problem, to determine the I, i-1 as a pair of chopsticks, the front also has a chopstick can be used as the longest three root, then,
As long as the guarantee (I-2)-(j-1) *3 >= 1, that is, before the j-1 personal distribution after at least 1 chopsticks, it must be the current I, I-1 Group
The longest root!
The state transfer came out like this:
When (i-2)-(j-1) *3 < 1 o'clock
F (i, j) = f (i-1, J);
When (i-2)-(j-1) *3 >= 1 o'clock
F (i, j) = Min{f (I-1, J), F (i-2, j-2) + (Len[i]-len[i-1]) ^2}
perfect!
Code
/**===================================================== * is a solution for ACM/ICPC problem * * @source: uva- 10271 Chopsticks * @description: DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: Zengshuangde@gmai
l.com * Copyright (C) 2013/09/02 23:45 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> #
Include <cstring> #define SQ (x) ((x) * (x)) using namespace Std;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
const int MAXN = 5100;
int n, m;
int LEN[MAXN];
Former I chopsticks, J personal use//www.bianceng.cn int f[maxn][1010]; int main () {int ncase; scanf ("%d", &ncase); while (ncase--) {scanf ("%d%d", &m, &n); for (int i = 1; I <= N;
++i) scanf ("%d", &len[i]);
m + + 8;
Memset (f, INF, sizeof (f));
for (int i = 0; I <= N; ++i) f[i][0] = 0; for (int i = n-2 i >= 1;-i) {for (int j = m; j >= 1;--j) {F[i][j] = f[i+1][J];
if (F[i+2][j-1]!= INF && (n-i-1)-(j-1) *3 >= 1) f[i][j] = min (F[i][j], f[i+2][j-1] + SQ (len[i]-len[i+1));
} printf ("%d\n", F[1][m]);
return 0; }