AcWing 105. 七夕祭
作者:互联网
分治 + 推式子(构造绝对值不等式)
交换行的元素时每列的元素个数不变,交换列时同理,所以可以分别考虑行和列
对于行(列),可以看成均分纸牌的模型,唯一的不同点是从链变成了环(首尾可交换)
然后就可以推式子了
推式子具体过程
#include<bits/stdc++.h>
using namespace std;
#define fr first
#define se second
#define et0 exit(0);
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const int INF = 0X3f3f3f3f, N = 1e5 + 10, MOD = 1e9 + 7;
int row[N], clm[N];
LL c[N];
LL calc(int n, int a[], int sum) {
if (sum % n) return -1;
int ave = sum / n;
for (int i = 1; i <= n; i++) a[i] -= ave;
c[1] = 0;
for (int i = 2; i <= n; i++) c[i] = c[i - 1] + a[i];
sort(c + 1, c + n + 1);
LL res = 0;
int mid = n / 2 + 1;
for (int i = 1; i <= n; i++) res += abs(c[mid] - c[i]);
return res;
}
void work() {
int n, m, t;
cin >> n >> m >> t;
for (int i = 1; i <= t; i++) {
int x, y;
cin >> x >> y;
row[x]++;
clm[y]++;
}
LL ct1 = calc(n, row, t), ct2 = calc(m, clm, t);
if (ct1 == -1 && ct2 == -1) cout << "impossible" << endl;
else if (ct2 == -1) cout << "row " << ct1 << endl;
else if (ct1 == -1) cout << "column " << ct2 << endl;
else cout << "both " << ct1 + ct2 << endl;
}
signed main() {
work();
return 0;
}
标签:clm,typedef,row,int,LL,long,105,七夕,AcWing 来源: https://www.cnblogs.com/xhy666/p/16435210.html