Skip to content

Instantly share code, notes, and snippets.

@rfaisal
Created September 8, 2013 23:31
Show Gist options
  • Select an option

  • Save rfaisal/6489532 to your computer and use it in GitHub Desktop.

Select an option

Save rfaisal/6489532 to your computer and use it in GitHub Desktop.
Little Elephant likes lemonade. When Little Elephant visits any room, he finds the bottle of the lemonade in that room that contains the greatest number of litres of lemonade and drinks it all. There are n rooms (numbered from 0 to n-1), each contains Ci bottles. Each bottle has a volume (in litres). The first room visited by Little Elephant was…
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
public class LittleElephantLemonade {
private int drunkVolume;
private ArrayList<PriorityQueue<Integer>> rooms;
public LittleElephantLemonade() {
drunkVolume=0;
rooms=new ArrayList<PriorityQueue<Integer>>();
}
public int getDrunkVolume(){
return drunkVolume;
}
public void populateRoom(int roomIndex, ArrayList<Integer>volumes){
PriorityQueue<Integer> pq=new PriorityQueue<Integer>(volumes.size(),new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
pq.addAll(volumes);
rooms.add(roomIndex, pq);
}
public void visitRoom(int roomIndex){
if(!rooms.get(roomIndex).isEmpty())
drunkVolume+=rooms.get(roomIndex).poll();
}
public static void main(String[] args) {
Scanner scanner = new Scanner( System.in );
int t=scanner.nextInt();
for(int i=0;i<t;i++){
LittleElephantLemonade lel= new LittleElephantLemonade();
int n=scanner.nextInt();
int k=scanner.nextInt();
int []visits= new int[k];
for(int j=0;j<k;j++){
visits[j]=scanner.nextInt();
}
for(int j=0;j<n;j++){
ArrayList<Integer> volumes= new ArrayList<Integer>();
int c=scanner.nextInt();
for(int l=0;l<c;l++)
volumes.add(scanner.nextInt());
lel.populateRoom(j, volumes);
}
for(int j=0;j<k;j++){
lel.visitRoom(visits[j]);
}
System.out.println(lel.getDrunkVolume());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment