POJ 1976 A Mini Locomotive(01背包)

本文探讨了一种火车调度算法,旨在通过三个小型火车头最优化运输乘客数量。算法首先判断是否所有车厢都能被拉动,若否,则采用01背包问题解决策略,通过合并车厢并计算最大运输能力。

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:

  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

题意描述:
给出车厢数量、每个车厢的人数以及一个车头最多能拉的车厢数,所拉车厢必须是连续的,现有三个车头,问最多能拉多少人。

解题思路:
将问题分为两部分。第一部分,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;
}
基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值