Polo the Penguin and Matrix

Posted 啦啦啦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Polo the Penguin and Matrix相关的知识,希望对你有一定的参考价值。

Little penguin Polo has an n?×?m matrix, consisting of integers. Let‘s index the matrix rows from 1 to n from top to bottom and let‘s index the columns from 1 to m from left to right. Let‘s represent the matrix element on the intersection of row i and column j as aij.

In one move the penguin can add or subtract number d from some matrix element. Find the minimum number of moves needed to make all matrix elements equal. If the described plan is impossible to carry out, say so.

Input

The first line contains three integers nm and d (1?≤?n,?m?≤?100,?1?≤?d?≤?104) — the matrix sizes and the d parameter. Next n lines contain the matrix: the j-th integer in the i-th row is the matrix element aij (1?≤?aij?≤?104).

Output

In a single line print a single integer — the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print "-1" (without the quotes).

Examples
input
Copy
2 2 2
2 4
6 8
output
Copy
4
input
Copy
1 2 7
6 7
output
Copy
-1
题解:看数据比较小,直接暴力跑了一遍。
 1 #pragma warning(disable:4996)
 2 #include<cmath>
 3 #include<string>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 using namespace std;
 9 
10 const int maxn = 10005;
11 
12 int n, m, d;
13 int a[maxn];
14 
15 int main()
16 {
17     while (cin >> n >> m >> d) {
18         int cnt = 0, ma = 0;
19         for (int i = 1; i <= n; i++) {
20             for (int j = 1; j <= m; j++) {
21                 int tp;
22                 scanf("%d", &tp);
23                 a[++cnt] = tp;
24                 ma = max(ma, tp);
25             }
26         }
27         int ans = 2000000007;
28         for (int i = 1; i <= ma; i++) {
29             int tem = 0;
30             bool flag = true;
31             for (int j = 1; j <= cnt; j++) {
32                 if (abs(a[j] - i) % d) { flag = false; break; }
33                 tem += abs(a[j] - i) / d;
34             }
35             if (flag) ans = min(ans, tem);
36         }
37         if (ans == 2000000007) cout << "-1" << endl;
38         else cout << ans << endl;
39     }
40     return 0;
41 }

正解:
  a1+x1*d=A;
  a2+x2*d=A;
  a3+x3*d=A;
  ·····
显然,a1%d=A%d=a2%d=A%d=a3%d=A%d=···,所以如果矩阵中每个元素模d的余数不想等,则必然不能通过加减使得相等。然后排序,再从中间向两边加减d就行了。
 1 #pragma warning(disable:4996)
 2 #include<cmath>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 const int maxn = 10005;
10 
11 int n, m, d;
12 int a[maxn];
13 
14 int main()
15 {
16     while (cin >> n >> m >> d) {
17         int cnt = 0;
18         for (int i = 1; i <= n; i++) {
19             for (int j = 1; j <= m; j++) scanf("%d", &a[++cnt]);
20         }
21 
22         bool flag = true;
23         for (int i = 2; i <= cnt; i++) if (a[i] % d != a[1] % d) { flag = false; break; }
24 
25         if (!flag) printf("-1\n");
26         else {
27             int ans = 0;
28             int pos = (cnt % 2 == 0) ? cnt / 2 : cnt / 2 + 1;
29 
30             sort(a + 1, a + cnt + 1);
31             for (int i = 1; i <= cnt; i++) if (i != pos) ans += (abs(a[i] - a[pos]) / d);
32             cout << ans << endl;
33         }
34     }
35     return 0;
36 }

 









以上是关于Polo the Penguin and Matrix的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 288C - Polo the Penguin and XOR operation(思路)

Codeforces 835E The penguin's game

Codeforces 835E. The penguin's game

Codeforces.835E.The penguin's game(交互 按位统计 二分)

多态and接口

POLO与LV