Lydsy3032 七夕祭(货仓选址+均分纸牌结合)
环形均分纸牌
#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\n",getans(heng,n)+getans(shu,m)); } else if(heng[0]%n==0){ printf("row %lld\n",getans(heng,n)); } else if(shu[0]%m==0){ printf("column %lld\n",getans(shu,m)); } else printf("impossible\n"); return 0; }