1 条题解

  • 1
    @ 2023-4-9 19:47:41
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N=100010;
    typedef long long LL;
    int c[N],s[N],row[N],col[N];
    int n,m,T;
    LL work(int n,int a[])
    {
        
        for(int i=1;i<=n;i++)
        s[i]=s[i-1]+a[i];
        
        if(s[n]%n)
        return -1;
        
        int ave=s[n]/n;
        c[1]=0;
        
        for(int i=2;i<=n;i++)
        c[i]=s[i-1]-(i-1)*ave;
        
        sort(c+1,c+1+n);
        LL res=0;
        
        int center=(n+1)/2;     
        for(int i=1;i<=n;i++)
        res+=abs(c[i]-c[center]);
        return res;
    }
    int main()
    {
        scanf("%d%d%d",&n,&m,&T);
        while(T--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            row[x]++;	
            col[y]++;	
        }
        LL row_num=work(n,row);		
        LL col_num=work(m,col);		
        
        if(row_num!=-1&&col_num!=-1)
        printf("both %lld\n",row_num+col_num);
        
        else if(row_num!=-1)
        printf("row %lld\n",row_num);
        
        else if(col_num!=-1)
        printf("column %lld\n",col_num);
        
        else
        puts("impossible");
        return 0;
    }
    
    • 1

    信息

    ID
    17
    时间
    1000ms
    内存
    128MiB
    难度
    1
    标签
    递交数
    129
    已通过
    102
    上传者