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 ...
随机推荐
- nasm fasm yasm 还是masm、gas
留个爪,稍后学习 选择编译器nasm?fasm?yasm?还是masm.gas或其他? 前面三个是免费开源的汇编编译器,总体上来讲都使用Intel的语法.yasm是在nasm的基础上开发的,与nasm ...
- ArcGIS Engine -- 常用方法
空间关系 计算两点间距离 feature平移 计算范围 得到点集合的n倍Envelope范围 查询 查询要素,返回多个要素union后的Geometry 查找图层 得到地图上图层列表 根据名称在地图上 ...
- VS2010/MFC编程入门之二(利用MFC向导生成单文档应用程序框架)
VS2010/MFC编程入门之二(利用MFC向导生成单文档应用程序框架)-软件开发-鸡啄米 http://www.jizhuomi.com/software/141.html 上一讲中讲了VS20 ...
- linux 操作系统下c语言编程入门
2)Linux程序设计入门--进程介绍 3)Linux程序设计入门--文件操作 4)Linux程序设计入门--时间概念 5)Linux程序设计入门--信号处理 6)Linux程序设计入门--消息管理 ...
- 296. Best Meeting Point
题目: A group of two or more people wants to meet and minimize the total travel distance. You are give ...
- java读写中文文件
在用Java程序进行读写含中文的txt文件时,经常会出现读出或写入的内容会出现乱码.原因其实很简单,就是系统的编码和程序的编码采用了不同的编码格式.通常,假如自己不修改的话,windows自身采用的编 ...
- rqnoj-106-最大加权矩形-dp
和我之前做的那个切西瓜的题目相比就是小巫见大巫了.. 运用最长字段和的原理把O(n^4)转化成O(n^3) #include<stdio.h> #include<string.h&g ...
- winform中关于panel中滚动条和键盘事件几点体会
最近在做winform开发中,遇到几个比较寄售的问题,通过上网查找计和自己琢磨,最终都圆满解决呢! 现在我将谈谈我在项目中遇到的问题集解决方案,以供大家参考! 一.就是我在使用键盘的keydown事件 ...
- depth_write
Sets whether or not this pass renders with depth-buffer writing on or not. Format: depth_write <o ...
- C# 类的访问修改符
C#共有五种修饰符:public.private.protected.internal.protected internal. ◆public:公有,对所有类可见,不受任何限制 ◆protected: ...