UVA_10271_Chopsticks

来源:互联网 发布:大数据专业 编辑:程序博客网 时间:2024/06/07 08:19

10271 - Chopsticks

Time limit: 3.000 seconds

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 nd
a way of composing the K + 8 sets, so that the total badness of all the sets is minimized.
Input
The rst 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.
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
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

说实话,没看出来这个是dp题目

发现贪心不能,二分不能

最后才知道这是一道dp题目


dp也感觉思路不是十分明朗

dp[i][j]代表了到第i根筷子组成了j组可能的最小值

那么如果i<3*j显然情况是不成立的,因为筷子不够用


把筷子的长度降序排列,由于输入保证了升序,那么反过来输入就可以为降序

而如果子状态成立,那么由它加2根筷子,后两根组成一组,而此时扫过的筷子满足i>=j*3

那么后面的这两根筷子一定可以找到一根不短于他们两个中长的的筷子(即满足题意)


那么最小的情况,如果组成0组那么所有的dp都应该是0

取一组的情况则会从其他取一组和取0组的情况中产生

进而第j组会从第j-1组的情况中产生,dp可用


其中第i根筷子如果用来组成一组中的短两根之一

则必定是与它最邻近不短于它的筷子与它组成一组

于是dp[i][j]=dp[i-2][j-1]+v(这两根筷子的权值)

如果这根筷子不用dp[i][j]=dp[i-1][j]


具体代码如下


#include <iostream>#include <stdio.h>#include <string.h>using namespace std;const int MN=5e3+5;const int MK=1e3+5;const int IN=0x7ffffff;int len[MN];int dp[MN][MK];    //i根筷子构成g组可能的最小和int main(){    int t;    int k,n;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&k,&n);        for(int i=n;i>=1;i--)//降序保证最长筷子的存在            scanf("%d",&len[i]);        k+=8;        for(int i=1;i<=n;i++)            for(int j=1;j<=k;j++)                dp[i][j]=IN;        for(int i=3;i<=n;i++)            for(int j=1;j<=k;j++)                if (i>=j*3)//保证可以选出最长的筷子                {                    //cout<<dp[i-1][j]<<" "<<dp[i-2][j-1]<<" "<<(len[i-1]-len[i])*(len[i-1]-len[i])<<" ";                    dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(len[i-1]-len[i])*(len[i-1]-len[i]));                    //cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;                }        printf("%d\n",dp[n][k]);    }    return 0;}


0 0
原创粉丝点击