题目链接:

https://vjudge.net/problem/POJ-1976

题目描述:

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 i th 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

题意描述:
输入车厢的节数和每节车厢的人数以及每个火车头最多能拉几节车厢数
计算并输出三个火车头最多能拉多少人
解题思路:
参加代码中的注释。
代码实现:
#include<stdio.h>
#include<string.h>
const int N=;
int qmsum[N],s[N];
int f[N][];
int max(int x, int y){
return x > y ? x : y;
}
/*
问题 前i节车厢用j个火车头拉,最多能拉多少人,其中每个火车头拉m节连续的车厢
变量 i,j
状态 f[i][j]表示 前i节车厢用j个火车头拉,最多能拉多少人
每种状态面临两种取法,取以第i节车厢为首的m节车厢,不取以第i节车厢为首的m节车厢
状态转移方程为f[i][j]=max(f[i-m][j-1] + (sum[i]-sum[i-m]) , f[i-1][j]);
其中sum[i]-sum[i-m]为每段车厢的人数和
*/
int main()
{
int T,n,i,j,m;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&s[i]);
qmsum[i]=qmsum[i-]+s[i];//前i节车厢中有多少人
}
scanf("%d",&m); memset(f,,sizeof(f));
for(i=m;i<=n;i++)
for(j=;j>=;j--)
f[i][j]=max(f[i-][j] , f[i-m][j-] + (qmsum[i]-qmsum[i-m])); printf("%d\n",f[n][]);
}
return ;
}

A Mini Locomotive(01背包变型)的更多相关文章

  1. POJ1976A Mini Locomotive(01背包装+连续线段长度)

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

  2. HDU 2639 Bone Collector II(01背包变型)

    此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...

  3. POJ Washing Clothes 洗衣服 (01背包,微变型)

    题意:有多种颜色的衣服,由两个人合作来洗,必须洗完一种颜色才能洗下一种,求需要多少时间能洗完. 思路:将衣服按颜色分类,对每种颜色进行01背包,容量上限是该种颜色衣服全部洗完的耗时长一半,其实就是在最 ...

  4. poj 01背包

    首先我是按这篇文章来确定题目的. poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<algo ...

  5. POJ之01背包系列

    poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using na ...

  6. poj1837 01背包(雾

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

  7. UVALive 4870 Roller Coaster --01背包

    题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F ,     D -= K 问在D小于等于一定限度的时 ...

  8. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

  9. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

随机推荐

  1. activemq生产者和消费者的双向通信

    http://websystique.com/spring/spring-4-jms-activemq-example-with-jmslistener-enablejms/

  2. cxGrid 的 Sorting和Filtering的总开关

  3. CentOS6.5分区与文件系统

    1 分区介绍 inux分区不同于windows,linux下硬盘设备名为(IDE硬盘为hdx(x为从a—d)因为IDE硬盘最多四个,SCSI,SATA,USB硬盘为sdx(x为a—z)),硬盘主分区最 ...

  4. JavaScript中的工厂函数

    所谓工厂函数,就是指这些内建函数都是类对象,当你调用他们时,实际上是创建了一个类实例. 在学习jQuery的时候,我们经常会看到“工厂函数”这个概念,那么究竟什么是“工厂函数”呢?我们来看看概念,“所 ...

  5. fcitx、ibus、scim

    我觉得还是小企鹅fcitx好用点,兼容性好.速度快.配置简单. 在debian stable下,ibus用apt-get install完以后根本就不出现. scim倒是不用配置自己出来了,但是问题多 ...

  6. 迁移桌面程序到MS Store(4)——桌面程序调用Win10 API

    上一篇我们讨论了如何在转制的桌面程序中,通过StartupTask来实现转制版本的开机自启动.实际操作中,我们通过编辑Packaging工程中的Package.appxmanifest文件,来添加自启 ...

  7. OpenvSwitch端口镜像

    OVS上实现端口镜像的基本流程如下: 创建 mirror ,在 mirror 中指定镜像数据源及镜像目的地 将创建的 mirror 应用到 bridge 中 镜像数据源可以通过下面几个选项来指定: s ...

  8. Java并发编程总结2——慎用CAS

    一.CAS和synchronized适用场景 1.对于资源竞争较少的情况,使用synchronized同步锁进行线程阻塞和唤醒切换以及用户态内核态间的切换操作额外浪费消耗cpu资源:而CAS基于硬件实 ...

  9. graphite custom functions

    尊重作者的劳动,转载请注明作者及原文地址 http://www.cnblogs.com/txwsqk/p/6522854.html 参考 https://graphite.readthedocs.io ...

  10. mongoose 基础api 图表整理

    一.背景 今天看 mongoose 的基础 API,参考了下面的链接做了图表以供查阅. 参考资料: http://www.cnblogs.com/xiaohuochai/p/7215067.html ...