feat: Add solution for Codeforces problem 1986E
This commit is contained in:
parent
7900783488
commit
771ba7f813
84
Codes/1986 E - Beautiful Array/1986E.cpp
Normal file
84
Codes/1986 E - Beautiful Array/1986E.cpp
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
#ifdef ONLINE_JUDGE
|
||||||
|
#define dbg(...)
|
||||||
|
#else
|
||||||
|
#include "Assets/debug.h"
|
||||||
|
#endif
|
||||||
|
#define int long long
|
||||||
|
#define ll long long
|
||||||
|
#define endl '\n'
|
||||||
|
#define all(x) x.begin(), x.end()
|
||||||
|
#define sz(x) (int)(x).size()
|
||||||
|
#define yes cout << "YES" << endl
|
||||||
|
#define no cout << "NO" << endl
|
||||||
|
#define rep(i,a,b) for(int i=a; i<b; ++i)
|
||||||
|
#define rrep(i,a,b) for(int i=a; i>=b; --i)
|
||||||
|
#define pb push_back
|
||||||
|
#define ppb pop_back
|
||||||
|
#define ins insert
|
||||||
|
#define ff first
|
||||||
|
#define ss second
|
||||||
|
#define FAST (ios_base::sync_with_stdio(false), cin.tie(nullptr));
|
||||||
|
ll pow(ll x,ll y,ll m=1e9+7){ll ans=1;x%=m;while(y){if(y&1)ans=(ans*x)%m;x=(x*x)%m;y>>=1;}return ans;}
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n,k;
|
||||||
|
cin>>n>>k;
|
||||||
|
map<int,vector<int>>m;
|
||||||
|
rep(i,0,n)
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
cin>>x;
|
||||||
|
m[x%k].pb(x/k);
|
||||||
|
}
|
||||||
|
int odd=n&1, ans=0;
|
||||||
|
for(auto it:m)
|
||||||
|
{
|
||||||
|
vector<int>&v=it.second;
|
||||||
|
int len = sz(v);
|
||||||
|
sort(all(v));
|
||||||
|
if(len&1)
|
||||||
|
{
|
||||||
|
if(!odd)
|
||||||
|
{
|
||||||
|
cout<<-1<<endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
odd--;
|
||||||
|
if(len==1) continue;
|
||||||
|
vector<int>pf(len,0),sf(len,0);
|
||||||
|
pf[1]=v[1]-v[0];
|
||||||
|
sf[len-2]=v[len-1]-v[len-2];
|
||||||
|
for(int i=3; i<len; i+=2) pf[i]=pf[i-2]+v[i]-v[i-1];
|
||||||
|
for(int i=len-4; i>=0; i-=2) sf[i]=sf[i+2]+v[i+1]-v[i];
|
||||||
|
int mn=INT_MAX;
|
||||||
|
for(int i=0; i<len; i+=2)
|
||||||
|
{
|
||||||
|
int sum=0;
|
||||||
|
if(i>0) sum+=pf[i-1];
|
||||||
|
if(i<len-1) sum+=sf[i+1];
|
||||||
|
mn = min(mn,sum);
|
||||||
|
}
|
||||||
|
ans+=mn;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for(int i=1; i<len; i+=2) ans+=v[i]-v[i-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout<<ans<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
signed main()
|
||||||
|
{
|
||||||
|
FAST;
|
||||||
|
int TCS = 1;
|
||||||
|
cin >> TCS;
|
||||||
|
for (int TC = 1; TC <= TCS; ++TC)
|
||||||
|
{
|
||||||
|
// cout<<"Case "<<TC<<": ";
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
@ -353,6 +353,7 @@ This repository contains my solutions of Codeforces problems. They are in C++ la
|
|||||||
| 341 | 1986 B | Matrix Stabilization | [Question](https://codeforces.com/problemset/problem/1986/B) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1986%20B%20-%20Matrix%20Stabilization)
|
| 341 | 1986 B | Matrix Stabilization | [Question](https://codeforces.com/problemset/problem/1986/B) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1986%20B%20-%20Matrix%20Stabilization)
|
||||||
| 342 | 1986 C | Update Queries | [Question](https://codeforces.com/problemset/problem/1986/C) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1986%20C%20-%20Update%20Queries)
|
| 342 | 1986 C | Update Queries | [Question](https://codeforces.com/problemset/problem/1986/C) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1986%20C%20-%20Update%20Queries)
|
||||||
| 343 | 1986 D | Mathematical Problem | [Question](https://codeforces.com/problemset/problem/1986/D) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1986%20D%20-%20Mathematical%20Problem)
|
| 343 | 1986 D | Mathematical Problem | [Question](https://codeforces.com/problemset/problem/1986/D) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1986%20D%20-%20Mathematical%20Problem)
|
||||||
|
| 344 | 1986 E | Beautiful Array | [Question](https://codeforces.com/problemset/problem/1986/E) | [Solution](https://github.com/ShazidMashrafi/Codeforces-Solutions/tree/master/Codes/1986%20E%20-%20Beautiful%20Array)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user