A Mini Locomotive(01背包变型)
题目链接:
https://vjudge.net/problem/POJ-1976
题目描述:
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 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
Sample Input
1
7
35 40 50 10 30 45 60
2
Sample Output
240 题意描述:
输入车厢的节数和每节车厢的人数以及每个火车头最多能拉几节车厢数
计算并输出三个火车头最多能拉多少人
解题思路:
参加代码中的注释。
代码实现:
#include<stdio.h>
#include<string.h>
const int N=;
int qmsum[N],s[N];
int f[N][];
int max(int x, int y){
return x > y ? x : y;
}
/*
问题 前i节车厢用j个火车头拉,最多能拉多少人,其中每个火车头拉m节连续的车厢
变量 i,j
状态 f[i][j]表示 前i节车厢用j个火车头拉,最多能拉多少人
每种状态面临两种取法,取以第i节车厢为首的m节车厢,不取以第i节车厢为首的m节车厢
状态转移方程为f[i][j]=max(f[i-m][j-1] + (sum[i]-sum[i-m]) , f[i-1][j]);
其中sum[i]-sum[i-m]为每段车厢的人数和
*/
int main()
{
int T,n,i,j,m;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&s[i]);
qmsum[i]=qmsum[i-]+s[i];//前i节车厢中有多少人
}
scanf("%d",&m); memset(f,,sizeof(f));
for(i=m;i<=n;i++)
for(j=;j>=;j--)
f[i][j]=max(f[i-][j] , f[i-m][j-] + (qmsum[i]-qmsum[i-m])); printf("%d\n",f[n][]);
}
return ;
}
A Mini Locomotive(01背包变型)的更多相关文章
- POJ1976A Mini Locomotive(01背包装+连续线段长度)
A Mini Locomotive Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2485 Accepted: 1388 ...
- HDU 2639 Bone Collector II(01背包变型)
此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...
- POJ Washing Clothes 洗衣服 (01背包,微变型)
题意:有多种颜色的衣服,由两个人合作来洗,必须洗完一种颜色才能洗下一种,求需要多少时间能洗完. 思路:将衣服按颜色分类,对每种颜色进行01背包,容量上限是该种颜色衣服全部洗完的耗时长一半,其实就是在最 ...
- poj 01背包
首先我是按这篇文章来确定题目的. poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<algo ...
- POJ之01背包系列
poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using na ...
- poj1837 01背包(雾
Description A train has a locomotive that pulls the train with its many passenger coaches. If the lo ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
随机推荐
- python之基础1
一.python介绍 介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,Guido开始写能够解释Python语言语法的解释器.Python这个名字 ...
- CSS 基础 例子 水平 & 垂直对齐
一.元素居中对齐 margin:auto 水平居中对齐一个元素(如 <div>),即div本身在容器中的对齐,用margin:auto,而且,需要设置 width 属性(或者设置 100% ...
- gcc 6.0编译opencv出错
在编译opencv3.2时候,出现下面错误: cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_NE ...
- HTTP协议(一)基本概念、HTTP方法、HTTP状态码
1.HTTP是什么? 它的全名叫:HyperText Transfer Protocol ,中国名字:超文本传输协议 : 它是可靠的数据传输协议,是Internet 的多媒体信使,即使数据相隔天涯海角 ...
- 使用WPF制作视频监控多画面切换
前言 曾有做过一个产品,有一个功能是视频监控模块,视频监控首先想到的是视频多画面切换功能,由于前端是用WPF开发的,所以当时就做了一个多画面切换组件,效果如下: 功能设计前提: 由于要使用海康大华天地 ...
- 如何将Skyline66嵌入WPF中
1.新建WPF项目: 2.添加引用 .net引用:System.Windows.Forms和WindowsFormsIntegration skyline引用:AxInterop.TerraExplo ...
- 阿里云oss服务通用类
在webconfig中配置信息 <?xml version="1.0" encoding="utf-8"?><configuration> ...
- MariaDB 主从同步与热备(14)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...
- 使用Samba实现文件共享
1987年,微软公司和英特尔公司,共同制定了SMB(Server Messages Block 服务消息块)协议,指在解决局域网内的文件或打印机等资源的共享问题,这也使得在多个主机之间共享文件变得越来 ...
- window.location API
概述 今天被自己鄙视了,竟然不会用window.location.search进行页面传值.现在好好总结下window.location API,记录一下供以后开发时参考,相信对其它人也有用. 页面传 ...