A Mini Locomotive
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 2485   Accepted: 1388

Description

A train has a locomotive that pulls the train with its many passenger coaches. If the locomotive breaks down, there is no way to pull the train. Therefore, the office of railroads decided to distribute three mini locomotives to each station. A mini locomotive can pull only a few passenger coaches. If a locomotive breaks down, three mini locomotives cannot pull all passenger coaches. So, the office of railroads made a decision as follows:

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

The first line of the input contains a single integer
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

There should be one line per test case, containing the
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背包装+连续线段长度)的更多相关文章

  1. A Mini Locomotive(01背包变型)

    题目链接: https://vjudge.net/problem/POJ-1976 题目描述: A train has a locomotive that pulls the train with i ...

  2. POJ-1976-A Mini Locomotive-dp

    A train has a locomotive that pulls the train with its many passenger coaches. If the locomotive bre ...

  3. A Mini Locomotive(动态规划 01)

     /*  题意:选出3个连续的 数的个数  为K的区间,使他们的和最大 分析: dp[j][i]=max(dp[j-k][i-1]+value[j],dp[j-1][i]);   dp[j][i]:从 ...

  4. PKU--1976 A Mini Locomotive (01背包)

    题目http://poj.org/problem?id=1976 分析:给n个数,求连续3段和的最大值. 这个题目的思考方式很像背包问题. dp[i][j]表示前i个数字,放在j段的最大值. 如果选了 ...

  5. POJ 1976 A Mini Locomotive【DP】

    题意:给出一列火车,可以由三个火车头拉,每个火车头最多拉m节车厢(这m节车厢需要保持连续),再给出n节车厢,每节车厢的人数,问最多能够载多少人到终点. 可以转化为三个长度相等的区间去覆盖n个数,使得这 ...

  6. 线性dp——求01串最大连续个数不超过k的方案数,cf1027E 好题!

    只写了和dp有关的..博客 https://www.cnblogs.com/huyufeifei/p/10351068.html 关于状态的继承和转移 这题的状态转移要分开两步来做: 1.继承之前状态 ...

  7. HDU2546(01背包饭卡)

    电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大家 ...

  8. POJ 1976 A Mini Locomotive

    $dp$. 要求选择$3$个区间,使得区间和最大.$dp[i][j]$表示前$i$个数中选择了$j$段获得的最大收益. #include <cstdio> #include <cma ...

  9. 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 ...

随机推荐

  1. Linux下检测IP地址冲突及解决方法

    问题说明:在公司办公网内的一台物理机A上安装了linux系统(ip:192.168.9.120),在上面部署了jenkins,redmine,svn程序.由于是在办公网内,这台机器和同事电脑都是在同一 ...

  2. poj3371

    Flesch Reading Ease Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2269 Accepted: 710 De ...

  3. 双绞线线序+POE供电网线

    0 重点 一般情况下会用1236(橙白.橙.绿白.绿)传输数据,1.2用于发送,3.6用于接收,45(蓝.蓝白)电源正极 78(棕白.棕)电源负极. 一 网线线序 12发 36收 二 poe网线供电 ...

  4. Web API路由和动作选择

    前言 本文描述ASP.NET Web API如何把一个HTTP请求路由到控制器的一个特定的Action上.关于路由的总体概述可以参见上一篇教程 http://www.cnblogs.com/aehyo ...

  5. java.sql.SQLException: 对只转发结果集的无效操作: last

    出错代码如下:static String u = "user";static String p = "psw";static String url = &quo ...

  6. chrome浏览器插件启动本地应用程序

    chrome浏览器插件启动本地应用程序 2014-04-20 00:04:30|  分类: 浏览器插件|举报|字号 订阅     下载LOFTER我的照片书  |     chrome的插件开发这里就 ...

  7. WIN8 浏览器排版不兼容问题

    经常访问网站失败 访问部分网站只显示白底蓝字没有排版也没有图片 尝试了网上的各种方法,也是用了360和百度安全管家的网页修复 重置了IE设置 都不行!!! 最后有效的方法是 更换DNS 208.67. ...

  8. 在MacBook Air 上装Win10的,反反复复的失败过程。

    这个月初,一个女性朋友托我帮她装电脑,往MacBook Air上面装Windows 系统,原因是windows用的习惯,用起来顺手.然后用脚趾头考虑了一下,就一口答应下来了.难道这就是一个标准程序员的 ...

  9. shell编程之基础

    1.初探shell #!/bin/bash echo -e 'hello \nworld' 执行结果 echo 是打印输出字符,-e选项是开启转义功能 注意: 在windows下编写的shell脚本直 ...

  10. javascript基础(1)

    1.前言 ECMAscript解释,用来解释JS代码 DOM 文档对象模型,浏览器在做显示时需要渲染DOM树 BOM  浏览器对象模型,可以控制浏览器的行为,代码的兼容性很差 2.基本类型 数字类型: ...