105. 七夕祭【环形均分纸牌问题】

在这里插入图片描述
首先你会发现,行交换和列交换是无影响的。故可以分开讨论。
这不就变成了环形均分纸牌的问题了么,和蓝桥杯糖果那题类似。
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long int LL;
LL x[N],y[N],c[N],n,m,t;
LL solve(LL s[],int n)
{
    
    
    LL avg=s[n]/n;
    for(int i=1;i<=n;i++) c[i]=avg*i-s[i];
    sort(c+1,c+n+1);
    LL temp=c[(n+1)/2];
    LL sum=0;
    for(int i=1;i<=n;i++) sum+=abs(temp-c[i]);
    return sum;
}
int main(void)
{
    
    
    cin>>n>>m>>t;
    while(t--)
    {
    
    
        int a,b; cin>>a>>b;
        x[a]++,y[b]++;
    }
    for(int i=1;i<=n;i++) x[i]+=x[i-1];
    for(int i=1;i<=m;i++) y[i]+=y[i-1];
    if(x[n]%n==0&&y[m]%m==0)
    {
    
    
        cout<<"both "<<solve(x,n)+solve(y,m);
    }else if(x[n]%n==0)
    {
    
    
        cout<<"row "<<solve(x,n);
    }
    else if(y[m]%m==0)
    {
    
    
         cout<<"column "<<solve(y,m);
    }else puts("impossible");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_46527915/article/details/123606587