1 条题解

  • 0
    @ 2021-6-14 23:29:30

    C :

    #include <stdio.h>
    
    int sum[51][51];
    int a[51];
    int N,M,g;
    struct node
    {
        int min,max;
    }f[10][51];
    
    int Init()
    {
        int i;
        scanf("%d%d",&N,&M);
        for (i=1;i<=N;++i)
        {
            scanf("%d",&a[i]);
            a[i]=(a[i]+200000)%10;
        }
    }
    void Work()
    {
        int k,i,j;
        for (i=1;i<=N;++i)
        {
            sum[i][i]=a[i];
            for (j=i;j<=N;++j) sum[i][j]=(sum[i][j-1]+a[j])%10;
            f[1][i].min=f[1][i].max=sum[1][i];
        }
        for (k=2;k<=M;++k)
            for (i=k;i<=N;++i)
            {
                f[k][i].min=g;
                f[k][i].max=0;
                for (j=k-1;j<i;++j)
                {
                    if (f[k-1][j].min*sum[j+1][i]<f[k][i].min)
                        f[k][i].min=f[k-1][j].min*sum[j+1][i];
                    if (f[k-1][j].max*sum[j+1][i]>f[k][i].max) 
                        f[k][i].max=f[k-1][j].max*sum[j+1][i];
                }
            }
        for (i=0;i<N;++i) a[i]=a[i+1];
        a[N]=a[0];
    }
    int main()
    {
        int i,min,max;
        Init();
        g=(1<<31)-1;
        min=g;
        max=0;
        for (i=0;i<N;++i)
        {
            Work();
            if (min>f[M][N].min) min=f[M][N].min;
            if (max<f[M][N].max) max=f[M][N].max;
        }
        printf("%d\n%d",min,max);
        return 0;
    }
    
    

    C++ :

    #include <stdio.h> 
    #include <string.h>  
    #define MAX 10000000  
    #define MIN -10000000  
    int line[200],n,m,Min,Max,sum[200];  
    int f[200][20];  
    int g[200][20]; 
    int min(int a,int b)  
    {  
        if(a>b)  
            return b;  
        return a;  
    }  
    int max(int a,int b)  
    {  
        if(a>b)  
            return a;  
        return b;  
    }  
    void dp(int a[])  
    {  
        int i,j,k;  
        for(i=1;i<=n;i++)  
            sum[i]=sum[i-1]+a[i];  
        for(i=0;i<=n;i++)  
            for(j=0;j<=m;j++)  
            {  
                f[i][j]=0;  
                g[i][j]=-1u>>1;  
            }  
    		for(i=1;i<=n;i++)  
    		{  
    			f[i][1]=g[i][1]=(sum[i]%10+10)%10;  
    		}  
    		f[0][0]=1;  
    		g[0][0]=1;  
    		for(j=2;j<=m;j++)  
    		{  
    			for(i=j;i<=n;i++)  
    			{  
    				for(k=j-1;k<i;k++)  
    				{  
    					{  
    						f[i][j]=max(f[i][j],f[k][j-1]*(((sum[i]-sum[k])%10+10)%10));  
    						g[i][j]=min(g[i][j],g[k][j-1]*(((sum[i]-sum[k])%10+10)%10));  
    					}  
    				}  
    			}  
    		}  
    		Max=max(Max,f[n][m]);  
    		Min=min(Min,g[n][m]);  
    }  
    int main()  
    {  
        int i,j,k;  
        Max=0;  
        Min=-1u>>1;  
        scanf("%d%d",&n,&m);  
        for(i=1;i<=n;i++)  
        {  
            scanf("%d",&line[i]);  
            line[i+n]=line[i];  
        }  
        for(i=0;i<n;i++)  
            dp(line+i);  
        printf("%d\n%d\n",Min,Max);  
        return 0;  
    }  
    

    Pascal :

    var
        a,atmp:array[0..50] of longint;
        f:array[0..9,0..50] of longint;
        n,m,i:longint;
    function g(a,b:longint):longint;
    var
        i:longint;
    begin
        g:=0;
        for i:=a to b do
            if atmp[i]>0 then g:=g+atmp[i]
            else g:=g+atmp[i];
        g:=(g+100000) mod 10;
    end;
    procedure question_min;
    var
        t,i,j,k,tmp,min:longint;
    begin
        min:=maxlongint;
        for t:=1 to n do
        begin
            for i:=1 to n do atmp[i]:=a[(i+t-1) mod n];
            atmp[n-t+1]:=a[n];f[0,1]:=atmp[1];
            for i:=1 to n do f[0,i]:=g(1,i);
            for i:=1 to m-1 do
                for j:=i+1 to n do
                begin
                    f[i,j]:=maxlongint;
                    for k:=i to j-1 do
                    begin
                        tmp:=g(k+1,j);
                        if f[i,j]>tmp*f[i-1,k] then f[i,j]:=tmp*f[i-1,k];
                    end;
                end;
            if f[m-1,n]<min then min:=f[m-1,n];
        end;
        writeln(min);
    end;
    procedure question_max;
    var
        t,i,j,k,tmp,max:longint;
    begin
        max:=0;
        for t:=1 to n do
        begin
            for i:=1 to n do atmp[i]:=a[(i+t-1) mod n];
            atmp[n-t+1]:=a[n];f[0,1]:=atmp[1];
            for i:=1 to n do f[0,i]:=g(1,i);
            for i:=1 to m-1 do
                for j:=i+1 to n do
                begin
                    f[i,j]:=0;
                    for k:=i to j-1 do
                    begin
                        tmp:=g(k+1,j);
                        if f[i,j]<tmp*f[i-1,k] then f[i,j]:=tmp*f[i-1,k];
                    end;
                end;
            if f[m-1,n]>max then max:=f[m-1,n];
        end;
        writeln(max);
    end;
    begin
        readln(n,m);
        for i:=1 to n do readln(a[i]);
        question_min;
        question_max;
    end.
    
    
    • 1

    信息

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