环形均分纸牌
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; typedef long long ll; const int maxn=100000+10; ll heng[maxn],shu[maxn]; ll sum[maxn]; ll getans(ll a[maxn],int n){ ll k=a[0]/n; for (int i=1;i<=n;i++){ a[i]=a[i]-k; sum[i]=sum[i-1]+a[i]; } ll ans=0; sort(sum+1,sum+1+n); for (int i=1;i<=n;i++) ans+=abs(sum[i]-sum[n+1>>1]); return ans; } int main(){ int m,n,t; int u,v; scanf("%d%d%d",&n,&m,&t); for (int i=1;i<=t;i++){ scanf("%d%d",&u,&v); heng[u]++; shu[v]++; } for (int i=1;i<=n;i++) heng[0]+=heng[i]; for (int i=1;i<=m;i++) shu[0]+=shu[i]; if(heng[0]%n==0&&shu[0]%m==0){ printf("both %lld ",getans(heng,n)+getans(shu,m)); } else if(heng[0]%n==0){ printf("row %lld ",getans(heng,n)); } else if(shu[0]%m==0){ printf("column %lld ",getans(shu,m)); } else printf("impossible "); return 0; }