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?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
題解:看數據比較小,直接暴力跑了一遍。
 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 }
 

?









轉載于:https://www.cnblogs.com/zgglj-com/p/8996744.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/452768.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/452768.shtml
英文地址,請注明出處:http://en.pswp.cn/news/452768.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

趣解 XSS和CSRF的原理

參考文章&#xff1a;趣解 XSS和CSRF的原理 推薦網站&#xff1a;古黑論 感謝作者分享&#xff01;

js異步解決方案 --- 回調函數 vs promise vs generater/yield vs async/await

javascript -- 深度解析異步解決方案 高級語言層出不窮, 然而唯 js 鶴立雞群, 這要說道js的設計理念, js天生為異步而生, 正如布道者樸靈在 node深入淺出--(有興趣的可以讀一下, 很有意思^_^) , 異步很早就存在于操作系統的底層, 意外的是&#xff0c;在絕大多數高級編程語言中…

什么是TPDU

TPDU,全稱Transport Protocol Data Unit&#xff0c;是指傳送協議數據單元。代表從一個傳輸實體發送至另一個傳輸實體的消息。 我們需要為傳輸實體之間交換的數據單元起一個更加一般化的名字&#xff0c;TCP的術語是數據段&#xff0c;它很容易混淆&#xff0c;而且在TCP領域之…

sql注入基本原理

1. 參考文獻&#xff1a; 趣解SQL注入原理 Sql注入基本原理 2.參考書籍

項目管理雜談-員工的積極性在哪里?

項目開發過程中&#xff0c;每每有人感嘆&#xff0c;曾幾何時&#xff0c;隊伍如何好帶&#xff0c;如何好用&#xff0c;而如今&#xff0c;人心繁雜&#xff0c;隊伍不好帶了。很多人的想法是“人望高處走”&#xff0c;不停的尋找待遇及其他方面更好的單位。其實&#xff0…

centos7硬盤分區

首先在虛擬機的設置中為系統添加硬盤 使用fdisk -l /dev/sdb 查看未分區的硬盤 fdisk -l /dev/sda 這是已經分區好得 接下來我們就要對sdb進行分區: 首先使用fdisk /dev/sdb 接著輸入m可以看到詳細命令 進行添加分區 已經建立好4個主分區&#xff0c;在建立時會看到以下 刪除…

java上傳rar文件_java實現上傳zip/rar壓縮文件,自動解壓

在pom中添加解壓jar依賴4.0.0org.springframework.bootspring-boot-starter-parent2.1.2.RELEASEcom.hfuncompress0.0.1-SNAPSHOTuncompress上傳壓縮文件(rar或者zip格式),解壓1.8org.springframework.bootspring-boot-starter-weborg.projectlomboklomboktrueorg.springframew…

從MapReduce的執行來看如何優化MaxCompute(原ODPS) SQL

摘要&#xff1a; SQL基礎有這些操作&#xff08;按照執行順序來排列&#xff09;&#xff1a; from join(left join, right join, inner join, outer join ,semi join) where group by select sum distinct count order by 如果我們能理解mapreduce是怎么實現這些SQL中的基本操…

套接字(socket)基本知識與工作原理

套接字&#xff08;socket&#xff09;基本知識與工作原理 一、Socket相關概念 Socket通常也稱作“套接字”&#xff0c;用于描述IP地址和端口&#xff0c;是一個通信鏈的句柄。&#xff08;其實就是兩個程序通信用的。&#xff09; SOCKET用于在兩個基于TCP/IP協議的應用程序之…

python 多線程--重點知識

1.全局變量global的用法 2.多線程共享全局變量-args參數 注意args參數類型為元組&#xff0c;逗號不能少&#xff01;

Flask WTForm表單的使用

運行環境&#xff1a; python2.7 flask 0.11 flask-wtf 0.14.2 wtform能夠通過一個類定義一些字段&#xff0c;這些字段會在前端生成標簽&#xff0c;并且通過設置字段的驗證規則&#xff0c;自動判斷前端輸入數據的格式。 一般用于用戶登錄&#xff0c;用戶注冊等信息錄入。…

Java與C#個人之比較

網上這方面的比較文章已經有不少了&#xff0c;不過大都是要么從很高的角度說的&#xff0c;要么就是從底層說的&#xff0c;本人就以自己這幾年的編程經歷中的感受&#xff0c;來談談自己的體會。 相似性&#xff1a; Java和C#都是一門面向對象的語言&#xff0c;Java更多地…

java利用子類求正方形_Java程序設計實驗2011

(2)掌握對象的聲明和使用&#xff1b;(3)掌握構造方法的概念和使用&#xff1b;(4)掌握類及成員的訪問控制符。2、實驗任務(1)閱讀下面的程序&#xff0c;在main()方法里添加語句完成如下的功能&#xff1a;①創建一個MyV alue類的對象myV alue。②為myV alue對象中的value域賦…

當導用模塊與包的import與from的問題(模塊與包的調用)

當在views.py里寫impor models會不會報錯呢&#xff1f; 1、Python里面的py文件都是每一行的代碼。2、Python解釋器去找一個模塊的時候&#xff0c;只去sys.path的路徑里找3、django項目啟動&#xff08;django項目的啟動文件是manage.py&#xff09;啟動項目是將manage.py的路…

ack和seq

ACK (Acknowledgement&#xff09;&#xff0c;即確認字符&#xff0c;在數據通信中&#xff0c;接收站發給發送站的一種傳輸類控制字符。表示發來的數據已確認接收無誤。 seq是序列號&#xff0c;這是為了連接以后傳送數據用的&#xff0c;ack是對收到的數據包的確認&#xff…

MySQL中的information_schema

0.引言 近日在學習網絡安全的sql注入時&#xff0c;用到mysql中的information_schema數據庫&#xff0c;其思路是利用information_schema中的SCHEMA獲取數據庫中的table名稱。現在對相關數據庫進行總結&#xff0c;方便以后復習使用。 2.information_schema數據庫 informati…

linux配置防火墻,開啟端口

linux配置防火墻&#xff0c;開啟端口 Centos7,配置防火墻&#xff0c;開啟端口  1.查看已開放的端口(默認不開放任何端口)    firewall-cmd --list-ports  2.開啟80端口    firewall-cmd --zonepublic(作用域) --add-port80/tcp(端口和訪問類型) --permanent(永久…

使用Intel編譯器系列合集

好的帖子&#xff1a;http://topic.csdn.net/u/20080327/16/071b45df-3795-4bf1-9c4d-da4eb5aaa739.html參考手冊&#xff1a;http://software.intel.com/sites/products/documentation/studio/composer/en-us/2011Update/compiler_c/index.htm 說明&#xff1a;本系列文章為個…

【前端】這可能是你看過最全的css居中解決方案了~

1.水平居中&#xff1a;行內元素解決方案 適用元素&#xff1a;文字&#xff0c;鏈接&#xff0c;及其其它inline或者inline-*類型元素&#xff08;inline-block&#xff0c;inline-table&#xff0c;inline-flex&#xff09; html部分代碼:<div>文字元素</div><…

java手機一款三國游戲_JAVA熱游—富甲三國之雄霸天下原創心得

因為工作忙碌的關系&#xff0c;很長時間都沒有來關注手機游戲論壇&#xff0c;這款富甲三國.雄霸天下&#xff0c;我也是前天才拿到手。游戲比想象中的簡單&#xff0c;個人僅用了兩個小時時間&#xff0c;就將三個人物全部通關。游戲的開始畫面制作得比較精美&#xff0c;而且…