zoukankan      html  css  js  c++  java
  • HDU 1421 搬寝室(基础dp)

    题意:中文题,自行读题

    思路:emmm,怎么说呢,感觉已经无数次dp初始化有问题造成wa了,我也很好奇为什么窝对于动规边界考虑的这么烂,简直没有带脑子,对于dp[i][j]代表前j和物品取i次了,所以dp转移方程见代码,dp[i][j]是和dp[i-1][j-2]有关的,我以为对于dp状态的定义反过来就不行了,今天思考了一下,发现其实是可以的,而且可能更好转移,对于初始化的要求更低

    代码:

    #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];
    
    int main()
    {
        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
    ",dp[k][n]);
        }
        return 0;
    }
  • 相关阅读:
    使用JDBCTemplate执行DQL/DML语句
    spring中JDBCTemplate的简单应用
    Druid数据库连接池工具类
    Same Tree
    Remove Duplicates from Sorted List
    Length of Last Word
    Remove Element
    Remove Duplicates from Sorted Array
    java-StringBuffer更改功能
    java-StringBuffer的删除功能
  • 原文地址:https://www.cnblogs.com/lalalatianlalu/p/8999437.html
Copyright © 2011-2022 走看看