UVA10271_Chopsticks
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的更多相关文章
随机推荐
- RestController和Controller的区别
知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用. 1) 如果只是使用@RestController注解Controller,则Co ...
- JavaScript深入之类数组对象与arguments(转载)
类数组对象 所谓的类数组对象: 拥有一个 length 属性和若干索引属性的对象 举个例子: var array = ['name', 'age', 'sex']; var arrayLike = { ...
- 修改Oracle并行度
什么是并行度: 并行度的优点就是能够最大限度的利用机器的多个cpu资源,是多个cpu同时工作,从而达到提高数据库工作效率的目的.在系统空闲时间,使用并行是个不错的选择,但是好东西总是相对而言,没有绝对 ...
- 消费者与生产者---LinkedList方式模拟
采用LinkedList数据结构方式来模拟消费者与生产者模型,小Demo import java.util.LinkedList; public class MyQueue { private fin ...
- JSP项目中使用ueditor(百度编辑器)
1. 从http://ueditor.baidu.com下载JSP版本的ueditor,注意GBK和UTF-8两种版本. 2. 在Web Project项目的WebRoot目录下新增thirdpart ...
- Python 语音识别
调用科大讯飞语音听写,使用Python实现语音识别,将实时语音转换为文字. 参考这篇博客实现的录音,首先在官网下载了关于语音听写的SDK,然后在文件夹内新建了两个.py文件,分别是get_audio. ...
- 一张图理解"Figure", "Axes", "Axis"
Figure is the object with the highest level in the hierarchy. It corresponds to the entire graphical ...
- The main Method
The main Method You can call static methods without having any objects. For example, you never const ...
- excel VBA 编程
Dim cuttent_columns As Integer Dim care_repeat As Integer Private Sub Workbook_Open() Dim i As Integ ...
- 数据挖掘:周期性分析SMCA算法
数据挖掘:周期性分析SMCA算法 原文地址:http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1423978 算法介绍 以时间顺序挖掘周期性的模式 ...