1 条题解

  • 0
    @ 2021-6-15 1:39:44

    C++ :

    #include <algorithm>
    #include <cstdio>
    using namespace std;
    
    const int M = 1000, N = 1000;
    int w[N][M], sum[N][M], cost[N], opt[M+1];
    int *fore[N], *rear[N], q[N][M+1];
    
    int main()
    {
        int n, m, p;
        scanf("%d%d%d", &n, &m, &p);
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                scanf("%d", &w[i][j]);
        for (int i = 0; i < n; ++i)
            scanf("%d", &cost[i]);
        for (int i = 0; i < n; ++i)
            for (int j = m; --j >= 0; )
                sum[i][j] = (j < m-1 ? sum[i][j+1] : 0) + w[(i+j)%n][j];
    
        for (int i = 0; i < n; ++i)
            fore[i] = q[i], rear[i] = q[i]+1, *fore[i] = m;
        opt[m] = 0;
        for (int i = m; --i >= 0; )
        {
            opt[i] = 0;
            for (int j = 0; j < n; ++j)
            {
                if (*fore[j] - i > p) ++fore[j];
                opt[i] = max(opt[i], opt[*fore[j]] + sum[j][i] - sum[j][*fore[j]] - cost[(i+j)%n]);
            }
            for (int j = 0; j < n; ++j)
            {
                while (fore[j] < rear[j] && opt[rear[j][-1]] - sum[j][rear[j][-1]] <= opt[i] - sum[j][i])
                    --rear[j];
                *rear[j]++ = i;
            }
        }
        printf("%d\n", opt[0]);
    }
    

    Pascal :

    var i,j,k,n,m,p,pastmax,nowmax:longint;
         coin,f,step:array[0..1000,0..1000] of longint;
         cost,past:array[1..1000] of longint;
    begin
    filldword(step,sizeof(step) shr 2,maxlongint);
    readln(n,m,p);
    for i:=2 to n do
       past[i]:=i-1;
    past[1]:=n;
    for i:=1 to n do
       for j:=1 to m do
        read(coin[j,i]);
    for i:=1 to n do read(cost[i]);
    pastmax:=0;
    for i:=1 to m do
       begin
        nowmax:=-maxlongint;
        for j:=1 to n do
         begin
          if step[i-1,past[j]]<p then
           begin
            if pastmax-cost[past[j]]>f[i-1,past[j]]
            then begin step[i,j]:=1;f[i,j]:=pastmax-cost[past[j]]+coin[i,past[j]];end
            else begin step[i,j]:=step[i-1,past[j]]+1;f[i,j]:=f[i-1,past[j]]+coin[i,past[j]];end;
           end
          else begin step[i,j]:=1;f[i,j]:=pastmax-cost[past[j]]+coin[i,past[j]];end;
          if f[i,j]>nowmax then nowmax:=f[i,j];
         end;
        pastmax:=nowmax;
       end;
    writeln(nowmax);
    end.
    
    • 1

    信息

    ID
    271
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者