Question

Josefine is in charge of the local student organization at The University of Algorithms. The organization gets tasks they musInput (stdin)

7

N 4 5

N 9 6

N 16 3

R

N 12 8

R

R

Expected Output

9

12

4

Make it in java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We can follow a simple approach here, to solve this problem.

For a given input, there are two possibilities:

1. Whenever a new task is assigned to the organization, we simply store the unique id, unique difficulty values associated with the task, into our data structure. (input is: N id difficulty)

2. When a member is ready to do a task, we pick the task with highest difficulty value, from our data structure, print the id of that task onto the screen, and remove that task form our data structure. ( input is: R )

The data structure used here is TreeMap.

so, what is TreeMap?

TreeMap stores the data in key, value pair. Each record is stored in <key, value> format. It provides an efficient means of sorting the <key, value> pair, in sorted order.

For our solution, we need the difficulty value of the task to be sorted. so, I have used the difficulty value as the key, and the Task id as value.

Java code:

import java.util.Scanner; // To scan input from standard input(Key Board)
import java.util.TreeMap; // To use the Tree Map data structure
import java.util.Collections; //To sort the data in descending order of difficulty level.


public class Main {

public static void main(String[] args) {

/* create an object of TreeMap which stores 2 integers as key, value pairs. Difficulty value and Task id. Comparator is used to sort the key values in descending order. The default order is Ascending order. */


TreeMap<Integer, Integer> treemap = new TreeMap<Integer, Integer>(Collections.reverseOrder());
Scanner scanner = new Scanner(System.in).useDelimiter("\n"); //delimiter is used to scan input string along with spaces
int t = scanner.nextInt(); //scan number of inputs
while(t > 0) {
t--;
String input = scanner.next(); //scan the actual string
char c = input.charAt(0); //taking first character from input provided and checking for new task.

// if block is executed, when we wish to assign a difficult task to a user.
if(c == 'R') {   

if( treemap.isEmpty() ) // checking if there are any tasks present
System.out.println("No task is present to assign");

else {   
Integer op = treemap.firstKey(); // taking the difficult task, present on top, as TreeMap is sorted.
  Integer assignid = treemap.remove(op); // Removing task, as it is assigned to a person. It returns task id
System.out.println(assignid); // displaying the id.

}
}

// else block is executed, when a new task is given to organization
else {

  /* breaking the input when space is encountered. This gives us three strings. First String is "N". Second and Third Strings represent Task id and Task Difficulty level */
String[] splited = input.split("\\s+"); // split using regular expression
Integer id = Integer.valueOf(splited[1]); //Task id
Integer strength = Integer.valueOf(splited[2]); // task difficulty
treemap.put(strength, id); // inserting in treemap with difficulty as key, and Task id as value.
}
  
} // end of while loop
scanner.close();
} // end of main function
} //end of class

Output for the given input:

9

12

4

This code works fine, for any given input. There will be no problem with similar keys, because it is mentioned that unique task difficulty is present. This program also prints a message saying "No Task is present to assign" when there are no tasks present with the organization, to assign tasks to its members.

Add a comment
Know the answer?
Add Answer to:
Input (stdin) 7 N 4 5 N 9 6 N 16 3 R N 12 8 R R Expected Output 9 12 4 Make it in java
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16...

    C 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 QUESTION 23 10 points Using the toilet is important for people's independence, health and dignity. It can be difficult, however, to teach people to use the toilet when they are aggressive. This study describes how standard toilet training methods can be adapted to help a/3-year-old child with challenging behavior learn to use the toilet independently. Ben was...

  • TABLE 1 PRESENT VALUE OF $1 n 4% 5% 6% 7% 8% 9% 10% 12% 14%...

    TABLE 1 PRESENT VALUE OF $1 n 4% 5% 6% 7% 8% 9% 10% 12% 14% 16% 20% 1 0.961538 0.952381 0.943396 0.934579 0.925926 0.917431 0.909091 0.892857 0.877193 0.862069 0.833333 2 0.924556 0.907029 0.889996 0.873439 0.857339 0.841680 0.826446 0.797194 0.769468 0.743163 0.694444 3 0.888996 0.863838 0.8396190.816298 0.793832 0.772183 0.751315 0.711780 0.674972 0.640658 0.578704 4 0.854804 0.822702 0.792094 0.762895 0.735030 0.708425 0.683013 0.635518 0.592080 0.552291 0.482253 5 0.821927 0.783526 0.747258 0.712986 0.680583 0.649931 0.620921 0.567427 0.519369 0.476113 0.401878 6 0.790315 0.746215...

  • What an Executive Summary Is An executive summary is a specific type of document that does...

    What an Executive Summary Is An executive summary is a specific type of document that does two things: it summarizes a research article, and it offers recommendations as to how information from the article can be used. Some long reports can contain an executive summary section, as indicated in the Pearson handbook. Write a 2 pahe Executive Summary In business contexts, an executive summary is always written for a specific purpose: to explain the information in the article to a...

  • Using the book, write another paragraph or two: write 170 words: Q: Compare the assumptions of...

    Using the book, write another paragraph or two: write 170 words: Q: Compare the assumptions of physician-centered and collaborative communication. How is the caregiver’s role different in each model? How is the patient’s role different? Answer: Physical-centered communication involves the specialists taking control of the conversation. They decide on the topics of discussion and when to end the process. The patient responds to the issues raised by the caregiver and acts accordingly. On the other hand, Collaborative communication involves a...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

  • First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below...

    First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below Include each of the following in your answer (if applicable – explain in a paragraph) Research problem: what do you want to solve using Delphi? Sample: who will participate and why? (answer in 5 -10 sentences) Round one questionnaire: include 5 hypothetical questions you would like to ask Discuss: what are possible outcomes of the findings from your study? Hint: this is the conclusion....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT