Header Ad

HackerEarth Counter Strike problem solution

In this HackerEarth Shil and Survival Game problem solution Navi is a counterstrike pro. He always says how good he is at counterstrike. After being tired of Navi, his friends decided to test his skills at shooting. They put M targets on an X - Y plane, each target is denoted by (X, Y) where X is x-coordinate and Y is y-coordinate. His friends also gave him N locations on the X - Y plane from where Navi can shoot the targets. Navi knows that he can shoot a target if the Manhattan distance between his location and target is ≤ D. If Navi can shoot more than half of the targets (for odd values of M check only for an integral part of half of M, say M = 3,2/3, 2/3 = 1 ) only then his friends believe that he is a pro at counter strike otherwise he is not.


HackerEarth Counter Strike problem solution


HackerEarth Counter-Strike problem solution.

#include <bits/stdc++.h>

using namespace std;

int abss(int x)
{
if (x < 0) return -x;
return x;
}

struct point {
int x,y;
} targets[1001], pos[1001];

int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n, m, i, j, ctr, d;
ctr = 0;
scanf("%d%d%d", &n, &m, &d);
for(i = 0; i < n; i++) {
scanf("%d%d", &pos[i].x, &pos[i].y);
}
for(i = 0; i < m; i++) {
scanf("%d%d", &targets[i].x, &targets[i].y);
}
for(j = 0; j < m; j++) {
for(i = 0; i < n; i++) {
if(abss(pos[i].x - targets[j].x) + abss(pos[i].y - targets[j].y) <= d) {
ctr++;
break; // so that a particular target will be counted once
}

}
}
if(ctr > (m)/2) printf("YES\n");
else printf("NO\n");
}
return 0;
}


Second solution

#include <bits/stdc++.h>
using namespace std ;
#define LL long long int
#define ft first
#define sd second
#define PII pair<int,int>
#define MAXN 1000001
#define mp make_pair
#define f_in(st) freopen(st,"r",stdin)
#define f_out(st) freopen(st,"w",stdout)
#define sc(x) scanf("%d",&x)
#define pr(x) printf("%lld\n",x)

int N,M,D,T;
vector<PII> target , position ;
vector<bool> V;

int dist(PII &a,PII &b){
return (abs(a.ft - b.ft) + abs(a.sd - b.sd)) ;
}

bool check(PII &a,PII &b){
return (dist(a,b) <= D) ;
}
int main(){
sc(T) ;
assert(T <= 6 && T >= 1) ;
while(T--){
sc(N) , sc(M) , sc(D) ;
assert(N <= 1000 && N >= 1) ;
assert(M <= 1000 && M >= 1) ;
assert(D <= 50000 && D >= 1) ;
position.resize(N+1) ;
target.resize(M+1) ;
V.resize(M+1) ;
fill(V.begin(),V.end(),0) ;
for(int i=1;i<=N;i++){
sc(position[i].ft) ;
sc(position[i].sd) ;
assert(position[i].ft <= 50000 && position[i].ft >= -50000) ;
assert(position[i].sd <= 50000 && position[i].sd >= -50000) ;
}
for(int i=1;i<=M;i++){
sc(target[i].ft) ;
sc(target[i].sd) ;
assert(target[i].ft <= 50000 && target[i].ft >= -50000) ;
assert(target[i].sd <= 50000 && target[i].sd >= -50000) ;
}
bool ok = 0 ;
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
if(check(position[i],target[j]))
V[j] = 1 ;
}
}
int cnt = 0 ;
for(int i=1;i<=M;i++){
if(V[i]) cnt ++ ;
}
if(cnt > M/2)
ok = 1 ;
puts(ok ? "YES" : "NO") ;
}
return 0 ;
}

Post a Comment

0 Comments