1. 程式人生 > >【BZOJ3032】七夕祭【絕對值不等式】【中位數】【數形結合】

【BZOJ3032】七夕祭【絕對值不等式】【中位數】【數形結合】

這題是BZOJ1045的二維版本。因為行列獨立,所以分別跑一次就行啦。

/* Pigonometry */
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 100005;

int n, m, t, x[maxn], y[maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int intabs(int x) {
	return x < 0 ? -x : x;
}

inline LL solve(int *r, int n) {
	static int c[maxn];
	for(int i = 0; i <= n; i++) c[i] = 0;

	for(int i = 1; i <= t; i++) c[r[i]]++;
	int M = t / n;
	for(int i = 1; i <= t; i++) c[i] += c[i - 1] - M;
	sort(c + 1, c + n + 1);

	LL ans = 0; int mid = c[(n + 1) >> 1];
	for(int i = 1; i <= n; i++) ans += intabs(c[i] - mid);
	return ans;
}

int main() {
	n = iread(); m = iread(); t = iread();
	for(int i = 1; i <= t; i++) x[i] = iread(), y[i] = iread();

	int flag = 0;
	if(t % n && t % m) flag = 1;
	else if(t % n == 0 && t % m == 0) flag = 2;
	else if(t % n == 0) flag = 3;
	else if(t % m == 0) flag = 4;

	LL ans = 0;
	if(flag == 1) printf("impossible\n");
	else if(flag == 2) ans = solve(x, n) + solve(y, m), printf("both %lld\n", ans);
	else if(flag == 3) ans = solve(x, n), printf("row %lld\n", ans);
	else if(flag == 4) ans = solve(y, m), printf("column %lld\n", ans);
	return 0;
}