题目链接:HDU - 1500

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.
题意描述:有K+8个人,每个人用3根筷子,现在有n根筷子,知道每根筷子的长度,要求给这些人分配一些筷子,使得每个人短的那两根筷子的差值平方总和最小。
算法分析:对筷子长度从大到小排序,dp[i][j]表示前i个人j根筷子的最优解。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; int k,n,num[maxn];
int dp[+][maxn]; int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&k,&n);
for (int i=n ;i>= ;i--) scanf("%d",&num[i]);
k += ;
memset(dp,,sizeof(dp));
for (int i= ;i<=k ;i++)
{
dp[i][*i]=dp[i-][*i-]+(num[*i-]-num[*i])*(num[*i-]-num[*i]);
for (int j=*i+ ;j<=n ;j++)
dp[i][j]=min(dp[i][j-],dp[i-][j-]+(num[j-]-num[j])*(num[j-]-num[j]));
}
printf("%d\n",dp[k][n]);
}
return ;
}

hdu 1500 Chopsticks DP的更多相关文章

  1. hdu 1500 Chopsticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1500 dp[i][j]为第i个人第j个筷子. #include <cstdio> #include ...

  2. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  3. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  4. hdu 3709 数字dp(小思)

    http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...

  5. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. hdu 4283 区间dp

    You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  8. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  9. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

随机推荐

  1. 孤荷凌寒自学python第九天Python的输出print的格式化

    孤荷凌寒自学python第九天Python的输出print的格式化 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) (今天感觉手写笔记整得清楚些,汇总电子 笔记时,自己思路凌乱了,练习过程也还 ...

  2. 【LeetCode】Swap Nodes in Pairs(两两交换链表中的节点)

    这是LeetCode里的第24题. 题目要求: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定1->2->3->4, 你应该返回2->1->4- ...

  3. Python保护变量、私有变量、私有方法

    保护变量.私有变量.私有方法介绍: _xxx: 单下划线开头叫保护变量,意思是只有类对象和子类对象自己能访问到这些变量,此变量不能通过from XXX import xxx 导入: __xxx : 双 ...

  4. c#中onclick事件请求的两种区别

    在C#中如果是asp控件的button有两个click的调用,一个是OnClick,一个是OnClientClick.那么这两者有什么区别呢,下面就来说说区别. <asp:Button ID=& ...

  5. Codeforces 498D Traffic Jams in the Land | 线段树

    题目大意: 给坐标轴1~n的点,每个点有一个权值,从一个点走到下一个点需要1s,如果当前时间是权值的倍数就要多花1s 给出q组操作,C表示单点修改权值,A表示询问0时刻x出发到y的时间 题解:因为权值 ...

  6. DP———2.最大m子序列和

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. 扩展欧几里得(ex_gcd),中国剩余定理(CRT)讲解 有代码

    扩展欧几里得算法 求逆元就不说了. ax+by=c 这个怎么求,很好推. 设d=gcd(a,b) 满足d|c方程有解,否则无解. 扩展欧几里得求出来的解是 x是 ax+by=gcd(a,b)的解. 对 ...

  8. 7月13号day5总结

    今天学习过程和小结 使用伪分布式进行大数据计算,计算气象站记录气温的平均值 weather map()方法,key值数据多所以用LongWritable,value值是string类型,string类 ...

  9. NGINX: 优化 use 参数

    转自:http://blog.sina.com.cn/s/blog_5eaf88f10100gkrq.html Nginx use参数分析对比 下图对比了poll select epoll和kqueu ...

  10. C# 编写的Windows serice程序. 安装时出现异常!

    初学Windows Service 程序的编写,按照MSDN上写了一个service! 遇到安装服务的错误, 能帮忙看下是什么原因吗? 下面是在命令行下的安装结果: 正在运行事务处理安装. 正在开始安 ...