Header Ad

HackerEarth Problem B Pikachu vs Team Meowstic and Helping Hand solution

In this HackerEarth ( Problem B) Pikachu vs Team Meowstic and Helping Hand problem solution Pikachu loves battling with other Pokemon. This time he has a team of N Meowstic to fight, ith of which has strength ai. He wants to fight with all of them K times. Team Meowstic came to know about this and now they have devised a strategy to battle against the mighty Pikachu.

All the N Meowstic stand in a straight line numbered from 1 to N. Before every round of battle, they simultaneously use a move, called Helping Hand. It changes the attacking power of Team Meowstic as follows:

The attacking power of first Meowstic remains ai.
The attacking power of remaining Meowstic changes as ai = ai | ai - 1 where 2 <= i <= N and A|B represents the bitwise OR of A and B 
For example, if the current attacking powers are [2,1,4,6,3], after using the Helping Hand, the powers change to [2,1|2,4|1,6|4,3|6], or [2,3,5,6,7].

Help Pikachu by finding the attacking powers of all Meowstic when he fights each of them for the last time, that is, for the Kth round.


HackerEarth ( Problem B ) Pikachu vs Team Meowstic and Helping Hand problem solution


HackerEarth ( Problem B ) Pikachu vs Team Meowstic and Helping Hand problem solution.

#include <bits/stdc++.h>

using namespace std;

int main(){
int N,K;
cin>>N>>K;
vector<int>x(N);
for(int i=0;i<N;i++){
cin>>x[i];
}
vector<int>last_set(30,INT_MIN);
vector<int>ans(N);
for(int i=0;i<N;i++){
for(int j=0;j<30;j++){
if((x[i]>>j)&1)last_set[j]=i;
if(i<=K+last_set[j])ans[i]|=(1<<j);
}
}
for(auto i:ans)cout<<i<<" ";

}

Second solution

n,k = map(int, raw_input().split())
arr = map(int, raw_input().split())

ans = [0 for __ in xrange(r)]
for b in xrange(30):
ds = -1
for i in xrange(n):
if arr[i]&1: ds = i+k
arr[i] >>= 1
ans[i] |= (i<=ds)<<b

print " ".join(map(str, ans))


Post a Comment

0 Comments