当前位置:网站首页>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;
}
边栏推荐
- 达梦数据库大表添加字段
- It turns out that Maya Arnold can also render high-quality works!Awesome Tips
- 强网杯2022 pwn 赛题解析——house_of_cat
- 2022 Fusion Welding and Thermal Cutting Operation Certificate Exam Questions and Mock Exams
- Mysql 死锁和死锁的解决方案
- 2006年星座运势全解-射手
- Use of thread pool (combined with Future/Callable)
- 唤醒手腕 - 微信小程序、QQ小程序、抖音小程序学习笔记(更新中)
- 数据库——概述
- After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career
猜你喜欢
随机推荐
openSource 知:社区贡献
Flink学习11:flink程序并行度
图片地址转为base64
4520. 质数
环网冗余式CAN/光纤转换器 CAN总线转光纤转换器中继集线器hub光端机
谷歌零碎笔记之MVCC(草稿)
Shiny02---Shiny exception solution
Flink学习10:使用idea编写WordCount,并打包运行
Cannot compare or sort text, ntext, and image data types
Stored procedure writing experience and optimization measures
微信 小程序 之PC端 不支持 wx.previewMedia 方法 故用自定义轮播图进行 模拟照片视频的播放
"Automatic Data Collection Based on R Language"--Chapter 3 XML and JSON
Redis常用命令
Tencent Business Security Post IDP Talk Summary
TRACE32——Break
利用Jenkins的持续集成
本地能ping通虚拟机,虚拟机ping不通本地
每月稳定干2万
C-Eighty seven(背包+bitset)
在原有数据库基础上执行sql文件有则跳过没有则添加如何实现?