QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#318621#6955. Assignmentluyiming123AC ✓91ms5848kbC++233.0kb2024-01-31 16:15:172024-01-31 16:15:18

Judging History

你现在查看的是最新测评结果

  • [2024-01-31 16:15:18]
  • 评测
  • 测评结果:AC
  • 用时:91ms
  • 内存:5848kb
  • [2024-01-31 16:15:17]
  • 提交

answer

# include <bits/stdc++.h>
# define int long long
using namespace std;
const int N = 105; 
int n, K, A[N][N], dp[N][N][12], mn[N][N][12], a[N], b[N], f[N][12]; 
void chkmin(int &x, int y) { x = min(x, y); return; }
signed main(void)
{
    int Test; scanf("%lld", &Test); 
    while(Test--)
    {
        scanf("%lld%lld", &n, &K); 
        for(int i = 1; i <= n; i++) scanf("%lld", &a[i]); 
        for(int i = 1; i <= n; i++) scanf("%lld", &b[i]); 
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++) scanf("%lld", &A[i][j]); 
        for(int l = 0; l <= n + 1; l++)
        {
            for(int r = 0; r <= n + 1; r++)
            {
                for(int d = 0; d <= K; d++) 
                {
                    dp[l][r][d] = mn[l][r][d] = INT_MAX; 
                }
            }
        }
        for(int i = 1; i <= n; i++) 
        {
            dp[i][i][0] = A[b[i]][1]; mn[i][i][0] = 0; 
            for(int j = 1; j <= K; j++) dp[i][i][j] = 0, mn[i][i][j] = 0; 
        }
        
        for(int len = 2; len <= n; len++)
        {
            for(int l = 1, r = len; r <= n; l++, r++)
            {
                for(int i = (b[l] == b[r] ? 0 : 1); i <= min(len, K); i++) 
                    mn[l][r][i] = mn[l][r - 1][i - (b[l] != b[r])]; 
                for(int k = len; k <= K; k++) dp[l][r][k] = mn[l][r][k] = 0; 
                for(int p = l; p < r; p++) 
                {
                    for(int k = 0; k <= min(len, K); k++)
                    {
                        for(int x = 0; x <= k; x++)
                        {
                            chkmin(dp[l][r][k], dp[l][p][x] + dp[p + 1][r][k - x]); 
                            chkmin(mn[l][r][k], mn[l][p][x] + dp[p + 1][r][k - x]); 
                        }
                    }
                }
                // for(int k = 1; k <= K; k++) chkmin(mn[l][r][k], mn[l][r][k - 1]); 
                for(int k = 0; k <= K; k++) 
                {
                    chkmin(dp[l][r][k], mn[l][r][k] + A[b[l]][len]);
                    // if(k) chkmin(dp[l][r][k], dp[l][r][k - 1]); 
                }
            }
        }

        for(int i = 0; i <= n + 1; i++)
            for(int j = 0; j <= K; j++) 
                f[i][j] = i == 0 ? 0 : INT_MAX; 
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j <= min(i, K); j++) 
            {
                // no 
                if(a[i] == b[i]) f[i][j] = f[i - 1][j]; 
                else if(j > 0) f[i][j] = f[i - 1][j - 1]; 
                for(int p = 0; p < i; p++) 
                {
                    for(int x = 0; x <= j; x++) 
                    {
                        chkmin(f[i][j], f[p][x] + dp[p + 1][i][j - x]); 
                    }
                }
                // if(j) chkmin(f[i][j], f[i][j - 1]); 
            }
        }
        for(int i = 0; i <= K; i++) printf("%lld%c", f[n][i], " \n"[i == K]); 
    }
    return 0; 
}

详细

Test #1:

score: 100
Accepted
time: 91ms
memory: 5848kb

input:

10
100 10
24 56 71 49 70 39 19 23 96 26 85 33 73 84 35 75 58 77 85 40 33 36 75 55 7 28 37 7 100 94 35 64 68 46 77 80 90 28 85 8 9 23 32 45 4 51 47 82 6 49 45 55 28 69 80 7 61 41 83 42 16 25 82 56 26 92 14 66 43 1 2 62 25 84 19 58 35 37 14 61 38 12 27 95 98 100 61 36 75 83 35 19 74 56 87 10 10 6 60 5...

output:

27473279 26539877 25650181 24801901 24025447 23252605 22479763 21733001 20960159 20221324 19493165
32063854 31138381 30266331 29465439 28683429 27901842 27157669 26414311 25690509 25035718 24380927
3973096 3343250 2761180 2428393 2215777 2075964 1991108 1914392 1829536 1823074 1749700
5341649 453916...

result:

ok 10 lines