当前位置:网站首页>USACO3.4 “破锣摇滚”乐队 Raucous Rockers - DP
USACO3.4 “破锣摇滚”乐队 Raucous Rockers - DP
2022-07-05 20:08:00 【小酒窝.】
https://www.luogu.com.cn/problem/P2736
题意
给定 N ( 1 ≤ N ≤ 20 ) N(1\leq N\leq 20) N(1≤N≤20) 首歌,从中选出若干首歌曲,发行 M ( 1 ≤ M ≤ 20 ) M(1\leq M\leq 20) M(1≤M≤20) 张 CD。
每张 CD 最多容纳 T ( 1 ≤ T ≤ 20 ) T(1\leq T\leq 20) T(1≤T≤20) 分钟的音乐,一首歌不能分装在两张 CD 中。CD 数量可以用完,也可以不用完。
根据以下标准进行选择:
- 歌曲必须按照创作的时间顺序在所有的 CD 盘上出现。(注:第 i i i 张盘的最后一首的创作时间要早于第 i + 1 i+1 i+1 张盘的第一首)
- 选中的歌曲数目尽可能地多。
问,能够装进 M M M 张 CD 的最多歌曲数量?
思路
“歌曲必须按照创作的时间顺序在所有的 CD 盘上出现” 这个条件说明不能直接贪心。
考虑用dp,当前容量中最多能够存储的歌曲数量从减掉当前歌曲大小的容量来转移。
而 “一首歌不能分装在两张 CD 中” 这个条件就说明不能简单的从上一个容量来转移,还需要考虑是否分装在两张中。
所以,定义状态 f[i, j, k]
表示,对于前 i
首歌曲,用前 j
张 CD 外加 k
分钟,能够容纳的最多歌曲数量。
那么最终的状态就为 f[n, m, 0]
。
状态转移:
- 首先继承上一位置的状态:
f[i, j, k] = f[i-1, j, k]
; - 当外加的
k
分钟大于当前歌曲大小时,就可以从f[i-1, j, k-a[i]
来转移:f[i, j, k] = f[i-1, j, k-a[i]] + 1
; - 否则,就需要存储在上一张唱片中,从
f[i-1, j-1, t-a[i]]
来转移:f[i, j, k] = f[i-1, j-1, t-a[i]] + 1
;
Code
#include<bits/stdc++.h>
using namespace std;
const int N = 210, mod = 1e9+7;
int T, n, m;
int a[N];
int f[N][N][N];
signed main(){
int t;
cin >> n >> t >> m;
for(int i=1;i<=n;i++) cin >> a[i];
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
for(int k=0;k<=t;k++)
{
f[i][j][k] = f[i-1][j][k];
if(k>=a[i]) f[i][j][k] = max(f[i][j][k], f[i-1][j][k-a[i]] + 1);
else if(t-a[i] >= 0 && j >= 1) f[i][j][k] = max(f[i][j][k], f[i-1][j-1][t-a[i]] + 1);
}
}
}
cout << f[n][m][0];
return 0;
}
经验
状态定义要敢想。
边栏推荐
- JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
- Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
- 【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
- Jvmrandom cannot set seeds | problem tracing | source code tracing
- Thread pool parameters and reasonable settings
- Is it safe for CICC fortune to open an account online?
- B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
- Analysis of openh264 decoded data flow
- 图嵌入Graph embedding学习笔记
- 随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
猜你喜欢
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
Elk distributed log analysis system deployment (Huawei cloud)
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
Recommended collection, my Tencent Android interview experience sharing
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
再忙不能忘安全
港股将迎“最牛十元店“,名创优品能借IPO突围?
leetcode刷题:二叉树15(找树左下角的值)
深度学习 卷积神经网络(CNN)基础
随机推荐
The difference between ID selector and class selector
Recommended collection, my Tencent Android interview experience sharing
c语言oj得pe,ACM入门之OJ~
Elk distributed log analysis system deployment (Huawei cloud)
How to select the Block Editor? Impression notes verse, notation, flowus
-v parameter of GST launch
港股将迎“最牛十元店“,名创优品能借IPO突围?
Cocos2d-x项目总结中的一些遇到的问题
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
浅浅的谈一下ThreadLocalInsecureRandom
95后阿里P7晒出工资单:狠补了这个,真香...
id选择器和类选择器的区别
零道云新UI设计中
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
处理文件和目录名
期货如何网上开户?安不安全?
c語言oj得pe,ACM入門之OJ~
Concept and syntax of function
Parler de threadlocal insecurerandom