Chopsticks
题意:
n个数3个相邻是一组,求选k组使得,各组组内较小的两个数的差之和最小。
分析:
对于每个数选或不选的问题,dp[i][j]表前i个数选了j组得到的最小和。
dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+差)选或不选,数应该降序排列。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int dp[][];
int l[];
int n,k;
void solve(){
for(int i=;i<=n;++i){
dp[i][]=;
for(int j=;j<=k;++j)
dp[i][j]=INF;
}
for(int i=;i<=n;++i)
for(int j=;j<=k;++j)
if(i>=*j&&dp[i-][j-]!=INF){
dp[i][j]=min(dp[i-][j],dp[i-][j-]+(l[i]-l[i-])*(l[i]-l[i-]));
}
printf("%d\n",dp[n][k]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&k,&n);
for(int i=n;i>;--i)
scanf("%d",&l[i]);
k+=;
solve();
}
return ;
}
Chopsticks的更多相关文章
- ZOJ 1234 Chopsticks
原题链接 题目大意:有这么一个公式 A,B,C(A<=B<=C), (A-B)^2来衡量这对数字的好坏,值越小越好.现在给出一个数组,要求每三个配对,最后得到的每组值总和最小. 解法:我是 ...
- ZOJ 1234 Chopsticks(动态规划)
Chopsticks 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=234 题目大意:给定n个筷子的长度,取k+8套筷 ...
- uva 10271 Chopsticks(dp)
题目连接:10271 - Chopsticks 题目大意:给出m和n, 然后给出n根筷子从小到大给出, 现在要从这n根筷子中选出m + 8组筷子, 每组筷子包括三根, 现在要求所有m + 8组每组筷子 ...
- hdu 1500 Chopsticks DP
题目链接:HDU - 1500 In China, people use a pair of chopsticks to get food on the table, but Mr. L is a b ...
- 【HDOJ】1500 Chopsticks
DP. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm ...
- hdu 1500 Chopsticks
http://acm.hdu.edu.cn/showproblem.php?pid=1500 dp[i][j]为第i个人第j个筷子. #include <cstdio> #include ...
- SOJ 1685:chopsticks(dp)
题目链接 说实话挺喜欢soj的界面,简简单单,没有多余的东西hhh(但是简单到连内存限制,时间限制都看不到了. 题意是有个“奇葩”的主人公,吃饭要用三根筷子.两根短的一根长的. 现在给你n根筷子,要在 ...
- UVA-10271 Chopsticks (线性DP)
题目大意:在n个数中,找出k个三元组(a<=b<=c),求最小的(a-b)*(a-b)之和. 题目分析:将所有数从大到小排序,定义dp(i,j)表示前 i 个数中找出 j 个三元组时的最小 ...
- zoj 2068 - Chopsticks
题目:非常多人在一起吃饭.有两组单支的筷子,定义badness为一对筷子长度差的平方,求最小的badness和. 分析:dp,最大公共子序列类似物. 这里利用数学关系找到一个结论: a < b ...
随机推荐
- 非root模式下安装mysql
1. 下载mysql.tar.gz wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz 2. 下载cmake [ ...
- WPF如何用TreeView制作好友列表、播放列表
WPF如何用TreeView制作好友列表.播放列表 前言 TreeView这个控件对于我来说是用得比较多的,以前做的小聊天软件(好友列表).音乐播放器(播放列表).类库展示器(树形类结构)等都用的是T ...
- flex 4 写皮肤
皮肤容器:s:SparkSkin 主机组件: [HostComponent("spark.components.Panel")] 绘制: <s:Group left=&qu ...
- (七)后台.apsx.cs获取前台客户端文本框的内容
<input ID='AllLocalData' name='AllLocalDataName' /> 其中最重要的一点是Request.Form[]中括号是放的name属性而非Id属性. ...
- c语言类型转换注意事项
转载自: http://blog.csdn.net/zhuimengzh/article/details/6728492 1.隐式转换 C在以下四种情况下会进行隐式转换: 1.算 ...
- 《c程序设计语言》读书笔记--子函数原型和声明的形参
#include <stdio.h> #define Num 20 int power(int base,int n) { int p = 1; int i; for(i = 0;i &l ...
- 下拉刷新控件(5)SwipeRefreshLayout官方教程(下)响应刷新事件
http://developer.android.com/training/swipe/respond-refresh-request.html This lesson shows you how t ...
- HDU 4651 Partition(整数拆分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4651 题意:给出n.求其整数拆分的方案数. i64 f[N]; void init(){ f[0 ...
- How to: Read Object Data from an XML File
This example reads object data that was previously written to an XML file using the XmlSerializer cl ...
- Java C++ Python PHP JS等各种语言中的INT最值
Java: Integer.MAX_VALUE; Integer.MIN_VALUE; C++ INT_MAX INT_MIN <limit.h> 有些其他头文件也有引用 Python & ...