Sunday, December 1, 2013

USACO - Section 1.1 - Friday


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/*
 ID: akshika1
 LANG: JAVA
 TASK: friday
 */
 class friday {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("friday.in"));
        PrintWriter pw = new PrintWriter(new File("friday.out"));

        int N = sc.nextInt();
        int[] days = new int[7];
        int firstday = 0;                 //0= monday, 1=tuesday....6=sunday
        int thirteenth;
        for (int i = 0; i <N; i++) {
           
            //jan
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+31)%7;
           
            if((i+1900)%400==0)                  // a leap year
            {                                    //february
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+29)%7;
           
            }
            else if((i+1900)%4==0 && (i+1900)%100!=0)               //lead year
            {
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+29)%7;
           
            }
            else   //just a noraml year
            {
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+28)%7;
           
            }
            //March 31
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+31)%7;
           
            //April 30
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+30)%7;
           
            //may 31
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+31)%7;
           
            //june 30
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+30)%7;
           
            //july 31
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+31)%7;
           
            //august 31
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+31)%7;
           
            //sep 30
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+30)%7;
           
            //oct 31
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+31)%7;
           
            //novem 30
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+30)%7;
           
            //decme 31
            thirteenth =(firstday + 12)%7;  
            days[thirteenth]++;
            firstday  = (firstday+31)%7;
           
           
        }
     
        pw.println(days[5]+" "+days[6]+" "+days[0]+" "+days[1]+" "+days[2]+" "+ days[3]+" "+days[4]);
        pw.close();
             
    }
}

USACO Section 1.1 - Gift

/*
 ID: akshika1
 LANG: JAVA
 TASK: gift1
 */

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

class gift1 {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("gift1.in"));
        PrintWriter pw = new PrintWriter(new File("gift1.out"));

        int times = sc.nextInt();
        sc.nextLine();  //just to go to the next line
        String[] names = new String[times];
        int[] original = new int[times];
     
        for (int i = 0; i < times; i++) {
            original[i] = -1;
        }
        int[] values = new int[times];
        int value;
        int division;
        int temptimes = times;
        int moneyToBeGiven = 0;
        String giver;

        for (int i = 0; i < times; i++)
       {
             names[i] = sc.nextLine();
        }

        for (int j = 0; j < times; j++) {
            giver = sc.nextLine();
            String t = sc.nextLine();
            String[] temp = new String[2];
            temp = t.split(" ");
            value = Integer.parseInt(temp[0]);
            division = Integer.parseInt(temp[1]);
            if (division == 0) {
                moneyToBeGiven = 0;
            } else {
                moneyToBeGiven = (value - (value % division)) / division;;
            }
       String[] tempArray = new String[division];
       for (int i = 0; i < division; i++) {
             tempArray[i] = sc.nextLine();
       }

        for (int k = 0; k < names.length; k++) {
              if (giver.equals(names[k])) {
                  if (original[k] == -1) {
                      original[k] = value;
                   }
                    values[k] += value;
                    values[k] -= (division * moneyToBeGiven);
                    break;
                }
            }
            for (int i = 0; i < tempArray.length; i++) {
                for (int z = 0; z < names.length; z++) {
                    if (names[z].equals(tempArray[i])) {
                        values[z] += moneyToBeGiven;
                    }
                }
            }

        }







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

            pw.println(names[i] + " " + (values[i] - original[i]));

        }

        pw.close();
        System.exit(0);
    }
}