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

题意:

某个车站有N个火车车厢,编号为1~N,每个车厢上有x个人。

这个车站还有三个火车头,他们能拉最多m个车厢(m<=N/3),而且这m个车厢的编号要连续的。问这三个火车头最多能拉多少个人。

参考博客:

https://blog.csdn.net/cttacm/article/details/45565447

我的代码:

 #include<stdio.h>
#include<iostream>
#include<cmath>
#include<string.h>
#include<iomanip>
using namespace std;
#define inf 0x3f3f3f3f int a[];
int sum[];
int dp[][];
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int tt;
cin>>tt;
while(tt--)
{
memset(a,,sizeof(a));
memset(sum,,sizeof(sum));
memset(dp,,sizeof(dp));
int n;
cin>>n;
for(int i=; i<=n; i++)
{
cin>>a[i];
sum[i]=sum[i-]+a[i];
}
int k;
cin>>k;
//int maxx=-inf;
for(int i=k; i<=n; i++)
{
for(int j=; j>=; j--)
{
dp[i][j]=max(dp[i-][j],dp[i-k][j-]+(sum[i]-sum[i-k]));
}
// maxx=max(dp[i],maxx);
}
cout<<dp[n][]<<endl;
}
return ;
}

POJ-1976-A Mini Locomotive-dp的更多相关文章

  1. POJ 1976 A Mini Locomotive【DP】

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

  2. POJ 1976 A Mini Locomotive

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

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

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

  4. POJ 3280 - Cheapest Palindrome - [区间DP]

    题目链接:http://poj.org/problem?id=3280 Time Limit: 2000MS Memory Limit: 65536K Description Keeping trac ...

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

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

  6. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  7. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

  8. poj 1185(状态压缩DP)

    poj  1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...

  9. poj 3254(状态压缩DP)

    poj  3254(状态压缩DP) 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相 ...

  10. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

随机推荐

  1. BCZM : 1.9

    有n个学生参加见面会,分别对m个研究组中的若干个感兴趣,为了满足所有学生的要求,每个学生都能参加自己感兴趣的见面会,如果每个见面会的时间为t,如何安排才能使得所有见面会的总时间最短? 分析: 先建立模 ...

  2. Spring - @ManagedResource, @ManagedOperation, @ManagedAttribute

    总结 通过annotation (@ManagedResource, @ManagedOperation, @ManagedAttribute)注解注册MBean到JMX实现监控java运行状态 参考 ...

  3. lua数据类型与变量

    Lua数据类型与变量 Lua中有  8个基本类型分别为: nil.boolean.number.string.userdata.function.thread和 table. lua变量三种类型:全局 ...

  4. pytorch实现kaggle猫狗识别

    参考:https://blog.csdn.net/weixin_37813036/article/details/90718310 kaggle是一个为开发商和数据科学家提供举办机器学习竞赛.托管数据 ...

  5. NX二次开发-UDO用户自定义对象(UFUN)【持续完善】

    每当提起UDO总是会让我想起大专毕业那会失业找工作,后来有个宝贵机会去了软件公司上班,拿到了我人生中的第一个NX二次开发项目,一个关于测量汽车前后左右摄像头的项目.当时那个项目就用到了UDO,对于只看 ...

  6. NX二次开发-如何在类外面定义一个结构体

    #include <uf.h> #include <uf_obj.h> #include <uf_part.h> using namespace NXOpen; u ...

  7. 其它课程中的python---1、python基础

    其它课程中的python---1.python基础 一.总结 一句话总结: 可以先把视频平台搭起来,这样学习效率会高很多,而且有额外收益 1.python的优势有哪些? 一个广泛的标准库 扩展性:比如 ...

  8. bootstrap中container和container-fluid的区别

    container和container-fluid 在bootstrap中,两者都是设置文本居中,但是它们还是有很大差别的 container 是随屏幕宽度的变化而变化的,是阶段性变化,有一个随浏览器 ...

  9. sql中desc的妙用

    很多情况下对表内部结构不熟悉,想看下我创建的表里面又多少表明,又想他按照首字母的规律排列, 这时候就要用desc +表名了. desc t_bike_info; hive> desc t_bik ...

  10. 高手总结CSS书写技巧

    这个时候我们就需要针对不同的浏览器去写不同的CSS,让它能够同时兼容不同的浏览器,能在不同的浏览器中也能得到我们想要的页面效果.这个针对不同的浏览器写不同的CSS code的过程,就叫CSS hack ...