ACM-ICPC SOJ-1685 Chopsticks

SOJ 1685 Chopsticks

Descirption

In China, people use a pair of chopsticks to get food on the table, but Mr. L is a bit different. He uses a set of three chopsticks – one pair, plus an EXTRA long chopstick to get some big food by piercing it through the food. As you may guess, the length of the two shorter chopsticks should be as close as possible, but the length of the extra one is not important, as long as it's the longest. To make things clearer, for the set of chopsticks with lengths A,B,C(A<=B<=C), (A-B)^2 is called the 'badness' of the set.
It's December 2nd, Mr.L's birthday! He invited K people to join his birthday party, and would like to introduce his way of using chopsticks. So, he should prepare K+8 sets of chopsticks(for himself, his wife, his little son, little daughter, his mother, father, mother-in-law, father-in-law, and K other guests). But Mr.L suddenly discovered that his chopsticks are of quite different lengths! He should find a way of composing the K+8 sets, so that the total badness of all the sets is minimized.

Input

The first line in the input contains a single integer T, indicating the number of test cases(1<=T<=20). Each test case begins with two integers K, N(0<=K<=1000, 3K+24<=N<=5000), the number of guests and the number of chopsticks. There are N positive integers Li on the next line in non-decreasing order indicating the lengths of the chopsticks.(1<=Li<=32000).

Output

For each test case in the input, print a line containing the minimal total badness of all the sets.

Sample Input

1
1 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 160 162 164

Sample Output

23

Note

For the sample input, a possible collection of the 9 sets is:
8,10,16; 19,22,27; 61,63,75; 71,72,88; 81,81,84; 96,98,103; 128,129,148; 134,134,139; 157,157,160

解题思路

这道题目如果第三根筷子会简单不少,首先证明任意一根筷子一定与与之相邻的筷子的Badness最短,所以讲每一个筷子与他的前一个的Badness放入dp中;然后从后往前,每取出一根筷子有两种情况:1、使用当前的筷子和下一根构成一双筷子;2、不适用;由此写出当前问题的解:slove(i, j) = min(slove(i-2, j+1) + dp[i][j], slove(i-1, j));

本题中还加入了第三根筷子,所以使用了变量max记录前面未使用的筷子个数,只有max > 0时,才可以组成一双筷子。

由于有大量的重复运算,所以使用table[total][index]来记录total根筷子中查找index双筷子的结果。

Code

#include <stdio.h>
#include <string.h>

int dp[5050];
int s[5050];
int table[5050][1009];

int k, m;
int max = 0;

int min(int a, int b)
{
    return a <= b ? a : b;
}

int squ(int  a)
{
    return a * a;
}

int slove(int total, int index)
{
    if (index >= k) return 0;
    if (total <= 0) return 0xffffff;
    if (table[total][index] >= 0) return table[total][index];

    if (max <= 0)
    {
        max++;
        table[total][index] = slove(total -1, index);
    }
    else
    {
        max--;
        int x = dp[total] + slove(total - 2, index+1);
        max++;

        max++;
        table[total][index] = min(x, slove(total - 1, index));
        max--;

        if (table[total][index] == x) max--;
        else max++;
    }

    return table[total][index];
}

int main()
{
    int t = 0;
    scanf("%d", &t);

    while (t--)
    {
        scanf("%d %d", &k, &m);
        k += 8, max = 1;

        memset(dp, 0, sizeof(dp));
        memset(s, 0, sizeof(s));
        memset(table, -1, sizeof(table));

        for (int i = 0; i < m; i++) scanf("%d", &s[i]);
        for (int j = m - 1; j > 0; j--) dp[j] = squ(s[j] - s[j - 1]);

        printf("%d\n", slove(m - 2, 0));
    }

    return 0;
}

后记

花了几个小时,还是没能相出比较好的解决方案,毕竟能力有限。代码优化不够好,SOJ上跑了一秒钟才过,比最优的答案慢了10倍。

你可能感兴趣的:(算法,动态规划,ACM-ICPC)