日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Polo the Penguin and Matrix

發(fā)布時(shí)間:2023/12/18 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Polo the Penguin and Matrix 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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?n,?m?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
題解:看數(shù)據(jù)比較小,直接暴力跑了一遍。
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=···,所以如果矩陣中每個(gè)元素模d的余數(shù)不想等,則必然不能通過(guò)加減使得相等。然后排序,再?gòu)闹虚g向兩邊加減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 }

?









轉(zhuǎn)載于:https://www.cnblogs.com/zgglj-com/p/8996744.html

總結(jié)

以上是生活随笔為你收集整理的Polo the Penguin and Matrix的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。