Two players are playing the game of deletion. P l a y e r 1 plays with array A and P l a y e r 2 plays with array B. The length of both the arrays is N.
The game is like this - both of them want to delete their respective arrays. A player can delete the array in multiple steps.
During one step, he chooses some numbers from the array and takes the bitwise OR of these numbers, and removes these numbers from the array.
This bitwise OR is the cost incurred in deleting the selected numbers. This way he deletes his whole array. The reward of deleting the entire array will be the sum of all numbers in the array minus the sum of the cost of all the steps.
The player with the maximum reward will win.
You have to print the winner along with the of reward, he wins with. Let's say scored and scored and wins, then the margin will be .
Example:
Input: n = 3, a = [1, 2, 1], b = [1, 2, 3]
Output: 2 2
Approach
C++
#include <bits/stdc++.h>using namespace std;void gameOfDeletion(int n, int a[], int b[]){int suma = 0, sumb = 0, costa = 0, costb = 0;for (int i = 0; i < n; i++){suma += a[i];costa = costa | a[i];}for (int i = 0; i < n; i++){sumb += b[i];costb = costb | b[i];}int rewarda = suma - costa;int rewardb = sumb - costb;if (rewarda > rewardb)cout << 1 << " " << rewarda - rewardb << "\n";elsecout << 2 << " " << rewardb - rewarda << "\n";}int main(){int n = 3;int a[n] = {1, 2, 1}, b[n] = {1, 2, 3};gameOfDeletion(n, a, b);return 0;}
No comments:
Post a Comment