2.Information is present in the screenshot and below. Based on that need help in solving the code for this problem in Java. The time complexity has to be as less as possible Apply greedy algorithm or divide and conquer in the problem. Make sure all test cases return expected outputs. Also please provide screenshot of the code actually displaying the outputs with the respective inputs.   The actual incomplete code import java.io.*; import java.util.*; public class Solution {     public static int r;     public static long tp;     public static void main(String[] args) throws Exception {         BufferedReader br = new BufferedReader(             new InputStreamReader(System.in)         );         String[] parts = br.readLine().trim().split(" ");         int n = Integer.parseInt(parts[0]);         int k = Integer.parseInt(parts[1]);         Card[] cards = new Card[n];         for(int i = 0; i < n; i++) {             parts = br.readLine().trim().split(" ");             int power = Integer.parseInt(parts[1]);             cards[i] = new Card(parts[0],power);         }         solve(n,k,cards);         System.out.printf("%d %d",r, tp);     }     /*     Solves a test case.     Parameters:     n : int - number of cards available     k : int - number of cards to include in Jason's deck     cards : array-like - string list of shape (n,2). Each element has two elements:      the first is a string containing the color (either "red" or "blue"). The second      is a string containing the power of the card.     Save the answers to the static variables r and tp where:     r  : int - number of red cards added     tp : int - total power of cards in your deck     */     public static void solve(int n, int k, Card[] cards) {         // TODO     } }    class Card {     public String color;     public int power;     public Card (String color, int power) {         this.color = color;         this.power = power;     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

2.Information is present in the screenshot and below. Based on that need help in solving the code for this problem in Java. The time complexity has to be as less as possible Apply greedy algorithm or divide and conquer in the problem. Make sure all test cases return expected outputs. Also please provide screenshot of the code actually displaying the outputs with the respective inputs.

 

The actual incomplete code

import java.io.*;
import java.util.*;

public class Solution {
    public static int r;
    public static long tp;

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in)
        );

        String[] parts = br.readLine().trim().split(" ");
        int n = Integer.parseInt(parts[0]);
        int k = Integer.parseInt(parts[1]);
        Card[] cards = new Card[n];

        for(int i = 0; i < n; i++) {
            parts = br.readLine().trim().split(" ");
            int power = Integer.parseInt(parts[1]);
            cards[i] = new Card(parts[0],power);
        }

        solve(n,k,cards);
        System.out.printf("%d %d",r, tp);
    }

    /*
    Solves a test case.

    Parameters:
    n : int - number of cards available
    k : int - number of cards to include in Jason's deck
    cards : array-like - string list of shape (n,2). Each element has two elements: 
    the first is a string containing the color (either "red" or "blue"). The second 
    is a string containing the power of the card.

    Save the answers to the static variables r and tp where:
    r  : int - number of red cards added
    tp : int - total power of cards in your deck
    */
    public static void solve(int n, int k, Card[] cards) {
        // TODO
    }
}   

class Card {
    public String color;
    public int power;

    public Card (String color, int power) {
        this.color = color;
        this.power = power;
    }
}





 

 

 

 

Jason is on the playtesting team of a new trading card game called Logic: The Unsundering. The game is in the
early prototype stages. As a result, only two major card colors have been implemented: red and blue. Red is
focused primarily on offense and resource maintenance. Blue is mainly focused on defense and board
control.
Each card belongs to one of the two colors and has a power value P₁. Unfortunately, the balancing is
imperfect; red cards are more powerful than blue cards. This is quite unfortunate, but Jason intends to make
the most out of this. If he can build a deck that dominates all other decks, then Jason can show the game
designers this crucial flaw in the game design.
Jason has N cards, each with a color C; and power P₁. He wants to build the strongest deck possible. Out of
all the possible decks, one deck is deemed stronger than another if it has more red cards. If two decks have
the same amount of red cards, the deck with a higher total P across all the cards is more powerful. Jason has
to build a deck containing K cards. Can you help Jason build the strongest deck?
Input Format
Input begins with a line containing two space-separated integers N and K, indicating the number of cards
available and the number of cards allowed in a deck respectively.
N lines follow, each containing two space-separated values C and P; that describe one card's color and
power respectively. C is a string with a value of either "red" or "blue" (without quotes). P; is an integer.
Constraints
1≤K≤N≤3.105
C₂ € {red, blue}
0 ≤ P ≤ 10⁹
Output Format
Output contains two space-separated integers R and TP. R is the number of red cards in Jason's deck. TP is
the total P₁ of the cards in the deck.
Transcribed Image Text:Jason is on the playtesting team of a new trading card game called Logic: The Unsundering. The game is in the early prototype stages. As a result, only two major card colors have been implemented: red and blue. Red is focused primarily on offense and resource maintenance. Blue is mainly focused on defense and board control. Each card belongs to one of the two colors and has a power value P₁. Unfortunately, the balancing is imperfect; red cards are more powerful than blue cards. This is quite unfortunate, but Jason intends to make the most out of this. If he can build a deck that dominates all other decks, then Jason can show the game designers this crucial flaw in the game design. Jason has N cards, each with a color C; and power P₁. He wants to build the strongest deck possible. Out of all the possible decks, one deck is deemed stronger than another if it has more red cards. If two decks have the same amount of red cards, the deck with a higher total P across all the cards is more powerful. Jason has to build a deck containing K cards. Can you help Jason build the strongest deck? Input Format Input begins with a line containing two space-separated integers N and K, indicating the number of cards available and the number of cards allowed in a deck respectively. N lines follow, each containing two space-separated values C and P; that describe one card's color and power respectively. C is a string with a value of either "red" or "blue" (without quotes). P; is an integer. Constraints 1≤K≤N≤3.105 C₂ € {red, blue} 0 ≤ P ≤ 10⁹ Output Format Output contains two space-separated integers R and TP. R is the number of red cards in Jason's deck. TP is the total P₁ of the cards in the deck.
Sample Input 0
53
red 1
red 1
blue 5
blue 7
blue 9
Sample Output 0
2 11
Sample Input 1
63
blue 13
blue 8
blue 2
blue 6
red 6
blue 2
Sample Output 1
1 27
Sample Input 2
74
red 4
blue 6
red 3
red 12
red 8
blue 1
red 3
Sample Output 2
4 27
Transcribed Image Text:Sample Input 0 53 red 1 red 1 blue 5 blue 7 blue 9 Sample Output 0 2 11 Sample Input 1 63 blue 13 blue 8 blue 2 blue 6 red 6 blue 2 Sample Output 1 1 27 Sample Input 2 74 red 4 blue 6 red 3 red 12 red 8 blue 1 red 3 Sample Output 2 4 27
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Top down approach design
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education