1 条题解

  • 0
    @ 2023-10-1 13:55:58

    你没猜错,又是我

    #include <bits/stdc++.h>
    using namespace std;
     
    const int N = (int)30;
    const int M = (int)5e3;
    const int inf = 0x3f3f3f3f;
     
    int n, m;
    struct node
    {
        int f, s;
    }g[N + 5];
    int s[N + 5];
    int dp[N + 5][M + 5];
    int ans[N + 5];
     
    bool cmp(node a, node b)
    {
        return a.f > b.f;
    }
     
    void work()
    {
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d", &g[i].f);
            g[i].s = i;
        }
        sort(g + 1, g + n + 1, cmp);
        for(int i = 1; i <= n; ++i)
            s[i] = s[i - 1] + g[i].f;
        memset(dp, inf, sizeof(dp));
        dp[0][0] = 0;
        for(int i = 1; i <= n; ++i)
        {
            for(int j = 1; j <= m; ++j)
            {
                if(j < i)
                    continue;
                dp[i][j] = dp[i][j - i];
                for(int k = 1; k <= i; ++k)
                    dp[i][j] = min(dp[i][j], dp[i - k][j - k] + (s[i] - s[i - k]) * (i - k));
            }
        }
        printf("%d\n", dp[n][m]);
        int i = n, j = m, h = 0;
        while(i)
        {
            if(dp[i][j] == dp[i][j - i])
                j -= i, ++h;
            else
            {
                for(int k = 1; k <= i; ++k)
                {
                    if(dp[i][j] == dp[i - k][j - k] + (s[i] - s[i - k]) * (i - k))
                    {
                        for(int u = 0; u <= k; ++u)
                            ans[g[i - u].s] = h + 1;
                        i -= k, j -= k;
                        break;
                    }
                }
            }
        }
        for(int i = 1; i <= n; ++i)
            printf("%d%c", ans[i], i == n ? '\n' : ' ');
    }
     
    int main()
    {
        work();
        return 0;
    }
     
    
    • 1

    信息

    ID
    188
    时间
    1000ms
    内存
    128MiB
    难度
    6
    标签
    递交数
    32
    已通过
    12
    上传者