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 ...
随机推荐
- 细说git merge & git rebase
git merge和git rebase两个都是用来合并两个分支用的,在使用过程中,这两个概念容易混淆. 在此,对这两个git技巧的用法进行详细描述,希望能帮助一些热爱git的朋友. -------- ...
- 了解了这些才能开始发挥jQuery的威力(转)
链接:http://www.cnblogs.com/dolphinX/archive/2013/10/08/3347677.html 由于当前jQuery如此的如雷贯耳,相信不用介绍什么是jQuery ...
- android中scrollTo和scrollBy的理解
protected int mScrollX; //该视图内容相当于视图起始坐标的偏移量 , X轴 方向 protected int mScrollY; //该视图内容相当 ...
- 给 IIS Express 配置虚拟目录
使用 vs2015 打开旧项目,之前使用 iis 配置站点,然后在 vs 中附加 w3wp.exe 进行开发和调试的. 由于种种原因 iis 上配置站点各种失败. 之后发现,其实在 vs2015 中按 ...
- CSS 实现加载动画之七-彩环旋转
今天整理的这个动画估计大家都不会陌生,彩环旋转,看过之后是不是觉得很熟悉,对,这个就是优酷视频APP里面的加载动画.本人空余时间喜欢看些视频,留意到这个动画后就想用代码实现出来,今天整理了下,跟大家分 ...
- C++系列: 如何将十六机制的字符串转成整数
bool convertHexStringToInt(char* pstrHex, unsigned long* pResult) { ) return false; else return true ...
- EL表达式 (详解)
L表达式 1.EL简介 1)语法结构 ${expression} 2)[]与.运算符 EL 提供.和[]两种运算符来存取数据. 当要存取的属性名称中包含一些 ...
- FlipView For Xamarin.Form 之 IOS
之前写过两篇博文 是关于 Android 和 Windows Phone 下的 FlipView 的实现. 上上周,有个印度佬通过 GitHub 找到我, 问我有没有打算个 ios 端的,还说比较了相 ...
- Android中的Intent详解
前言: 每个应用程序都有若干个Activity组成,每一个Activity都是一个应用程序与用户进行交互的窗口,呈现不同的交互界面.因为每一个Acticity的任务不一样,所以经常互在各个Activi ...
- python 读写文件和设置文件的字符编码
一. python打开文件代码如下: f = open("d:\test.txt", "w") 说明:第一个参数是文件名称,包括路径:第二个参数是打开的模式mo ...