题目1452:搬寝室(dp题目)
题目链接:http://ac.jobdu.com/problem.php?pid=1452
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
// // 1452 搬寝室.cpp // Jobdu // // Created by PengFei_Zheng on 24/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cmath> #include <limits.h> //#define MAX 0x7fffffff #define MAX_SIZE 2001 #define MAX_ROW 1001 using namespace std; int n, k; int weight[MAX_SIZE]; long long dp[MAX_ROW][MAX_SIZE]; int main(){ while(scanf("%d%d",&n,&k)!=EOF){ for(int i = 1 ; i <= n ; i++){ scanf("%d",&weight[i]); } sort(weight+1,weight+1+n); for(int i = 1 ; i <= n ; i++){ dp[0][i]=0; } for(int i = 1 ; i <= k ; i++){ for(int j = 2*i ; j <= n ; j++){ if(j>2*i){ dp[i][j] = dp[i][j-1]; } else dp[i][j] = INT_MAX; if(dp[i][j] > dp[i-1][j-2]+pow(weight[j]-weight[j-1],2)){ dp[i][j] = dp[i-1][j-2]+pow(weight[j]-weight[j-1],2); } } } printf("%lld\n",dp[k][n]); } return 0; } /************************************************************** Problem: 1452 User: zpfbuaa Language: C++ Result: Accepted Time:50 ms Memory:17176 kb ****************************************************************/
作者: 伊甸一点
出处: http://www.cnblogs.com/zpfbuaa/
本文版权归作者伊甸一点所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接 如有问题, 可邮件(zpflyfe@163.com)咨询.