POJ1976A Mini Locomotive(01背包装+连续线段长度)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 2485 | Accepted: 1388 |
Description
1. Set the number of maximum passenger coaches a mini locomotive can
pull, and a mini locomotive will not pull over the number. The number is same
for all three locomotives.
2. With three mini locomotives, let them
transport the maximum number of passengers to destination. The office already
knew the number of passengers in each passenger coach, and no passengers are
allowed to move between coaches.
3. Each mini locomotive pulls consecutive
passenger coaches. Right after the locomotive, passenger coaches have numbers
starting from 1.
For example, assume there are 7 passenger coaches, and
one mini locomotive can pull a maximum of 2 passenger coaches. The number of
passengers in the passenger coaches, in order from 1 to 7, is 35, 40, 50, 10,
30, 45, and 60.
If three mini locomotives pull passenger coaches 1-2,
3-4, and 6-7, they can transport 240 passengers. In this example, three mini
locomotives cannot transport more than 240 passengers.
Given the number
of passenger coaches, the number of passengers in each passenger coach, and the
maximum number of passenger coaches which can be pulled by a mini locomotive,
write a program to find the maximum number of passengers which can be
transported by the three mini locomotives.
Input
t (1 <= t <= 11), the number of test cases, followed by the input data for
each test case. The input for each test case will be as follows:
The first
line of the input file contains the number of passenger coaches, which will not
exceed 50,000. The second line contains a list of space separated integers
giving the number of passengers in each coach, such that the ith
number of in this line is the number of passengers in coach i. No coach holds
more than 100 passengers. The third line contains the maximum number of
passenger coaches which can be pulled by a single mini locomotive. This number
will not exceed 1/3 of the number of passenger coaches.
Output
maximum number of passengers which can be transported by the three mini
locomotives.
Sample Input
1
7
35 40 50 10 30 45 60
2
Sample Output
240
题意:
有三个火车头,n个车厢,每个车厢里面对应的有一定的人数。规定每个火车头最多拉m个连续的车厢而且他们拉的车厢一定是从左到右连续的,问它能够拉的最多的人数;
思路:
类似01背包的解法,首先每个火车最多拉m个连续的车厢,这里我们把只要存在连续的m个车厢的就看成一个物品。相当于往背包容量为3的背包里面放物品所得的最大价值量。但是这里注意每连续的m个车厢为一个物品,f[i][j] = max(f[i - 1][j],f[i - m][j - 1] + sum[i] - sum[i - m]); 这里对于每个物品要么不放,要么就是放(放连续的m个车厢)
sum[i] = a[0] + a[1] + ... + a[i];
之前看到这个解题思路感觉一点有疑问:会不会有重复,第一节拉1,2;第二节拉2,3这样的,最后结论是不会;因为不取这个车厢的话,那必然就是【i-1】【j】,如果取的话那么肯定就是【i-m】【j-1】,跳到了i-m了,所以不会重,太弱了,其实这道题也挺简单,就是不会,弱
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int MAX = + ;
int dp[MAX][],sum[MAX],a[MAX];
int main()
{
int t,n,m;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
memset(dp, , sizeof(dp));
memset(sum, , sizeof(sum));
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = ; i <= n; i++)
sum[i] = sum[i - ] + a[i];
scanf("%d", &m);
int tp;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= ; j++)
{
if(i < m) //因为最多是m节,不足m也是可以的,需要处理一下
{
tp = ;
}
else
tp = i - m;
dp[i][j] = max(dp[i - ][j], dp[tp][j - ] + sum[i] - sum[tp]);
}
}
printf("%d\n", dp[n][]);
}
return ;
}
POJ1976A Mini Locomotive(01背包装+连续线段长度)的更多相关文章
- A Mini Locomotive(01背包变型)
题目链接: https://vjudge.net/problem/POJ-1976 题目描述: A train has a locomotive that pulls the train with i ...
- POJ-1976-A Mini Locomotive-dp
A train has a locomotive that pulls the train with its many passenger coaches. If the locomotive bre ...
- A Mini Locomotive(动态规划 01)
/* 题意:选出3个连续的 数的个数 为K的区间,使他们的和最大 分析: dp[j][i]=max(dp[j-k][i-1]+value[j],dp[j-1][i]); dp[j][i]:从 ...
- PKU--1976 A Mini Locomotive (01背包)
题目http://poj.org/problem?id=1976 分析:给n个数,求连续3段和的最大值. 这个题目的思考方式很像背包问题. dp[i][j]表示前i个数字,放在j段的最大值. 如果选了 ...
- POJ 1976 A Mini Locomotive【DP】
题意:给出一列火车,可以由三个火车头拉,每个火车头最多拉m节车厢(这m节车厢需要保持连续),再给出n节车厢,每节车厢的人数,问最多能够载多少人到终点. 可以转化为三个长度相等的区间去覆盖n个数,使得这 ...
- 线性dp——求01串最大连续个数不超过k的方案数,cf1027E 好题!
只写了和dp有关的..博客 https://www.cnblogs.com/huyufeifei/p/10351068.html 关于状态的继承和转移 这题的状态转移要分开两步来做: 1.继承之前状态 ...
- HDU2546(01背包饭卡)
电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大家 ...
- POJ 1976 A Mini Locomotive
$dp$. 要求选择$3$个区间,使得区间和最大.$dp[i][j]$表示前$i$个数中选择了$j$段获得的最大收益. #include <cstdio> #include <cma ...
- T1110-计算线段长度
原题链接: https://nanti.jisuanke.com/t/T1010 题目简述: 已知线段的两个端点的坐标A(Xa,Ya),B(Xb,Yb)A(X_a,Y_a),B(X_b,Y_b)A(X ...
随机推荐
- NSURLSession学习笔记
NSURLSession学习笔记(一)简介 一.URL Session的基本概念 1.三种工作模式: 默认会话模式(default):工作模式类似于原来的NSURLConnection,使用的是基于磁 ...
- swift为UIView添加extension扩展frame
添加swift file:UIView+Extension import UIKit extension UIView { // x var x : CGFloat { get { return fr ...
- 导航 tab
- 微软职位内部推荐-SW Engineer II for Skype
微软近期Open的职位: We are the Skype Beijing team. Skype division drives the communications strategy for Mi ...
- [shell]. 点的含义
. 的含义 当前目录 隐藏文件 任意一个字符 生效配置文件
- [android界面]android中src和background区别——前景与背景
ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸.src是图片内容(前 ...
- 5332盛照宗 如何获取新技能+c语言学习调查
如何获取新技能+c语言学习调查 你有什么技能比大多人(超过90%以上)更好? 如果问我有没有什么技能比大多数人,并且是90%的人好,我还真不敢说有,因为世界上有70亿人,要比63亿人做的好才行啊.我也 ...
- SpringMVC类型转换、数据绑定详解[附带源码分析]
目录 前言 属性编辑器介绍 重要接口和类介绍 部分类和接口测试 源码分析 编写自定义的属性编辑器 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那 ...
- 窥探算法之美妙——寻找数组中最小的K个数&python中巧用最大堆
原文发表在我的博客主页,转载请注明出处 前言 不论是小算法或者大系统,堆一直是某种场景下程序员比较亲睐的数据结构,而在python中,由于数据结构的极其灵活性,list,tuple, dict在很多情 ...
- 《1024伐木累》-te别篇,庭审你知道吗?
思前想后,我觉得不应该发这一期,因为,做完这一期之后突然发觉,自己失去了主题,到底是在讽刺?还是在讽刺?还是在讽刺呢?不论是什么,大家自己判断吧.就当作者不想发表自己的观点,先看这一期的对白吧! 1. ...