brute force dp implementation sortings ternary search *1400

Please click on ads to support us..

Python Code:

import statistics as stat

line = input().split()
n, m, d = [int(x) for x in line]
nums = []
for i in range(n):
    line = input().split()
    int_line = [int(x) for x in line]
    nums += int_line

base = nums[0] % d
def check(): 
    for i in range(n * m):
        if nums[i] % d != base:
            return False
    return True

if check():
    steps = [(x-base) // d for x in nums]
    median = stat.median(steps)
    result = 0
    for i in range(len(steps)):
        result += abs(steps[i] - median)
    result = int(result)
    print(result)
else:
    print(-1)

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define int ll
#define ll long long
#define pi pair<int, int>
#define vii vector<pi>
#define vi vector<int>
#define vvi vector<vi>
#define vpi vector<pi>
#define all(x) x.begin(),x.end()
#define print(x) cout<<(#x)<<" : "<<x<<endl;
#define pb push_back
#define sti stack<int>
#define mii map<int,int>
#define uii unordered_map<int,int>
#define uci unordered_map<char,int>
#define umsi unordered_map<string,int>
#define si set<int>
#define usi unordered_set<int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define len(s) s.length()
#define w(x) int x; cin>>x; while(x--)
#define mod 1000000007
#define inf 1e18
#define fp(x,y) fixed<<setprecision(y)<<x
#define sum(v,i) accumulate(all(v),i)
#define mk(arr,n,type)  type *arr=new type[n];
#define mem(a,b) memset(a, b, sizeof a)
#define iota(v,i) iota(all(v),i)
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define el "\n"
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define vinn(name,N) int N;cin>>N;vi name(N);rep(i,0,N){cin>>name[i];}
#define vin(name,N) vi name(N);rep(i,0,N) cin>>name[i];
#define show(V) for(auto&i:V){cout<<i<<" ";}cout<<el;
#define flush cin.ignore(numeric_limits<streamsize>::max(), '\n') // clear buffer
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rep2(i,m) for(auto &i:m)
#define rep3(i,m) for(auto i:m)
#define repr(i,a,b) for(int i=a;i>b;i--)
int max(int n1, int n2) {
	return n1 > n2 ? n1 : n2;
}


template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
	const char* comma = strchr (names + 1, ',');
	cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}


void fio() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

}


void solve() {
    int n,m,d;
    cin>>n>>m>>d;
    int size=n*m;
    vin(v,size);
    sort(all(v));
    rep(i,1,size){
        if((v[i]-v[i-1])%d){
            cout<<-1;
            return;
        }
    }
    int cnt=0;
    if(size&1){
        int idx=size/2;
        rep(i,0,size) cnt+=abs(v[idx]-v[i])/d;
    }
    else{
        int i1=size/2,i2=size/2-1;
        int c1=0,c2=0;
        rep(i,0,size) c1+=abs(v[i1]-v[i])/d;
        rep(i,0,size) c2+=abs(v[i2]-v[i])/d;
        cnt=min(c1,c2);
    }
    cout<<cnt;
}


int32_t main()
{
	fio();
	// clock_t z = clock();
	// w(t)
	solve();
	// cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
	return 0;
}


Comments

Submit
0 Comments
More Questions

361A - Levko and Table
5E - Bindian Signalizing
687A - NP-Hard Problem
1542C - Strange Function
961E - Tufurama
129D - String
888A - Local Extrema
722B - Verse Pattern
278A - Circle Line
940A - Points on the line
1742C - Stripes
1742F - Smaller
1742B - Increasing
1742A - Sum
1742D - Coprime
390A - Inna and Alarm Clock
1398B - Substring Removal Game
1742G - Orray
416B - Art Union
962A - Equator
803B - Distances to Zero
291A - Spyke Talks
1742E - Scuza
1506D - Epic Transformation
1354G - Find a Gift
1426F - Number of Subsequences
1146B - Hate "A"
1718C - Tonya and Burenka-179
834A - The Useless Toy
1407D - Discrete Centrifugal Jumps