Header Ad

HackerEarth Explosive Game problem solution

In this HackerEarth Explosive Game problem solution, There's no day that Kevin doesn't want to explore something.

Today Kevin and Elvis decided to make dynamite. They have N packages of explosives (N is an even number). The i-th package contains two boxes: one has ai grams of explosives the other bi grams. Elvis and Kevin alternatively take any package and add one of the boxes to giant dynamite. Another box is discarded. Elvis starts this process. Kevin believes an explosion is beautiful if the giant dynamite contains exactly x grams of explosives. But he hasn't decided on the value of x yet. This process ends when all boxes are taken.

Help him and determine for each of Q values xi whether Kevin can make dynamite with exactly xi grams of explosives regardless of the Elvis' moves.


HackerEarth Explosive Game problem solution


HackerEarth Explosive Game problem solution.

#include <string>
#include <vector>
#include <map>
#include <list>
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility>
#include <cassert>
#include<complex>
#include <time.h>
using namespace std;

#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, b, a) for(int i=(b)-1;i>=(a);--i)
#define FILL(A,value) memset(A,value,sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define Pi 3.14159265358979
#define x0 ikjnrmthklmnt
#define y0 lkrjhkltr
#define y1 ewrgrg

typedef long long Int;
typedef unsigned long long UInt;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef complex<double> base;

const int INF = 1000000000;
const int MAX = 107;
const int MAXE = 5000;
const int MAXV = 20*20;
const int BASE = 1000000007;
const int MOD = 1000000007;

int main()
{
int n, q;
cin >> n >> q;

VI a;

Int S = 0;

FOR(i,0,n)
{
int l , r;
cin >> l >> r;
if (l > r) swap(l, r);
S += l;
a.push_back(r - l);
}

sort(ALL(a));

bool ok = 1;
for(int i = 0;i < n; i += 2)
{
if (a[i] != a[i + 1]) ok = 0;
S += a[i];
}
if (!ok) S = -1;

FOR(i,0,q)
{
Int x;
scanf("%lld" , &x);
if (x == S)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}

return 0;
}


Second solution

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

vector<int>v;
long long can;
int main() {

int n, q;
cin>>n>>q;
for (int i = 1; i <= n; i++) {
int aa, bb;
cin>>aa>>bb;
if (aa > bb) swap(aa, bb);
v.push_back(bb - aa);
can += aa;
}


bool fl = true;
sort(v.begin(), v.end() );
for (int i = 0; i < v.size(); i += 2) {
if (v[i] != v[i + 1]) {
fl = false;
}
else {
can += v[i];
}
}

for (int i = 0; i < q; i++) {
long long x;
cin>>x;
if (fl == true && x == can) {
cout<<"YES\n";
}
else {
cout<<"NO\n";
}
}
return 0;
}

Post a Comment

0 Comments