zoukankan      html  css  js  c++  java
  • codeforces 148E Aragorn's Story 背包DP

    Aragorn's Story

    Time Limit: 20 Sec  Memory Limit: 256 MB

    题目连接

    http://codeforces.com/problemset/problem/148/E

    Description

    Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time. A的某一段完全重合,或者能够经过上下左右平移与折线A的某一段完全重合,则表示秋实大哥吹出了妹子的一部分旋律。

    Input

    The first line of input data contains two integers n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 10000). The next n lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer between 1 and 100, inclusive), followed by the values of the items (integers between 1 and 100, inclusive), in the order in which they appear on the shelf (the first number corresponds to the leftmost item, the last one — to the rightmost one). The total number of items is guaranteed to be at least m.

    Output

    Output the maximal total value of a tantrum of m shrieks.

    Sample Input

    2 3
    3 3 7 2
    3 4 1 5

    Sample Output

    15

    HINT

    题意

    有n排花盆,每排有k个,然后有个人想扔m个花瓶,每个花瓶有个价值val

    他只能选择每一排的最左边或者最右边扔

    求扔的最大价值

    题解:

    背包问题,bag[i][j]表示第i排扔j个的最大值

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    int bag[1000][1000];
    int t[1000];
    int a[1000][1000];
    int dp[105][10001];
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&t[i]);
            for(int j=1;j<=t[i];j++)
            {
                scanf("%d",&a[i][j]);
                a[i][j]+=a[i][j-1];
            }
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=t[i];j++)
                for(int k=0;k<=j;k++)
                    bag[i][j]=max(bag[i][j],a[i][k]+a[i][t[i]]-a[i][t[i]-j+k]);
        for(int i=1;i<=n;i++)
        {
            for(int j=m;j>=0;j--)
            {
                for(int k=0;k<=t[i]&&k<=j;k++)
                {
                    dp[i][j]=max(dp[i][j],dp[i-1][j-k]+bag[i][k]);
                }
            }
        }
        cout<<dp[n][m]<<endl;
    
    }
  • 相关阅读:
    HDU2034:人见人爱A-B
    Codeup1085: 阶乘的和
    HDU2037:今年暑假不AC
    剑指Offer(Java版)第二十七题:从上往下打印出二叉树的每个节点,同层节点从左至右打印。
    剑指Offer(Java版)第二十六题:输入两个整数序列,第一个序列表示栈的压入顺序, 请判断第二个序列是否为该栈的弹出顺序。 假设压入栈的所有数字均不相等。 例如序列1、2、3、4、5是某栈的压栈序列, 序列5、4、3、2、1是该压栈序列对应的一个弹出序列, 但4、3、5、1、2就不可能是该压栈序列的弹出序列。
    剑指Offer(Java版)第二十五题:包含min函数的栈
    剑指Offer(Java版)第二十四题:顺时针打印矩阵
    MongoDB学习笔记10——分片
    MongoDB学习笔记9——复制
    MongoDB学习笔记8——优化
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4498745.html
Copyright © 2011-2022 走看看