Monday, December 6, 2021

USACO February 2019 Bronze Problem: Revegetate

/*
ID: karihee1
LANG: JAVA
TASK: revegetate
*/

/*  revegetate.java
*/

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

class revegetate {
    public static void main (String [] args) throws IOException {

        //BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //PrintWriter out = new PrintWriter(System.out);
        BufferedReader in = new BufferedReader(new FileReader("revegetate.in"));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("revegetate.out")));

        StringTokenizer st;
    
        st = new StringTokenizer(in.readLine());        
        int N = Integer.parseInt(st.nextToken());        // Pastures
        int M = Integer.parseInt(st.nextToken());        // Cows
        
        // System.out.println(N + " " + M);   // DEBUGGING PRINT STATEMENT

        ArrayList<ArrayList<Integer> > pastureList = new ArrayList<ArrayList<Integer>>(N);
        for (int i=0; i<N; i++) {
            ArrayList<Integer> temp = new ArrayList<Integer>();
            pastureList.add(temp);
        }

        for (int i=0;i<M;i++) {
            st = new StringTokenizer(in.readLine());
            int P1 = Integer.parseInt(st.nextToken());    // Pasture 1 for this cow 
            int P2 = Integer.parseInt(st.nextToken());    // Pasture 2 for this cow 
            
            // System.out.println(P1 + " " + P2);  // DEBUGGING PRINT STATEMENT

            pastureList.get(P1-1).add(P2-1);
            pastureList.get(P2-1).add(P1-1);
        }

        //for (int i=0;i<N; i++) {
        //    System.out.println(pastureList.get(i).toString()); // DEBUGGING PRINT STATEMENT
        //}

        // Input Data Loaded...Begin Planting Seed

        int[] answer = new int[N];
        answer[0]=1; //initialize with seed type 1 in pasture 0, can't be conflict yet.

        ArrayList<Integer> work = new ArrayList<Integer>();
        for (int i=1;i<N;i++) {
            for (int j=1;j<=4;j++) {
                // attempt to place seed j in pasture i, check for conflict
                boolean conflict = false;
                work = pastureList.get(i);

                for (int p = 0; p < work.size(); p++) {
                    if (j==answer[work.get(p)]) {
                        conflict = true;
                        break;
                    }
                }
                if (conflict) continue;  // conflict found for seed type j, try next seed type
                else {
                    answer[i]=j;         // success!
                    break;
                } 
            }
        }

		for (int i=0; i<N; i++) {
            out.print(answer[i]);
        }
		out.println();
        out.close();
    }
}

Sunday, November 7, 2021

USACO "Guess The Animal" Java Solution

/*
* Solution to 2019 USACO January Bronze Problem: Guess the Animal
*/

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

class guess {

    public static void main (String [] args) throws IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        StringTokenizer st;

        st = new StringTokenizer(in.readLine());  
        int N = Integer.parseInt(st.nextToken());
        // out.println(N);  // DEBUG PRINT
        
        ArrayList<HashSet<String>> list = new ArrayList<HashSet<String>>();

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

            st = new StringTokenizer(in.readLine());   
			String name = st.nextToken();
            int nqualities = Integer.parseInt(st.nextToken());

            // out.println(name + " " + nqualities); // DEBUG PRINT
            HashSet<String> tempset = new HashSet<String>();

			for (int j=0; j<nqualities; j++) {
			    tempset.add(st.nextToken());	
		    }
            list.add(tempset);
        }

        // out.println(list.toString()); // DEBUG PRINT
        int maxoverlap = 0;

        for (int i=0; i<N; i++) {
            for (int j=i+1; j<N; j++) {
                // make a copy of the HashSet at list.get(i) 
                //     because retainAll is destructive, 
                //         want to keep list intact
                HashSet<String> intersection = new HashSet<String>(list.get(i));
                intersection.retainAll(list.get(j));
                int overlap = intersection.size();
                if (overlap>maxoverlap) {
                    maxoverlap = overlap;
                }
                // out.println(i + " " + j + " " + intersection.toString() + " " + overlap); // DEBUG PRINT
            }
        }
        int answer = maxoverlap+1;
        out.println(answer); 
        // out.println(list.toString()); // DEBUG PRINT
        out.close();
    }
}

Sunday, October 24, 2021

Custom Object ArrayList Sort milk.java

/*
ID: karihee1
LANG: JAVA
TASK: milk
*/

