当前位置:网站首页>P1103 书本整理
P1103 书本整理
2022-08-05 07:31:00 【Recursi】
题目描述
Frank是一个非常喜爱整洁的人。他有一大堆书和一个书架,想要把书放在书架上。书架可以放下所有的书,所以Frank首先将书按高度顺序排列在书架上。但是Frank发现,由于很多书的宽度不同,所以书看起来还是非常不整齐。于是他决定从中拿掉k本书,使得书架可以看起来整齐一点。
书架的不整齐度是这样定义的:每两本书宽度的差的绝对值的和。例如有4本书:
1 × 2 1 \times 2 1×2
5 × 3 5 \times 3 5×3
2 × 4 2 \times 4 2×4
3 × 1 3 \times 1 3×1
那么Frank将其排列整齐后是:
1 × 2 1 \times 2 1×2
2 × 4 2 \times 4 2×4
3 × 1 3 \times 1 3×1
5 × 3 5 \times 3 5×3
不整齐度就是 2 + 3 + 2 = 7 2+3+2=7 2+3+2=7
已知每本书的高度都不一样,请你求出去掉k本书后的最小的不整齐度。
输入格式
第一行两个数字 n n n和 k k k,代表书有几本,从中去掉几本。( 1 ≤ n ≤ 100 , 1 ≤ k < n 1 \le n \le 100, 1 \le k<n 1≤n≤100,1≤k<n)
下面的 n n n行,每行两个数字表示一本书的高度和宽度,均小于 200 200 200。
保证高度不重复
输出格式
一行一个整数,表示书架的最小不整齐度。
样例 #1
样例输入 #1
4 1
1 2
2 4
3 1
5 3
样例输出 #1
3
/* * @Description: To iterate is human, to recurse divine. * @Autor: Recursion * @Date: 2022-08-04 19:24:40 * @LastEditTime: 2022-08-04 21:00:34 */
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;
const int N = 1e3+7;
int dp[N][N];
//f[i][j]:以i作末尾,选了j本书时的最小花费
struct node{
int w;
int h;
}a[N];
bool cmp(node x,node y){
if(x.h<y.h)
return true;
return false;
}
int n ,m;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(dp,INF,sizeof(dp));
cin >> n >> m;
m = n - m;
for(int i = 1;i <= n;i ++)
cin >> a[i].h >> a[i].w;
sort(a + 1,a + 1 + n,cmp);
for(int i = 1;i <= n;i ++)
dp[i][1] = 0;
for(int i = 2;i <= n;i ++)
for(int j = 1;j <= i - 1;j ++)
for(int l = 2;l <= min(i,m);l++)
dp[i][l] = min(dp[i][l],dp[j][l - 1] + abs(a[i].w - a[j].w));
int ans = INF;
for(int i = m;i <= n;i ++)
ans = min(ans,dp[i][m]);
cout << ans << endl;
return 0;
}
边栏推荐
猜你喜欢

binary search tree problem

IO process thread -> communication between processes -> day7

SVG大鱼吃小鱼动画js特效

busybox 知:构建

uniapp time component encapsulates year-month-day-hour-minute-second

2022 Fusion Welding and Thermal Cutting Operation Certificate Exam Questions and Mock Exams

TRACE32——List源代码查看

游戏思考19:游戏多维计算相关:点乘、叉乘、点线面距离计算

Modeling of the MAYA ship

环网冗余式CAN/光纤转换器 CAN总线转光纤转换器中继集线器hub光端机
随机推荐
TRACE32——通用寄存器查看与修改
C# FileSystemWatcher
Invalid operator for data type.The operator is add and the type is text.
关于MP3文件中找不到TAG标签的问题
2022 Fusion Welding and Thermal Cutting Operation Certificate Exam Questions and Mock Exams
每一个女孩曾经都是一个没有泪的天使
Shiny02---Shiny异常解决
双向循环带头链表
GAN生成动漫头像Pytorch
busybox 知:构建
YOLOv3 SPP理论详解(包括CIoU及Focal loss)
Use of thread pool (combined with Future/Callable)
奇怪的Access错误
常用的遍历map的方法
props 后面的数据流是什么?
2022.8.2 模拟赛
2022 crane driver (limited bridge crane) exam question bank and simulation test
Access Denied: "microsoft.web.ui.webcontrols" workaround
Redis 全套学习笔记.pdf,太全了
C-Eighty seven(背包+bitset)