码迷,mamicode.com
首页 > 其他好文 > 详细

Dp Hdu1421 搬寝室

时间:2014-06-12 15:01:20      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   http   color   com   

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1421

对每相邻的一对取或者不取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
const int INF=0xfffffff;
int Min(int a,int b)
{
    return a>b?b:a;
}
int a[2222];
int dp[2222][1111];
int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF){
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+1+n);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=k;j++)
            dp[i][j]=INF;
        for(int i=0;i<=n;i++)
            dp[i][0]=0;
        for(int i=1;i<=n;i++){
            for(int j=0;j<=i/2;j++){
                dp[i][j]=Min(dp[i-1][j],dp[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1]));
            }
        }
        printf("%d\n",dp[n][k]);
    }
    return 0;
}

  

Dp Hdu1421 搬寝室,布布扣,bubuko.com

Dp Hdu1421 搬寝室

标签:class   blog   code   http   color   com   

原文地址:http://www.cnblogs.com/yigexigua/p/3782008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!