JZOJ 3382. 【NOIP2013模拟】七夕祭 (Standard IO)
JZOJ 3382. 【NOIP2013模拟】七夕祭 (Standard IO)
3382. 【NOIP2013模拟】七夕祭 (Standard IO)
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <iostream> 5 #include <algorithm> 6 #define N 100007 7 using namespace std; 8 long long n, m, k, r[N], c[N], rs[N], cs[N], ans; 9 10 long long read() 11 { 12 long long s = 0; 13 char ch = getchar(); 14 while (ch < '0' || ch > '9') ch = getchar(); 15 while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); 16 return s; 17 } 18 19 int abs(int a) {if (a < 0) return -a; return a;} 20 21 void work1() 22 { 23 long long s = k / m; 24 for (int i = 1; i <= m; i++) 25 cs[i] += cs[i - 1] + (c[i] - s); 26 sort(cs + 1, cs + m + 1); 27 long long sum = 0; 28 for (int i = 1; i <= m; i++) 29 sum += abs(cs[i] - cs[(1 + m) / 2]); 30 ans += sum; 31 } 32 33 void work2() 34 { 35 long long s = k / n; 36 for (int i = 1; i <= n; i++) 37 rs[i] += rs[i - 1] + (r[i] - s); 38 sort(rs + 1, rs + n + 1); 39 long long sum = 0; 40 for (int i = 1; i <= n; i++) 41 sum += abs(rs[i] - rs[(1 + n) / 2]); 42 ans += sum; 43 } 44 45 int main() 46 { 47 n = read(), m = read(), k = read(); 48 int x, y; 49 for (int i = 1; i <= k; i++) 50 { 51 x = read(), y = read(); 52 r[x]++, c[y]++; 53 } 54 if (k % n == 0 && k % m == 0) 55 { 56 work1(); 57 work2(); 58 printf("both "); 59 printf("%lld", ans); 60 } 61 else if (k % n == 0) 62 { 63 work2(); 64 printf("row "); 65 printf("%lld", ans); 66 } 67 else if (k % m == 0) 68 { 69 work1(); 70 printf("column "); 71 printf("%lld", ans); 72 } 73 else printf("impossible"); 74 }
View Code