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

时间:2022-09-25 22:37:19
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. MS SQL验证字符串是否包含有大小写字母

    昨晚有实现一个小功能,就是在MS SQL Server中,检查字符串是否包含有大小写字母.通常应用在字符串的复杂度. ) = N'SDFfgGRYJhhTYUJ' IF LOWER(@s) COLLA ...

  2. php5&period;3到php7&period;0&period;x新特性介绍

    <?php /*php5.3*/ echo '<hr>'; const MYTT = 'aaa'; #print_r(get_defined_constants()); /* 5.4 ...

  3. tomcat实现ServletContext的addListener方法的源码解说&lpar;原创&rpar;

    tomcat 8.0.36 知识点: 动态监听器有七类: ServletContextAttributeListener ServletRequestListener ServletRequestAt ...

  4. VB&period;NET入门基础

    众所周知,Visual Basic.NET是由Visual Basic发展而来,这两者之间的升级使得Visual Basic语言发生了革命性的变革,使得由基于对象编程的Visual Basic过渡到了 ...

  5. eclipse 设置 默认编码为 utf-8

    学习javaweb时,开发工具都采用utf-8的编码方式,给eclipse设置默认编码为utf-8的编码方法 菜单 Window -> preference -> General -&gt ...

  6. mysql-8&period;0&period;11-winx64 免安装版配置方法

    mysql-8.0.11-winx64.zip  下载地址:https://dev.mysql.com/downloads/file/?id=476233 mysql-8.0.11-winx64.zi ...

  7. apache 图片防盗链

    RewriteEngine on RewriteCond %{HTTP_REFERER} !ot.com [NC] RewriteCond %{HTTP_REFERER} !baidu.com [NC ...

  8. HDU - 4407 Sum &lpar;容斥&rpar;

    题意:初始序列[1..N](1<=N<=4e5),支持两种操作:1.求区间[x,y]内与p互素的数之和: 2.将x位置的数变为c. 分析:很容易把人骗到线段树的思维中,而实际上操作2单点的 ...

  9. QMesageBox的使用

    一.使用构造函数弹出对话框 1. QMessageBox msgBox://最简单的对话框,里面什么也没有 QString str = “test”: msgBox.setText(str); msg ...

  10. Java项目性能瓶颈分析及定位(八)——Java线程堆栈分析(五)

    对于CPU而言,常见的瓶颈主要有两种:服务器的压力很小,但是CPU的利用率却很高,这样的性能瓶颈相对比较容易定位(好比我只是说了你一句,你就哭了,你的弱点立马就暴露出来了):给服务器施加的压力很大,但 ...