AcWing 105. 七夕祭
原题链接
困难
作者:
dajianer
,
2021-06-01 19:55:51
,
所有人可见
,
阅读 323
P34 中位数的性质
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5+5;
int x[maxn], y[maxn];
int row[maxn];
int col[maxn];
int cal(int a[], int n) {
vector<int> sum(n+1, 0);
for (int i = 1; i <= n; i++) {
sum[i] = sum[i-1] + a[i] - a[0]/n;
}
sort(sum.begin()+1, sum.end());
int ans = 0;
int temp = (n + 1) / 2;
for (int i = 1; i <= n; i++) {
ans += abs(sum[i] - sum[temp]);
}
return ans;
}
signed main() {
int n, m, t;
cin >> n >> m >> t;
for (int i = 1; i <= t; i++) {
cin >> x[i] >> y[i];
row[x[i]]++;
col[y[i]]++;
}
for (int i = 1; i <= n; i++) {
row[0] += row[i];
}
for (int i = 1; i <= m; i++) {
col[0] += col[i];
}
if (t % n == 0 && t % m == 0) cout << "both " << cal(row, n) + cal(col, m) << '\n';
else if (t % n== 0) cout << "row " << cal(row, n) << '\n';
else if (t % m == 0) cout << "column " << cal(col, m) << '\n';
else cout << "impossible\n";
}