알고리즘 단련장/해커랭크

[해커랭크] Fun Game

snapcoder 2025. 8. 7. 00:40
728x90
반응형
SMALL

https://www.hackerrank.com/challenges/fun-game-1/problem?isFullScreen=true

 

Kyle and Mike are bored on a rainy day and decide to pass the time by creating a new game having the following rules:

  • The game starts with two -sized integer arrays,  and , and is played by two players,  and .
  • The players move in alternating turns, with  always moving first. During each move, the current player must choose an integer, , such that . If the current player is , then  receives  points; if the current player is , then  receives  points.
  • Each value of  can be chosen only once. That is, if a value of  is already chosen by some player, none of the player can re-use it. So, game always ends after  moves.
  • The player with the maximum number of points wins.
  • The arrays A and B are accessible to both the players P1 and P2. So the players make a optimal move at every turn.

Given the values of , , and , can you determine the outcome of the game? Print  if  will win,  if  will win, or  if they will tie. Assume both players always move optimally.

Input Format

The first line of input contains a single integer, , denoting the number of test cases. Each of the  subsequent lines describes a test case. A single test case is defined over the following three lines:

  1. An integer, , denoting the number of elements in arrays  and .
  2.  space-separated integers, , where each  describes the element at index  of array .
  3.  space-separated integers, , where each  describes the element at index  of array .

Constraints

  •  
  •  
  •  

Output Format

For each test case, print one of the following predicted outcomes of the game on a new line:

  • Print  if  will win.
  • Print  if  will win.
  • Print  if the two players will tie.

Sample Input

3
3
1 3 4
5 3 1
2
1 1
1 1
2
2 2
3 3

Sample Output

First
Tie
Second

Explanation

Test Case 0: ,  The players make the following  moves:

  1.  chooses  and receives  points.
  2.  chooses  and receives  points. Note that  will not choose , because this would cause  to win.
  3.  chooses  (which is the only remaining move) and receives  points.

As all  moves have been made, the game ends. 's score is  points and 's score is  points, so  is the winner and we print  on a new line.

Test Case 1: ,  Because both players will only make  move and all possible point values are , the players will end the game with equal scores. Thus, we print  on a new line.

Test Case 1: , 
Because both players will only make  move and all the possible point values for  are greater than all the possible point values for ,  will win the game. Thus, we print  on a new line.

 

 

 

 

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'funGame' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY a
     *  2. INTEGER_ARRAY b
     */

    public static String funGame(List<Integer> a, List<Integer> b) throws IOException {
    // Write your code here
        int size = a.size();
        int[][] m = new int[size][3];
        for(int i=0; i<size; i++){
            m[i][0] = a.get(i);
            m[i][1] = b.get(i);
            m[i][2] = a.get(i) + b.get(i);
        }
       
        Arrays.sort(m, (x, y) -> {
            if (y[2] != x[2]) {
                return y[2] - x[2];
            } else {
                return y[0] - x[0];
            }
        });
       
        for(int i=0; i<size; i++){
            // System.out.println(String.format("%d : %d", m[i][0], m[i][1]));
        }
       
        int aScore = 0;
        int bScore = 0;
        for (int i = 0; i < size; i++) {
            if (i % 2 == 0) {
                aScore += m[i][0]; // P1's turn, take A
            } else {
                bScore += m[i][1]; // P2's turn, take B
            }
        }
       
        // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String str;
        if(aScore > bScore){
            str = "First";
        }
        else if(aScore < bScore){
            str = "Second";
        }
        else{
            str = "Tie";
        }
        // bw.close();
        return str;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int t = Integer.parseInt(bufferedReader.readLine().trim());

        IntStream.range(0, t).forEach(tItr -> {
            try {
                int n = Integer.parseInt(bufferedReader.readLine().trim());

                List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                    .map(Integer::parseInt)
                    .collect(toList());

                List<Integer> b = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                    .map(Integer::parseInt)
                    .collect(toList());

                String result = Result.funGame(a, b);

                bufferedWriter.write(result);
                bufferedWriter.newLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });

        bufferedReader.close();
        bufferedWriter.close();
    }
}

 

 

728x90
반응형
LIST