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 =  1000000007;
int dp[5010][1010];
int l[5010];
int n,k;
void solve(){
   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>=3*j&&dp[i-2][j-1]!=INF){
        dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(l[i]-l[i-1])*(l[i]-l[i-1]));
            }
    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>0;--i)
            scanf("%d",&l[i]);
               k+=8;
        solve();
    }
return 0;
}
时间: 2024-11-03 05:37:03

Chopsticks的相关文章

UVA 10271 Chopsticks(线性DP)

In China, people use a pair of chopsticks to get food on the table, but Mr. L is a bit different. He uses a set of three chopsticks – one pair, plus an EXTRA long chopstick to get some big food by piercing it through the food. As you may guess, the l

uva10271 - Chopsticks(递推)

题目:uva10271 - Chopsticks(递推) 题目大意:给出N支筷子,值代表长度,现在要求在这些筷子中选出K对,每对筷子(A,B,C),badness(B- A)^2.要求总的badness最小. 解题思路:选择相邻的筷子来做A和B,这样的badness肯定比较小.但是还要考虑C比较麻烦.最后看了大神的题接,筷子应该从长到短开始考虑,dp[k][j]:前j根筷子凑出了K对,最小的badness. dp[k][j] = Min ((dp[k][j - 1], dp[k - 1][j -

hdu 1500 Chopsticks

http://acm.hdu.edu.cn/showproblem.php?pid=1500 dp[i][j]为第i个人第j个筷子. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int dp[1011][5011]; 7 int a[5011]; 8 int k,n; 9 int sqr(int x) 10 { 11 return x

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 bit different. He uses a set of three chopsticks -- one pair, plus an EXTRA long chopstick to get some big food by piercing it through the food. As you

uva 10271 Chopsticks 【dp】

题目:uva 10271 Chopsticks 题意:从一组数中选出每三个为一组,价值为三个中两个小的差值的平方和,让这个总价值最小. 分析:定义dp[i][j]为从后 i 个中选出 j 对的最小价值. 转移方程:dp[i][j] = min(dp[i-1][j],dp[i+2][j-1]+(a[i]-a[i+1])*(a[i]-a[i+1])) 注意状态转移的条件:就是每组要保留一个最大的值作为第三个值. AC代码: #include <iostream> #include <cstd

UVA10271【Chopsticks】

UVA10271[Chopsticks] Description: In China, people use a pair of chopsticks to get food on the table, but Mr. L is a bit different. He usesa set of three chopsticks – one pair, plus an EXTRA long chopstick to get some big food by piercing it through

UVA-10271 Chopsticks (线性DP)

题目大意:在n个数中,找出k个三元组(a<=b<=c),求最小的(a-b)*(a-b)之和. 题目分析:将所有数从大到小排序,定义dp(i,j)表示前 i 个数中找出 j 个三元组时的最小和,则状态转移方程为dp(i,j)=min(dp(i-1,j),dp(i-2,j-1)),第二种决策是在前i-1个数构成j-1组三元组时必须还要有剩余的数的前提下才能做出.这道题和“搬寝室”和“筷子”类似,同样要填表求解并且注意边界. 代码如下: # include<iostream> # inc

【HDOJ】1500 Chopsticks

DP. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <iostream> 6 using namespace std; 7 8 #define MAXN 5005 9 #define MAXK 1005 10 #define INF 0x3f3f3f3f 11 int a[MAXN]; 12 int

zoj 2068 - Chopsticks

题目:非常多人在一起吃饭.有两组单支的筷子,定义badness为一对筷子长度差的平方,求最小的badness和. 分析:dp,最大公共子序列类似物. 这里利用数学关系找到一个结论: a < b < c < d 时,(c - a)^2 +(d-b)^2 <(d-a^2)+(c-b)^2:(展开就可以) 所以最优解一定不会交叉,然后先用元素少的串,求长串的LCS的就可以. 权值计算用长度差的平方,而不是1. 这里,能够设置两种状态f(i.j): 1.以a[i].b[j]为结束标志的最优