• poj1837 01背包(雾


    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 Output240






    在做poj01背包的时候搜到这一题,说实话,看不懂,想了很久,看题解一开始也没懂怎么dp的。。。
    基本是把大佬的啊题解照搬过来了,因为讲的很详细QAQ,也容易懂

    题目大意:

    有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。

    其中可以把天枰看做一个以x轴0点作为平衡点的横轴

    dp思路:

    每向天平中方一个重物,天平的状态就会改变,而这个状态可以由若干前一状态获得。

    首先定义一个平衡度j的概念

    当平衡度j=0时,说明天枰达到平衡,j>0,说明天枰倾向右边(x轴右半轴),j<0则相反

    那么此时可以把平衡度j看做为衡量当前天枰状态的一个值

    因此可以定义一个 状态数组dp[i][j],意为在挂满前i个钩码时,平衡度为j的挂法的数量

    由于距离c[i]的范围是-15~15,钩码重量的范围是1~25,钩码数量最大是20

    因此最极端的平衡度是所有物体都挂在最远端,因此平衡度最大值为j=15*20*25=7500。原则上就应该有dp[ 1~20 ][-7500 ~ 7500 ]。

    因此为了不让下标出现负数,做一个处理,使使得数组开为 dp[1~20][0~15000],则当j=7500时天枰为平衡状态(ps 关键)

    那么每次挂上一个钩码后,对平衡状态的影响因素就是每个钩码的 力臂

    力臂=重量 *臂长 = w[i]*c[k]

    那么若在挂上第i个砝码之前,天枰的平衡度为j

       (换言之把前i-1个钩码全部挂上天枰后,天枰的平衡度为j)

    则挂上第i个钩码后,即把前i个钩码全部挂上天枰后,天枰的平衡度 j=j+ w[i]*c[k]

       其中c[k]为天枰上钩子的位置,代表第i个钩码挂在不同位置会产生不同的平衡度

    就可以推出动态转移方程 dp[i][ j+ w[i]*c[k] ]= ∑(dp[i-1][j])

    //1496K  0MS 
     
     
    #include<iostream>
    using namespace std;
     
    int dp[21][15001]; //状态数组dp[i][j]
                           //放入(挂上)前i个物品(钩码)后,达到j状态的方法数
    int main(int i,int j,int k)
    {
        int n;  //挂钩数
        int g;  //钩码数
        int c[21];  //挂钩位置
        int w[21];  //钩码重量
     
        
        /*Input*/
     
        cin>>n>>g;
     
        for(i=1;i<=n;i++)
            cin>>c[i];
        for(i=1;i<=g;i++)
            cin>>w[i];
     
        /*Initial*/
     
        memset(dp,0,sizeof(dp));  //达到每个状态的方法数初始化为0
        dp[0][7500]=1;     //7500为天枰达到平衡状态时的平衡度
                           //放入前0个物品后,天枰达到平衡状态7500的方法有1个,就是不挂钩码
     
        /*DP*/
     
        for(i=1;i<=g;i++)
            for(j=0;j<=15000;j++)
                if(dp[i-1][j])  //优化,当放入i-1个物品时状态j已经出现且被统计过方法数,则直接使用统计结果
                                //否则忽略当前状态j
                    for(k=1;k<=n;k++)
                        dp[i][ j+w[i]*c[k] ] += dp[i-1][j]; //状态方程
        
        /*Output*/
     
        cout<<dp[g][7500]<<endl;
        return 0;
    }
     
  • 相关阅读:
    Python之语句与函数
    python语言的特别之处
    kafka消费者客户端
    kafka生产者客户端
    kafka技术分享02--------kafka入门
    kafka技术分享01--------why we study kafka?
    hadoop之hdfs及其工作原理
    hadoop之hdfs------------------FileSystem及其源码分析
    数据结构之红黑树(一)
    mysql中,唯一索引和普通索引应如何选择
  • 原文地址:https://www.cnblogs.com/xly1029/p/9336503.html
Copyright © 2020-2023  润新知