• 2283: A Mini Locomotive(01背包)


    2283: A Mini Locomotive 分享至QQ空间

    时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte
    总提交: 45            测试通过:25

    描述

     

    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.

    输入

     

    The first line of the input file 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.

    输出

     

    There should be one line per test case, containing the maximum number of passengers which can be transported by the three mini locomotives.

    样例输入

     

    1
    7
    35 40 50 10 30 45 60
    2

    样例输出

     240

    题目来源

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int maxn=5e4+5;
     8 int dp[maxn][4];    //前i辆客车,用了几辆小拖拉机的最大运输量
     9 int arr[maxn],sum[maxn];
    10 int t,n,m;
    11 
    12 int main(){
    13     ios::sync_with_stdio(false);
    14     cin>>t;
    15     while(t--){
    16         cin>>n;
    17         for(int i=1;i<=n;i++){
    18             cin>>arr[i];
    19             sum[i]=sum[i-1]+arr[i];
    20         }
    21         cin>>m;
    22         for(int i=m;i<=n;i++){
    23             for(int j=1;j<=3;j++){
    24                 dp[i][j]=max(dp[i-1][j],dp[i-m][j-1]+sum[i]-sum[i-m]);
    25             }
    26         }
    27         cout << dp[n][3] << endl;
    28     }
    29     return 0;
    30 }
    View Code
  • 相关阅读:
    《Effective C++》笔记
    《C++ Qt 编程视频教程》
    Windows下802.11抓包
    springcloud01_ribbon使用及原理
    springboot04_springboot特性之Actuator
    springboot04_手写实现starter实现
    linux操作01_redis服务器安装
    springboot03_核心特性及设计思想
    springboot重新认识
    springboot01_微服务架构的现状及未来【上】
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11704362.html
Copyright © 2020-2023  润新知