构造分组背包(CF)
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
2 5 1
01001
10110
5
2 5 0
01001
10110
8
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
题意 : 输入数据有 n 行,代表天数,然后每天有 m 节课, 他可以选择性的翘 K 节课,问最终在校时间最短是多少。
思路分析 : 最开始想的是 3 层 for 的大暴力,每次逃的课一定是某一天的最左或最右的课,但是这样不一定对,可以想一想
后来看了大佬的博客,想明白了,是一个分组背包的问题,我们要做的就是提前预处理出在某一天我们逃 x 节课的所减少的在校时间,当一天的课全部逃后,那么所减少的在校时间就是 m ,然后 3层 for 搞定dp 就ok了
代码示例 :
int n, m, p;
char s[505];
int pos[505][505], num[505];
int c[505][505]; // 删除 k 个字符后的花费最多可以节约的花费
int dp[505]; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> m >> p;
for(int i = 1; i <= n; i++){
scanf("%s", s+1);
int pp=1;
for(int j = 1; j <= m; j++){
if (s[j] == '1') {num[i]++, pos[i][pp++] = j;}
}
}
for(int i = 1; i <= n; i++){
for(int j = 0; j <= num[i]-1; j++){
int res = inf;
for(int k = 0; k <= num[i]-1; k++){
if (j-k < 0) break;
int q = j - k; // q右边开始选多少个1 k表示左边
res = min(res, pos[i][num[i]-q]-pos[i][k+1]+1);
}
c[i][j] = m-res;
}
c[i][num[i]] = m;
}
//for(int i = 1; i <= n; i++){
//for(int j = 0; j <= m; j++){
//cout << c[i][j] << " " ;
//}
//cout << endl;
//}
for(int i = 1; i <= n; i++){
for(int j = p; j >= 0; j--){
for(int k = 0; k <= min(num[i], j); k++){
dp[j] = max(dp[j], dp[j-k]+c[i][k]);
}
}
//printf("i = %d dp = %d\n",i, dp[p]);
}
cout << n*m-dp[p] << endl;
return 0;
}
构造分组背包(CF)的更多相关文章
- #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable
2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...
- HDU 1712 ACboy needs your help(分组背包)
题意:给你n的课程组,每个课程组有m个课程,每个课程有一个完成时间与价值.问在m天内每组课程组最多选择一个,这样可以得到的最大价值是多少 题解:分组背包,其实就是每个课程组进行01背包,再在课程组内部 ...
- Codeforces Round #383 (Div. 2) D 分组背包
给出一群女孩的重量和颜值 和她们的朋友关系 现在有一个舞台 ab是朋友 bc是朋友 ac就是朋友 给出最大承重 可以邀请这些女孩来玩 对于每一个朋友团体 全邀请or邀请一个or不邀请 问能邀请的女孩的 ...
- HDU 3033 分组背包变形(每种至少一个)
I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1712 分组背包
ACboy needs your help Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...
- HDU3535AreYouBusy[混合背包 分组背包]
AreYouBusy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ1837 Balance[分组背包]
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13717 Accepted: 8616 Descript ...
- Codevs1378选课[树形DP|两种做法(多叉转二叉|树形DP+分组背包)---(▼皿▼#)----^___^]
题目描述 Description 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修 ...
随机推荐
- C# GUID ToString
最近在看到小伙伴直接使用 Guid.ToString ,我告诉他需要使用 Guid.ToString("N") ,为什么需要使用 N ,因为默认的是 D 会出现连字符. Guid ...
- UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]
解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...
- javascript异步编程 Async/await
Async/await Async/await 在学习他之前应当补充一定的 promise 知识 它是一种与 promise 相配合的特殊语法,目前被认为是异步编程的终级解决方案 值得我们每一个人学习 ...
- computed计算属性(二)
一.说明 在computed中,可以定义一些属性,即计算属性. 计算属性本质是方法,只是在使用这些计算属性的时候,把他们的名称直接当作属性来使用,并不会把计算属性当作方法去调用,不需要加小括号()调用 ...
- vue-learning:39 - router - vue-router的基本使用
vue-router路由的基本使用 一张图阐述vue-router的基本使用步骤 // 0. 如果全局使用CDN引入:vue 引入在前,vue-router引入在后 // <script src ...
- js的cookie操作及知识点详解
<html> <head> <script type="text/javascript"> function getCookie(c_name) ...
- gu集合
离散型随机变量的一切可能的取值 与对应的概率 乘积之和称为该离散型随机变量的数学期望,本题期望是概率乘得分之和 数列是递增的,可以枚举第二小的数,假设选第i个数为第2小的数,则第1小的数有i-1种 ...
- C++模板特化与偏特化
C++模板 说到C++模板特化与偏特化,就不得不简要的先说说C++中的模板.我们都知道,强类型的程序设计迫使我们为逻辑结构相同而具体数据类型不同的对象编写模式一致的代码,而无法抽取其中的共性,这样显然 ...
- Elasticsearch搜索调优
最近把搜索后端从AWS cloudsearch迁到了AWS ES和自建ES集群.测试发现search latency高于之前的benchmark,可见模拟数据远不如真实数据来的实在.这次在产线的bac ...
- JS事件之自建函数bind()与兼容性问题解决
JavaScript事件绑定常用方法 对象.事件 = 函数; 它只能同时为一个对象的一个事件绑定一个响应函数 不能绑定多个,如果有多个,后面的会覆盖前面的 addEventListener() 此方法 ...