HDU 1421 搬寝室
搬寝室
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12604 Accepted Submission(s): 4220
解题思路:将前n项从小到大排序,然后从前n项中选出j对最小平方和;
状态方程:dp[i][j] = min (dp[i-2][j-1] + (a[i]-a[i-1])*(a[i]-a[i-1]), dp[i-1][j]);
解题代码:
// File Name: 搬寝室 1421.cpp
// Author: sheng
// Created Time: 2013年07月15日 星期一 09时55分28秒 #include <stdio.h>
#include <math.h>
#include <algorithm> #include <iostream>
using namespace std; const int max_n = ;
int a[max_n], dp[max_n][max_n/]; int main()
{
int n, k;
while (scanf ("%d%d", &n, &k) != EOF)
{
for (int i = ; i <= n; i ++)
scanf ("%d", &a[i]);
sort(a+, a+n+);
a[] = ;
dp[][] = ;
for (int i = ; i <= n; i ++)
for (int j = ; j <= k ; j ++)
dp[i][j] = 0x7fffffff;
for (int i = ; i <= n; i ++)
{
for (int j = ; j <= i/; j ++)
dp[i][j] = min(dp[i-][j-] + (a[i]-a[i-])*(a[i]-a[i-]), dp[i-][j]);
}
printf ("%d\n", dp[n][k]);
}
return ;
}
G++
HDU 1421 搬寝室的更多相关文章
- hdu 1421:搬寝室(动态规划 DP + 排序)
搬寝室 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 题解报告:hdu 1421 搬寝室(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1421 Problem Description 搬寝室是很累的,xhd深有体会.时间追述2006年7月9 ...
- HDU 1421 搬寝室 (线性dp 贪心预处理)
搬寝室 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu 1421 搬寝室(dp)
Problem Description 搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆, ...
- HDU 1421 搬寝室(经典DP,值得经常回顾)
搬寝室 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status D ...
- HDU 1421 搬寝室 解题报告(超详细)
**搬寝室 Time Limit: 2000/1000 MS Memory Limit: 65536/32768 K Problem Description 搬寝室是很累的,xhd深有体会.时间追述2 ...
- 【dp】HDU 1421 搬寝室
http://acm.hdu.edu.cn/showproblem.php?pid=1421 [题意] 给定n个数,要从n个数中选择k个二元组{x,y},最小化sum{(x-y)^2} 2<=2 ...
- [HDU 1421]搬寝室(富有新意的DP)
题目地址:pid=1421" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1421 题目大 ...
- hdu 1421 搬寝室 (dp)
思路分析: dp[i][j] 表示选取到第 i 个 组成了 j 对的最优答案. 当然排序之后 选取相邻两个是更优的. if(i==j*2) dp[i][j] = dp[i-2][j-1] + w[ ...
随机推荐
- LN : leetcode 292 Nim Game
lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...
- automapper的简单用法
AutoMapper对象转换方面(Object-Object Mapping)对象映射工具,实现对象和对象之间的转化.主要应用在项目的dto,model,entity或viewmodel之间转换,其实 ...
- Mybatis动态SQL
1.动态SQL基本标签 •if •choose (when, otherwise) •trim (where, set) •foreach 2.IF 具体用法 <select id=" ...
- ToolBar存档
上图是将本阶段要完成的结果画面做了标示,结合下面的描述希望大家能明白. colorPrimaryDark(状态栏底色):在风格 (styles) 或是主题 (themes) 里进行设定. App ba ...
- 如何快速重置OUTLOOK2013,2016到初始配置状态,outlook 修改数据文件位置
适用范围: 安装OUTLOOK的机器 知识点分析: 快速清除当前OUTLOOK所有账户,回归到初始配置状态. 操作步骤: WIN+R调出运行 输入: C:\Program Files (x86)\Mi ...
- Linux下cron的使用
cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: /sbin/service c ...
- C#语法功能结构
1.File打开指定文件夹或者文件,"\"为转义字符System.Diagnostics.Process.Start(Application.StartupPath + " ...
- flask页面中Head标签内容为空问题
在使用flask时遇到点问题,以前还没有注意到. 生成页面的时候使用的是模板继承方式,当添加meta标题的时候,本来是添加的base.html模板中的head标签中,但是生成页面后,head中的内容却 ...
- Android BLE API: GATT Notification not received
When setting the value to the descriptor instead of putting descriptor.setValue(BluetoothGattDescrip ...
- Leetcode-Construct Binary Tree from inorder and preorder travesal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...