七夕祭

 

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N = 100010;
 5 int row[N], col[N], s[N], c[N];
 6 //s是前缀和数组
 7 ll work(int n, int a[]) {
 8     //先求前缀和
 9     for (int i = 1; i <= n; i++) {
10         s[i] = s[i - 1] + a[i];
11     }
12     if (s[n] % n) {
13         return -1;
14     }
15     int avg = s[n] / n;
16     //求c数组
17     c[1] = 0;
18     for (int i = 2; i <= n; i++) {
19         c[i] = s[i - 1] - (i - 1) * avg;
20     }
21     sort(c + 1, c + 1 + n);
22     ll res = 0;
23     for (int i = 1; i <= n; i++) {
24         res += abs(c[i] - c[(n + 1) / 2]);
25     }
26     return res;
27 }
28 int main() {
29     int n, m, cnt;
30     cin >> n >> m >> cnt;
31     for (int i = 0; i < cnt; i++) {
32         int x, y;
33         cin >> x >> y;
34         row[x]++;
35         col[y]++;
36     }
37     ll r = work(n, row);
38     ll c = work(m, col);
39     //返回-1说明无解
40     if (r != -1 && c != -1) {
41         cout << "both " << r + c << endl;
42     } else if (r != -1) {
43         cout << "row " << r << endl;
44     } else if (c != -1) {
45         cout << "column " << c << endl;
46     } else {
47         cout << "impossible" << endl;
48     }
49     return 0;
50 }

 

posted @ 2020-11-11 14:50  kyk333  阅读(120)  评论(0)    收藏  举报