Chopsticks

大致就是有一堆筷子,知道了他们的长度,现在选长度为abc的三个筷子a<=b<=c为一对筷子,质量为(a-b)平方,现在选k双这样的筷子,求质量最小

思路:

第一次看到这个题目,人太弱完全没思路,因为之前状态转移都会有一个明显的子局面或者可以根据新增的i+1变量就行状态转移,可是这一题,一直想不出来如何转移,以为新增的筷子可以是a或b或c,但后面看了题解之后发现,人弱能怪谁!!!这题在最开始状态就很模糊没有建好,而且转移的时候还有技巧

应该这样建dp[i][k]就是前i个筷子组成k双筷子,那么为了方便状态的转移并且使用贪心可以先sort一下从大到小排,并且设定选取筷子的时候默认是再选a,同时他的前一个就是b,这样c只要位于b的前面就行了,c对质量无影响.那么问题来了,万一这样选不存在c了怎么办,所以转移的时候如果j<i*3那么肯定不存在,不进行转移!!!!!!

则转移方程为

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 5e3+100;
const int INF = 0x3f3f3f3f;
int a[maxn],dp[maxn][maxn];
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
int t,n,k;
cin>>t;
while(t--){
cin>>k>>n;k+=8;
for(int i=n;i>0;--i) cin>>a[i];
for(int i=1;i<=n;i++){
dp[i][0]=0;
for(int j=1;j<=k;j++){
dp[i][j]=INF;
}
}
for(int i=3;i<=n;i++){
for(int j=1;j<=k;j++){
if(i>=j*3&&dp[i-2][j-1]!=INF){
dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1]));
}
}
}
cout<<dp[n][k]<<endl; }
return 0;
}


UVA10271_Chopsticks的更多相关文章

随机推荐

  1. each of which 用法

    each of which 在以下為 同位語,非關代. 1. An urn contains two balls, each of which is known to be either white ...

  2. mysql (mariadb)表结构添加修改删除方法总结

    1,添加表字段 alter table table1 add ptel varchar(100) not Null; alter table table1 add id int unsigned no ...

  3. SqlServer 查看表注释

    SELECT DISTINCT d.name, f.value FROM syscolumns a LEFT JOIN systypes b ON a.xusertype= b.xusertype I ...

  4. C# 跨线程调用控件的4中方法

    原文:C# 跨线程调用控件 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应.  同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线 ...

  5. 2.Web中使用iReport 整合----------创建html格式的

    转自:https://wenku.baidu.com/view/104156f9770bf78a65295462.html 1.

  6. Codeforces 735E 树形DP

    题意:给你一棵树,你需要在这棵树上选择一些点染成黑色,要求染色之后树中任意节点到离它最近的黑色节点的距离不超过m,问满足这种条件的染色方案有多少种? 思路:设dp[x][i]为以x为根的子树中,离x点 ...

  7. Hibernate 一对多配置 级联操作(级联失败问题分析解决)

    一方: package com.xdfstar.domain; import java.io.Serializable;import java.util.Date;import java.util.H ...

  8. 输出匹配项:grep

    命令格式: grep pattern [file...] When grep encounters a "pattern" in the file, it prints out t ...

  9. Vue----渐进式框架的理解

    对“渐进式”这三个字的理解:Vue渐进式-先使用Vue的核心库,再根据你的需要的功能再去逐渐增加加相应的插件. 以下理解出处:https://www.zhihu.com/question/519072 ...

  10. 英语单词substitution

    substitution 来源——shell字符串切片 [root@centos73 ~]# echo ${$alpha:3:4} -bash: ${$alpha:3:4}: bad substitu ...