HDU 1421 Moving Dorms (Basic DP)

Question meaning: Chinese questions, self-reading questions

Idea: emmm, how should I put it, I feel that there have been countless dp initialization problems that have caused wa, and I am also curious why the nest is so bad about the boundary of the dynamic gauge, I just don’t have a brain, for dp[i][j] represents the front j and the item have been taken i times, so the dp transfer equation is shown in the code, dp[i][j] is related to dp[i-1][j-2], I thought the definition of dp state would not work in reverse , I thought about it today and found that it is actually possible, and it may be better to transfer, with lower requirements for initialization

Code:

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define zero(a) fabs(a)<eps
#define lowbit(x) (x&(-x))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define MOD 1000000007
int max(int x,int y){return x>y?x:y;};
int min(int x,int y){return x<y?x:y;};
int max(int x,int y,int z){return max(x,max(y,z));};
int min(int x,int y,int z){return min(x,min(y,z));};
typedef long long LL;
const double PI=acos(-1.0);
const double eps=1e-8;
const int inf=0x3f3f3f3f;
const LL linf=0x3f3f3f3f3f3f3f3fLL;
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int maxn=2007;
int dp[maxn][maxn];
int a[maxn];

intmain ()
{
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        for(int i=1;i<=n;i++){
            a[i]=read();
        }
        sort(a+1,a+1+n);
        memset(dp,0x3f,sizeof(dp));
        for(int i=0;i<=n;i++)
                dp[0][i]=0;
        for(int i=1;i<=k;i++){
             for(int j=2;j<=n;j++){
                dp[i][j]=min(dp[i][j-1],dp[i-1][j-2]+(a[j]-a[j-1])*(a[j]-a[j-1]));
             }
        }
        int minn=0x3f3f3f3f;
        printf("%d\n",dp[k][n]);
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325424797&siteId=291194637