博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Polo the Penguin and Matrix
阅读量:4641 次
发布时间:2019-06-09

本文共 3121 字,大约阅读时间需要 10 分钟。

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
3 #include
4 #include
5 #include
6 #include
7 #include
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
3 #include
4 #include
5 #include
6 #include
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 }
 

 

 

转载于:https://www.cnblogs.com/zgglj-com/p/8996744.html

你可能感兴趣的文章
hdu 1728 逃离迷宫
查看>>
HTML5与CSS3权威指南之CSS3学习记录
查看>>
docker安装部署
查看>>
AVL树、splay树(伸展树)和红黑树比较
查看>>
多媒体音量条显示异常跳动
查看>>
运算符及题目(2017.1.8)
查看>>
React接入Sentry.js
查看>>
ssh自动分发密匙脚本样板
查看>>
转 小辉_Ray CORS(跨域资源共享)
查看>>
Linux安装postgresql
查看>>
MyBatis启动:MapperStatement创建
查看>>
【 全干货 】5 分钟带你看懂 Docker !
查看>>
[转]优化Flash性能
查看>>
popStar手机游戏机机对战程序
查看>>
Java Web项目结构
查看>>
lambda表达式树
查看>>
OpenCV YUV 与 RGB的互转(草稿)
查看>>
二次注入原理及防御
查看>>
会话记住已登录功能
查看>>
Linux内核分析——可执行程序的装载
查看>>