[BJOI2019]排兵布阵

[BJOI2019]排兵布阵

题面

链接

题解

直接转化为分组背包

#include<bits/stdc++.h>

using namespace std;

inline int read()
{
    int f = 1 , x = 0;
    char ch;
    do
    {
        ch = getchar();
        if(ch=='-') f=-1;
    } while(ch<'0'||ch>'9');
    do
    {
        x=(x<<3) + (x<<1) + ch - '0';
        ch = getchar();
    }while(ch>='0'&&ch<='9');
    return f*x;
}

const int MAXN = 100 + 10;
const int MAXM = 20000 + 10;

int s,n,m;
struct node
{
    int v;
    int w;
};
vector<node>a[MAXN];
int dp[MAXN][MAXM];

inline bool cmp(node a,node b)
{
    return a.v<b.v;
}

int main()
{
    s = read(),n = read(),m = read();
    for(int j=1;j<=s;j++)
    {
        for(int i=1;i<=n;i++)
        {
            node x;
            x.v = read(),x.w=0;
            x.v=x.v*2+1;
            a[i].push_back(x);
        }
    }
    for(int i=1;i<=n;i++)
    {
        sort(a[i].begin(),a[i].end(),cmp);
        for(int j=1;j<=s;j++)
        {
            a[i][j-1].w = i*j;
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=m;j>=0;j--)
        {
            dp[i][j] = dp[i-1][j];
            for(int k=1;k<=s;k++)
            {
                if(j>=a[i][k-1].v)
                dp[i][j] = max(dp[i][j],dp[i-1][j-a[i][k-1].v]+a[i][k-1].w);        
            }
        }
    }
    cout<<dp[n][m]<<endl;
}

 

 

posted @ 2019-10-23 06:51  wlzs1432  阅读(130)  评论(0编辑  收藏  举报