UVA10271【Chopsticks】

UVA10271【Chopsticks】

Description:

In China, people use a pair of chopsticks to get food on the table, but Mr. L is a bit different. He usesa 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 aspossible, 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)* (A ? B)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 liketo 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.

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


Translation:

有n个数据,给定k,要从中选出k+8个三元组(x,y,z,其中x<=y<=z),每选一次的代价为(x-y)^2,求最小代价和。

Solution:

DP

状态:f[i][j]表示前i个数中选j对,令(ans)min.

转移方程:f[i][j]=min(f[i-1][j],f[i-2][j-1]+(a[i]-a[i-1])^2);

Tips :

1. 题中给定了一个不下降序列a[],这样就免得排序了。

2. 1<=i <=N-1

3. 1<=j <=k+8

4. 3*j <= i 不能枚举所有情况,如i=4,j=2不能成立,因为需要考虑第三根筷子

5. 巧妙的方法是倒着输入a[i],这样保证了前面的数一定大于后面的数,即保证了Z>max(X,Y)

Code:

#include <stdio.h>
#include <string.h>
#define MAXN 6000
#define INF 99999
int t,n,k;
int a[MAXN],f[MAXN][1700];
int min(int a,int b){return a<b?a:b;}
int cmp(int a,int b){return a<b?1:0;}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%d%d",&k,&n);
        for(int i=n;i>=1;i--)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=n;i++)
        {
            f[i][0]=0;
            for(int j=1;j<=k+8;j++)
            {
                f[i][j]=INF;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=k+8;j++)
            {
                if(3*j<=i)
                    f[i][j]=min(f[i-1][j],f[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1]));
            }
        }
        printf("%d\n",f[n][k+8]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-31 06:57:06

UVA10271【Chopsticks】的相关文章

uva 10271 Chopsticks 【dp】

题目:uva 10271 Chopsticks 题意:从一组数中选出每三个为一组,价值为三个中两个小的差值的平方和,让这个总价值最小. 分析:定义dp[i][j]为从后 i 个中选出 j 对的最小价值. 转移方程:dp[i][j] = min(dp[i-1][j],dp[i+2][j-1]+(a[i]-a[i+1])*(a[i]-a[i+1])) 注意状态转移的条件:就是每组要保留一个最大的值作为第三个值. AC代码: #include <iostream> #include <cstd

【Kettle】4、SQL SERVER到SQL SERVER数据转换抽取实例

1.系统版本信息 System:Windows旗舰版 Service Pack1 Kettle版本:6.1.0.1-196 JDK版本:1.8.0_72 2.连接数据库 本次实例连接数据库时使用全局变量. 2.1 创建新转换:spoon启动后,点击Ctrl+N创建新转换 2.2 在新转换界面中,右键点击DB连接,系统会弹出[数据库连接]界面. windows系统环境下,可用${}获取变量的内容. 说明: 连接名称:配置数据源使用名称.(必填) 主机名称:数据库主机IP地址,此处演示使用本地IP(

详解go语言的array和slice 【二】

上一篇  详解go语言的array和slice [一]已经讲解过,array和slice的一些基本用法,使用array和slice时需要注意的地方,特别是slice需要注意的地方比较多.上一篇的最后讲解到创建新的slice时使用第三个索引来限制slice的容量,在操作新slice时,如果新slice的容量大于长度时,添加新元素依然后使源的相应元素改变.这一篇里我会讲解到如何避免这些问题,以及迭代.和做为方法参数方面的知识点. slice的长度和容量设置为同一个值 如果在创建新的slice时我们把

【转载】C++拷贝构造函数(深拷贝,浅拷贝)

对于普通类型的对象来说,它们之间的复制是很简单的,例如:int a=88;int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量.下面看一个类对象拷贝的简单例子. #include <iostream>using namespace std;class CExample {private:     int a;public:     CExample(int b)     { a=b;}     void Show ()     {        cout<

【BZOJ】1799: [Ahoi2009]self 同类分布

[题意]给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数.1 ≤ a ≤ b ≤ 10^18 [算法]数位DP [题解] 感觉这种方法很暴力啊. 枚举数位和1~162(不能枚举0,不然会模0,相当于除0),记忆化f[pos][sum][val],sum表示当前数位和,val表示数字取模枚举的数位和. 每次sum+i和(val*10+i)%MOD转移. sum用减法优化,即记忆化(MOD-sum),但是枚举过程中都要memset,导致效率低下,记忆化效果很差. 要什么方法才能跑1.3s

【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依次更新这log位,如果最高位依然有进位,那么找到最高位后面的第一个0,将中间的所有1变成0,那个0变成1.这个显然要用到线段树,但是复杂度是nlog2n的,肯定过不去. 于是我在考场上yy了一下,这log位是连续的,我们每次都要花费log的时间去修改一个岂不是很浪费?我们可以先在线段树上找到这段区间

【BZOJ4945】[Noi2017]游戏 2-SAT

[BZOJ4945][Noi2017]游戏 题目描述 题解:2-SAT学艺不精啊! 这题一打眼看上去是个3-SAT?哎?3-SAT不是NPC吗?哎?这题x怎么只有8个?暴力走起! 因为x要么不是A要么不是B,所以直接2^8枚举所有x就行了.然后就变成了一个2-SAT问题.假设有两场游戏1,2,分别可以使用的地图为A1,A2,B1,B2,如果有一个限制是1 A 2 A,那么选A1就必须选A2,然后我这个沙茶就开开心心的拿了55分. 为什么不对?我建出来的图显然不对偶啊!考虑逆否命题,选A1就必须选

【BZOJ】2337: [HNOI2011]XOR和路径

[算法]期望+高斯消元 [题解]因为异或不能和期望同时运算,所以必须转为加乘 考虑拆位,那么对于边权为1取反,边权为0不变. E(x)表示从x出发到n的路径xor期望. 对于点x,有E(x)=Σ(1-E(y))(边权1)||E(y)(边权0)/t[x]  t[x]为x的度. 那么有n个方程,整体乘上t[x]确保精度,右项E(x)移到左边--方程可以各种变形. 每次计算完后*(1<<k)就是贡献. 逆推的原因在于n不能重复经过,而1能重复经过,所以如果计算"来源"不能计算n,

【BZOJ】[HNOI2009]有趣的数列

[算法]Catalan数 [题解] 学了卡特兰数就会啦>_<! 因为奇偶各自递增,所以确定了奇偶各自的数字后排列唯一. 那么就是给2n个数分奇偶了,是不是有点像入栈出栈序呢. 将做偶数标为-1,做奇数标为+1,显然当偶数多于奇数时不合法,因为它压不住后面的奇数. 然后其实这种题目,打表就可知啦--QAQ 然后问题就是求1/(n+1)*C(2n,n)%p了,p不一定是素数. 参考bzoj礼物的解法. 看到网上清一色的素数筛+分解质因数解法,不解了好久,感觉写了假的礼物-- 后来觉得礼物的做法才比