A Mini Locomotive
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:
- 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.
- 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.
- 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
题意描述:
给出车厢数量、每个车厢的人数以及一个车头最多能拉的车厢数,所拉车厢必须是连续的,现有三个车头,问最多能拉多少人。
解题思路:
将问题分为两部分。第一部分,3个车头所拉的总车厢数大于现有车厢数,此时可全部拉完,直接输出人数之和即可。第二部分,3个车头所拉的总车厢数小于现有车厢数,假如每个车头可以拉的最大车厢数为m,则将每m个车厢合并成一个车厢,此时问题转化为01背包问题。
AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long dp[50000][4],w[100000],v[100000];
int main(void)
{
int i,j,k,n,m,sum=0,x,y;
scanf("%d",&k);
while(k--)
{
memset(dp,0,sizeof(dp));
memset(w,0,sizeof(w));
scanf("%d",&n);
sum=0;
x=1;
for(i=1;i<=n;i++)
{
scanf("%lld",&v[i]);
sum+=v[i];//统计一共有多少人
}
scanf("%d",&m);
for(i=1;i<=n;i++)//将每m个车厢合并成一个新车厢
{
if(i+m>n+1)
break;
for(j=i;j<i+m;j++)
{
w[x]+=v[j];
}
x++;//统计合并后的车厢个数
}
x-=1;
if(n<=m*3)//车头能拉的最大车厢数大于现有车厢数
printf("%d\n",sum);
else
{
for(i=1;i<=x;i++)//01背包找出3个火车头能拉的最大人数
{
for(j=3;j>=1;j--)
{
if(i-m<0)
y=0;
else
y=i-m;
dp[i][j]=max(dp[i-1][j],dp[y][j-1]+w[i]);
}
}
printf("%lld\n",dp[x][3]);
}
}
return 0;
}
本文探讨了一种火车调度算法,旨在通过三个小型火车头最优化运输乘客数量。算法首先判断是否所有车厢都能被拉动,若否,则采用01背包问题解决策略,通过合并车厢并计算最大运输能力。
498

被折叠的 条评论
为什么被折叠?



