A Mini Locomotive

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

 

火车有一个火车头,它和许多客车一起牵引火车。如果火车头坏了,就没有办法拉火车了。因此,铁路局决定在每个车站配置三辆小型机车。小型机车只能拉几节客车。如果一辆机车发生故障,三辆小型机车就不能牵引所有的客车。因此,铁路局做出如下决定:

1.设置小机车能牵引的最大客车数,小机车不能牵引该数。这三辆机车的车号都一样。
2.第二步。有了三辆小型机车,他们就可以最大限度地运送乘客到目的地。办公室已经知道了每辆客车的乘客人数,并且不允许乘客在车厢之间移动。
三。每辆小型机车都会连续牵引客车。在机车之后,客车的车号从1开始。

例如,假设有7辆客车,一辆小型机车最多可牵引2辆客车。客车中乘客的数量从1到7依次为35、40、50、10、30、45和60。

如果三辆小型机车牵引客车1-2、3-4和6-7,可以运送240名乘客。在这个例子中,三辆小型机车不能运送超过240名乘客。

根据客车的数量、每节客车的乘客数量以及小机车能牵引的最大客车数量,编写程序,找出三辆小机车能运输的最大乘客数量。
输入
输入的第一行包含一个整数t(1<=t<=11),即测试用例数,然后是每个测试用例的输入数据。每个测试用例的输入如下:
输入文件的第一行包含客车数量,不超过50000辆。第二行包含一个以空格分隔的整数列表,给出每节车厢的乘客数,这样这行的第i个乘客数就是第一节车厢的乘客数。没有一节车厢能容纳超过100名乘客。第三条线路包含单个小型机车可牵引的最大客车数量。这个数字不超过客车数量的1/3。

 

上面是全文翻译,这一题重在理解,只要理解了,就好做了。

题意的重点是,有三辆小火车,每辆车能拉的车厢由输入给出,问这三辆小火车最多能拉多少人。

(车厢必须按顺序拉,如果一辆小火车能拉2节车厢,不能是1,3之类的,只能是1,2之类的,这就限定了不能排序)

先将车厢按节累加起来    sum[i]=sum[i-1]+a[i];

这是这一题的递推方程。j是小火车的个数,i是车厢的个数,m是每节小火车能拉的个数(能拉完,就拉完,这样小火车拉的人才更多)。在遍历一个车厢的时候,要考虑,要不要它,

这就是  dp[i-1][j],不要它的结果;dp[i-m][j-1]+sum[i]-sum[i-m]是加上它的结果,dp[i-m][j-1]是空出一辆小火车,和不遍历i后m节车厢,这要和第i节车厢连在一起的 。因为只能连着拉,所以是sum[i]-sum[i-m],这就是为什么要累加sum的原因,因为m的值不确定,单个的车厢不好数。

dp[i][j]=max(dp[i-1][j],dp[i-m][j-1]+sum[i]-sum[i-m]);

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int sum[50020];
int dp[50020][4],a[50020];
int main()
{
	int n,k,m,t;
	scanf("%d",&t);
	while(t--)
	{
		memset(sum,0,sizeof(sum));
		memset(dp,0,sizeof(dp));
		scanf("%d",&k);
		for(int i=1;i<=k;i++)
		{
			scanf("%d",&a[i]);
			sum[i]=sum[i-1]+a[i];
		}
		scanf("%d",&m);
		for(int i=m;i<=k;i++)//每节车厢都要考虑 
			for(int j=1;j<=3;j++)//因为有3辆小火车
			{
				dp[i][j]=max(dp[i-1][j],dp[i-m][j-1]+sum[i]-sum[i-m]);
			}
			printf("%d\n",dp[k][3]);
	}
	return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值