-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
61 lines (56 loc) · 872 Bytes
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cmath>
using namespace std;
int halla(int arreglo[],int n){
int x=0,suma,aux,i;
for(i=0;i<n;i++){
aux+=abs(arreglo[i]);
}
do{
suma=0;
x++;
for(i=0;i<n;i++){
suma=suma+abs(x-arreglo[i]);
}
if(suma+abs(x)<aux+abs(x-1)){
aux=suma;
}else{
x--;
break;
}
}while(1);
do{
suma=0;
x--;
for(i=0;i<n;i++){
suma=suma+abs(x-arreglo[i]);
}
if(suma+abs(x)<aux+abs(x+1)){
aux=suma;
}else{
x++;
break;
}
}while(1);
return aux+abs(x);
}
int main(int argc, char *argv[]) {
int n,m,d,un_num,resto,movimientos=0,i;
int arreglo[10001];
cin>>n>>m>>d;
cin>>un_num;
resto=un_num%d;
for(i=0;i<n*m-1;i++){
cin>>arreglo[i];
if(arreglo[i]%d!=resto){
cout<<-1;
return 0;
}
}
for(i=0;i<n*m-1;i++){
arreglo[i]-=un_num;
arreglo[i]/=d;
}
cout<<halla(arreglo,m*n-1);
return 0;
}