/*  milk.java
*
*  This is a solution to the 10th USACO Training Problem "Mixing Milk".  
*  A copy of the problem statement can be found here:  https://jvonk.github.io/usaco/2018/09/29/milk.html
*
*  The solution below demonstrates sorting an ArrayList containing a custom class
*
*  NOTE: since USACO Trainiing Problems still uses files for input and output,
*  I have replaced the BufferedReader and PrintWriter lines that use stdin and stdout.
*
* The file "milk.in" looks like this:
* 100 5 
* 5 20 
* 9 40 
* 3 10 
* 8 80 
* 6 30
*/

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

class milk {
    public static void main (String [] args) throws IOException {

        //BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //PrintWriter out = new PrintWriter(System.out);
        BufferedReader in = new BufferedReader(new FileReader("milk.in"));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("milk.out")));

        StringTokenizer st;
    
        st = new StringTokenizer(in.readLine());        
        int N = Integer.parseInt(st.nextToken());        // Milk Demand (0 <= N <= 2,000,000)
        int M = Integer.parseInt(st.nextToken());        // Number of farmers to buy from (0 <= M <= 5,000)
        
        // out.println(N + " " + M);   // DEBUGGING PRINT STATEMENT
    
        ArrayList<Farmer> al = new ArrayList<Farmer>(M);
    
        for (int i=0;i<M;i++) {
            st = new StringTokenizer(in.readLine());
            int P = Integer.parseInt(st.nextToken());    // Price
            int A = Integer.parseInt(st.nextToken());    // Amount
            
            // out.println(P + " " + A);  // DEBUGGING PRINT STATEMENT
            
            al.add(new Farmer(P,A));
        }
    
        Collections.sort(al);
        
        Farmer f;
        int answer=0;
        
        for (int i=0;i<M; i++) {
            f = al.get(i);
            
            //out.println(f.price + " " + f.amount);  // DEBUGGING PRINT STATEMENT
            
            if ((N>0) && (f.amount>0)) {              // We want to buy from this farmer!
                if (N>=f.amount) {
                    answer += f.amount * f.price;     // buy all from this farmer
                    N -= f.amount;
                } else {
                    answer += N*f.price;              // buy some from this farmer -- demand fulfilled
                    N = 0;
                }
            }
        }
        out.println(answer);
        out.close();
    }
}

class Farmer implements Comparable<Farmer> {
    public int price;
    public int amount;
 
    public Farmer(int price,int amount) {
        this.price = price;
        this.amount = amount;
    }
    @Override
    public int compareTo(Farmer obj) {
    // sort in ascending order.  
    // compareTo returns a negative integer, zero, or a positive integer as 
    // this object is less than, equal to, or greater than the specified object. 
        if (this.price<obj.price) {
            return -1;            
        } else if (this.price==obj.price) {
            return 0;             
        } else {
            return 1;              
        }
    }
}

Sunday, October 10, 2021

Buffered Input IOTEST.java

/*  IOTEST.java
*
* After compiling, run this program like this:  
* java IOTEST < IOTEST.in
*
* Where IOTEST.in looks like this:
11
222222222222
3.333
COWABUNGA
A B C D E F G
HIJKLMNOPQRSTUVWXYZ
*
*/

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

class IOTEST {
    public static void main (String [] args) throws IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        StringTokenizer st;
    
        st = new StringTokenizer(in.readLine());         // read first line from input
        int a = Integer.parseInt(st.nextToken());        // parse integer (32-bit) from line
    
        st = new StringTokenizer(in.readLine());         // read second line
        long b = Long.parseLong(st.nextToken());         // parse long integer (64-bit) from line
    
        st = new StringTokenizer(in.readLine());         // read third line
        double c = Double.parseDouble(st.nextToken());   // parse double floating-point decimal (64-bit)
    
        st = new StringTokenizer(in.readLine());         // read fourth line
        String d = st.nextToken();                       // nextToken is already a String
    
        st = new StringTokenizer(in.readLine());         // read fifth line
        char[] arr1 = new char[7];
        for (int i=0;i<7;i++) {                          // loop through tokens on this line of input
            arr1[i] = (st.nextToken()).charAt(0);        // take first character from nextToken String, assign to array element
        }
    
        st = new StringTokenizer(in.readLine());         // read sixth line
        char[] arr2 = (st.nextToken()).toCharArray();    // break nextToken String into array of chars
    
        out.println(a);
        out.println(b);
        out.println(c);
        out.println(d);
        out.println(Arrays.toString(arr1));              // example array output
        out.println(Arrays.toString(arr2));              // good for debugging
        out.close();
    }
}