思路
代码
#include
#include
#include
#include
using namespace std;
const int N = 100010;
typedef long long ll;
ll a[N], b[N];
int n, m, t;
ll f[N];
ll calc(ll *A, int n)
{
ll ans = 0;
memset(f, 0, sizeof f);
for (int i = 1; i <= n; i++)
{
A[i] -= A[0] / n;
f[i] = f[i - 1] + A[i];
}
sort(f + 1, f + n + 1);
for (int i = 1; i <= n; i++)
{
ans += abs(f[i] - f[(n + 1) / 2]);
}
return ans;
}
int main()
{
scanf("%d%d%d", &n, &m, &t);
for (int i = 1; i <= t; i++)
{
int x, y;
scanf("%d%d", &x, &y);
a[x]++;
b[y]++;
}
for (int i = 1; i <= n; i++)
a[0] += a[i];
for (int i = 1; i <= m; i++)
b[0] += b[i];
if (a[0] % n == 0 && b[0] % m == 0)
printf("both %lld\n", calc(a, n) + calc(b, m));
else if (a[0] % n == 0 && b[0] % m != 0)
printf("row %lld\n", calc(a, n));
else if (a[0] % n != 0 && b[0] % m == 0)
printf("column %lld\n", calc(b, m));
else
puts("impossible");
return 0;
}