Header Ad

HackerEarth Reduction Game problem solution

In this HackerEarth Reduction Game problem solution You are given an array A consisting of N distinct integers. If the size of the array is greater than one, then you can perform the following operations any number of times:

Select two integers Ai and Aj such that CountOnes(Ai XOR Aj) = 1 and delete either Ai or Aj from the array. This operation costs C1.

Select two integers Ai and Aj such that CountOnes(Ai XOR Aj) > 1 and delete either Ai or Aj from the array. This operation costs C2.

Here, CountOnes(X) returns the number of 1s available in the binary representation of integer X. Determine the minimum cost to reduce the size of the array to 1.


HackerEarth Reduction Game problem solution


HackerEarth Reduction Game problem solution.

#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> v[10005],cc[10005];
queue<int> q;
unordered_map<ll,ll> f , mapid;
int t, n, vis[10005], lev[10005], start;
ll c1,c2, pow2[40], a[10005];
void dfs(int s,int cid)
{
vis[s]=1;
cc[cid].push_back(s);
for(auto i:v[s]){
if(vis[i]==0){
dfs(i,cid);
}
}
}
void init()
{
ll res=1;
pow2[0]=res;
for(int i=1;i<=40;i++){
res=res*2;
pow2[i]=res;
}
}
void build_graph()
{
for(int i=1;i<=n;i++){
for(int j=0;j<=30;j++){
ll factor = pow2[j];
ll new_val = a[i];
if((pow2[j]&a[i]) == 0){
new_val += factor;
}
else{
new_val -= factor;
}
if(f[new_val] > 0){
v[i].push_back(mapid[new_val]);
}
}
}
}
ll solve1(int conn)
{
ll res = (conn-1)*c2;
for(int i = 1;i<=conn;i++){
res+= (cc[i].size()-1)*c1;
}
return res;
}
int solve2(int s)
{
while(!q.empty())
{
q.pop();
}
q.push(s);
vis[s]=true;
while(!q.empty())
{
int x = q.front();
q.pop();
for(auto i:v[x])
{
if(vis[i]==0){
q.push(i);
vis[i]=1;
lev[i]=lev[x]+1;
if(lev[i]>=3){
return 1;
}
}
}
}
return 0;
}
int main()
{
//freopen("test1.txt","r",stdin);
//freopen("file1.txt","w",stdout);
std::ios_base::sync_with_stdio(false);
cin.tie(0);
init();
cin>>t;
while(t--)
{
cin>>n;
cin>>c1>>c2;
f.clear();
mapid.clear();
for(int i=1;i<=n;i++){
cin>>a[i];
f[a[i]]++;
mapid[a[i]]=i;
v[i].clear();
cc[i].clear();
}
build_graph();
ll minv = 1e9 + 5;
for(int i=1;i<=n;i++){
if(v[i].size() < minv){
minv = v[i].size();
start = i;
}
}
memset(vis,0,sizeof(vis));
int conn=0;
for(int i=1;i<=n;i++){
if(vis[i]==0){
conn++;
dfs(i,conn);
}
}
if(c1 <= c2){
cout<<solve1(conn)<<endl;
}
else{
if(conn>1){
//cout<<"Err.............1"<<endl;
cout<<((n-1)*c2)<<endl;
}
else{
memset(vis,0,sizeof(vis));
memset(lev,0,sizeof(lev));
//cout<<"Errr............2"<<endl;
int res = solve2(start);
if(res==1){
cout<<((n-1)*c2)<<endl;
}
else{
cout<<((n-2)*c2 + c1)<<endl;
}
}
}
}
}

Second solution

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int M = 1e4 + 239;
const int B = 30;

int n, a[M];
ll c1, c2;
bool used[M];
map<int, int> id;

void dfs(int p)
{
used[p] = true;
for (int i = 0; i < B; i++)
{
int r = (1 << i) ^ a[p];
if (id.find(r) == id.end()) continue;
r = id[r];
if (!used[r]) dfs(r);
}
}

void dfs2(int p)
{
used[p] = true;
for (int i = 0; i < n; i++)
if (!used[i] && __builtin_popcount(a[i] ^ a[p]) > 1)
dfs2(i);
}

void solve()
{
cin >> n >> c1 >> c2;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
if (c2 <= c1)
{
if (n > 100)
{
cout << (ll)c2 * (ll)(n - 1) << "\n";
return;
}
for (int i = 0; i < n; i++) used[i] = false;
ll w = -1;
for (int i = 0; i < n; i++)
if (!used[i])
{
w++;
dfs2(i);
}
cout << w * (ll)c1 + (ll)(n - 1 - w) * (ll)c2 << "\n";
return;
}
id.clear();
for (int i = 0; i < n; i++) id[a[i]] = i;
for (int i = 0; i < n; i++) used[i] = false;
ll w = -1;
for (int i = 0; i < n; i++)
if (!used[i])
{
w++;
dfs(i);
}
cout << w * (ll)c2 + (ll)(n - 1 - w) * (ll)c1 << "\n";
}

int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T;
cin >> T;
while (T--) solve();
return 0;
}

Post a Comment

0 Comments