Created
April 18, 2019 22:24
-
-
Save garganshul108/1f3b813a039305dfbd1e67925604991c to your computer and use it in GitHub Desktop.
Correctsol.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define ll long long | |
#define fr(v,s,n) for(int v=s;((s<n)?v<n:v>n);((s<n)?v++:v--)) | |
#define debug(p) cout<<#p<<' '<<p<<'\n'; | |
#define debuga(a,i) cout<<#a<<"["<<i<<"]"<<" "<<a[i]<<"\n"; | |
#define debugal(a) for(int v=0;v<(sizeof(a)/sizeof(a[0]));v++) cout<<#a<<"["<<v<<"]"<<" "<<a[v]<<"\n"; | |
#define debugm(a,i,j) cout<<#a<<"["<<i<<"]["<<j<<"]"<<" "<<a[i][j]<<"\n"; | |
#define pb push_back | |
#define mp make_pair | |
#define vii vector<pair<int,int>> | |
#define vi vector<int> | |
#define si stack<int> | |
#define fread(ff) freopen(ff,"r",stdin); | |
#define fwrite(ff) freopen(ff,"w",stdout); | |
#define dbl double | |
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); | |
#define sec second | |
#define fst first | |
#define sep cout<<"\n------------------\n"; | |
// modd function | |
int modd(int a, int b) | |
{ | |
if(a>=0) return a%b; | |
else return (a%b + b); | |
} | |
int Solution::solve(vector<vector<int> > &A, int B) { | |
int n = A.size(); | |
int m = A[0].size(); | |
vector<int> c; | |
fr(i,0,n) fr(j,0,m) c.pb(A[i][j]); | |
vector<int>mm(c.size()); | |
// | |
// | |
// mod fn corrected | |
// | |
// i am taking care of c[i] being negative or positive here | |
// it gives WA | |
// | |
// | |
fr(i,0,n*m) mm[i] = modd(c[i],B); | |
sort(mm.begin(),mm.end()); | |
if(mm[0] != mm[n*m -1]) return -1; | |
sort(c.begin(),c.end()); | |
int index = (int)((m*n)/2); | |
int median = c[index]; | |
// debug(median) | |
int ans = 0; | |
fr(i,0,m*n) | |
{ | |
// sep | |
int tt = abs(c[i] - median); | |
int temp = (tt/B); | |
// debug(tt) | |
// debug(temp)debug(c[i]) | |
ans += temp; | |
// debug(ans); | |
// sep | |
} | |
return ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment