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 ...
随机推荐
- apimonitor
1.简介 由于Andorid更新很快,较之Droidbox这种通过hook系统动态分析APK行为的方法,APIMonitor这种通过在APK包中注入监控代码(监控API调用然后保存为日志)然后重打包A ...
- 一行代码设置TLabel.Caption的前世今生
第零步,测试代码: procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := 'Hello World'; end ...
- MyBatis学习总结_14_Mybatis使用技巧总结
1. 区分 #{} 和 ${}的不同应用场景 1)#{} 会生成预编译SQL,会正确的处理数据的类型,而${}仅仅是文本替换.对于SQL: select * from student where xC ...
- 《Java编程那点事儿》读书笔记(六)——异常处理
1.抛出异常:throw 异常对象; 下面的代码是一个进制转换代码,可以转换为2进制和8进制,如果输入其他参数,则抛出异常. public static String transform(int va ...
- bootstrap table 服务器端分页例子
1,前台引入所需的js 可以从官网上下载 function getTab(){ var url = contextPath+'/fundRetreatVoucher/fundBatchRetreatV ...
- hdu-4893-Wow! Such Sequence!-线段树【2014多校第三场-J】
题意:一个初始为0的数组,支持三种操作:1.向第k个数添加d,(|d| < 2^31);2.把[l, r]区间内的数字都换成与它最相近的Fibonacci数;3.询问[l, r]区间的和. 思路 ...
- php面向对象(二) $this
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- [Topcoder]AvoidRoads(dp,hash)
题目连接:https://community.topcoder.com/stat?c=problem_statement&pm=1889&rd=4709 题意:给一张n*m的地图,上面 ...
- 浅析JavaScript引用类型之--Object、Array
1.Object类型 对象是某个特定引用类型的实例,新对象有两种创建方式: i.使用new操作符调用构造函数来创建. var person = new Object(); person.name = ...
- how to check unsolved conflicts file list in git merge?
how to check unsolved conflicts file list in git merge?