Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 93 additions & 105 deletions Google_Hash_Code_2020.java
Original file line number Diff line number Diff line change
@@ -1,117 +1,105 @@
//This is my submission for the Google Hash Code 2020 Online Qualification Round. In fetched me a score of 15,435,636
//and a Global Rank of 5364 out of 10724 teams.
import java.util.*;

//A few modifiactions can be done to improve the code and score further.
// Class representing a Library
class Library {
int id;
int books;
int signup;
int booksPerDay;
ArrayList<Integer> bookIDs;

import java.util.*;
Library(int id, int books, int signup, int booksPerDay, ArrayList<Integer> bookIDs) {
this.id = id;
this.books = books;
this.signup = signup;
this.booksPerDay = booksPerDay;
this.bookIDs = bookIDs;
}
}

//class to store details of each library
class library{
int id; //unique Library ID
int books; //number of books in library
int signup; //Sign up time
int booksPerDay; //number of books that can be processed per day
ArrayList<Integer> bookIDs; //IDs of books in the library
library(int i,int b, int s, int bp, ArrayList<Integer> bIDs){
id = i;
books = b;
signup = s;
booksPerDay = bp;
bookIDs = bIDs;
// Comparator for sorting libraries by signup time (greedy heuristic)
class SortBySignup implements Comparator<Library> {
public int compare(Library a, Library b) {
return a.signup - b.signup;
}
}

class SortbySignup implements Comparator<library>
{
// Used for sorting in ascending order of library sign up time
public int compare(library a, library b)
{
return a.signup - b.signup;
}
}
public class hc {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

public class hc {
int totalBooks = sc.nextInt();
int numOfLib = sc.nextInt();
int totalDays = sc.nextInt();

int[] bookScore = new int[totalBooks];

for (int i = 0; i < totalBooks; i++) {
bookScore[i] = sc.nextInt();
}

// Store libraries
HashMap<Integer, Library> map = new HashMap<>();

for (int i = 0; i < numOfLib; i++) {

int books = sc.nextInt();
int signup = sc.nextInt();
int bpd = sc.nextInt();

ArrayList<Integer> bid = new ArrayList<>();

for (int j = 0; j < books; j++) {
bid.add(sc.nextInt());
}

map.put(i, new Library(i, books, signup, bpd, bid));
}

// Convert to list for sorting
ArrayList<Library> libs = new ArrayList<>(map.values());
libs.sort(new SortBySignup());

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int totalBooks = s.nextInt();
int numOfLib = s.nextInt();
int totalDays = s.nextInt(); //total number of days where libraries can sign-up and books can be proceesed
int cost[] = new int[totalBooks]; //to store cost of each book

for(int i=0;i<totalBooks;i++) cost[i] = s.nextInt();

//to store Library Info with it's Unique ID
HashMap<Integer,library> map = new HashMap<Integer,library>();

//storing information of all libraries
for(int i=0;i<numOfLib;i++){
int books = s.nextInt(); //num of books in library
int signup = s.nextInt(); //Sign-up time of library
int bpd = s.nextInt(); //number of books that can be processed per day

//to store book IDs of books in the library
ArrayList<Integer> bid = new ArrayList<Integer>();

for(int j=0;j<books;j++){
bid.add(s.nextInt());
}

library lib = new library(i,books, signup, bpd, bid);
map.put(i,lib);
}

//sorting libraries by sign-up time
ArrayList<library> sortedBySignup = new ArrayList(map.values());
Collections.sort(sortedBySignup, new SortbySignup());

//Map to store Library ID and the number of days when the books can be shipped
HashMap<Integer,Integer> map2 = new HashMap<Integer,Integer>();
int val = totalDays;
int i = 0;

//calculating shipment days for each library
while(i<numOfLib && val>0){
library ll = sortedBySignup.get(i);
int remDays = val - ll.signup;
int shipment = remDays*ll.booksPerDay;
map2.put(ll.id,shipment);
val-=ll.signup;
i++;
}

//store library IDs to be displayed on console
ArrayList<Integer> sol = new ArrayList<Integer>();
int cntLibs = 0;


for(Map.Entry<Integer,Integer> entry : map2.entrySet()){
int ID = entry.getKey();
int ship = entry.getValue();
if(ship>0){ //to check for a valid value
cntLibs++;
sol.add(ID);
}
}

//Printing solution
System.out.println(cntLibs); //number of libraries
for(int j=0;j<cntLibs;j++){
int ID = sol.get(j);
int shipBooks = map2.get(ID);
library llll = map.get(ID);
ArrayList<Integer> bid = llll.bookIDs;
shipBooks = Math.min(bid.size(),shipBooks);
System.out.println(ID+" "+shipBooks);

for(int k=0;k<shipBooks;k++){
System.out.print(bid.get(k)+" ");
}
System.out.println();
}
}
// Result storage
ArrayList<Integer> selectedLibs = new ArrayList<>();

int remainingDays = totalDays;

for (Library lib : libs) {

if (remainingDays <= lib.signup) break;

remainingDays -= lib.signup;

int shipCapacity = remainingDays * lib.booksPerDay;

if (shipCapacity > 0) {
selectedLibs.add(lib.id);
}
}

// Output
System.out.println(selectedLibs.size());

for (int id : selectedLibs) {

Library lib = map.get(id);

int remainingDaysAfterSignup = totalDays - lib.signup;
int shipCapacity = remainingDaysAfterSignup * lib.booksPerDay;

shipCapacity = Math.min(shipCapacity, lib.bookIDs.size());

System.out.println(id + " " + shipCapacity);

for (int k = 0; k < shipCapacity; k++) {
System.out.print(lib.bookIDs.get(k) + " ");
}
System.out.println();
}

sc.close();
}
}