Sunday, 27 July 2014

UVa 10141 - Request for Proposal

/*
 * @author Divyanshu
 * @mail divyanshu17x@gmail.com
 * @place GNDU RC SATHIALA
 * @date 27 / 07 / 2014
 * @judge uva.onlinejudge.org
 * @verdict Accepted
 * @problem 10141 - Request for Proposal
 * @problemType  Linear Scanning
 */


import java.io.*;
import java.util.*;
 public class  Main {
     public static void main(String args[]) throws IOException{
            Reader.init(System.in);
          
            int noOfRequirements = Reader.nextInt();
            int noOfProposals  = Reader.nextInt();
          
            //loop through all proposals
            for(int c = 1 ; noOfRequirements > 0 ; ++c)
            {
                //array of proposal name s
                String proposalName[] =  new String[noOfProposals];
              
                //array of each proposal's prices
                double proposalPrice[] = new double[noOfProposals];
              
                //array of number of requirements met by each proposal
                int metRequirements[] = new int[noOfProposals];
              
                // read the requirements name for compliance required
                for(int i = 0 ; i < noOfRequirements ; ++i)
                {
                    Reader.reader.readLine();
                }
              
                //variables for showing result
                int bestIndex = 0 ;
                int maxRequirementPresent = -1 ;
          
                //read the specs for each proposal
                for(int k = 0 ; k < noOfProposals ; ++k)
                {
                    // add the value of proposal name , price and total
                     // requirements met for each proposal
                    proposalName[k] = Reader.reader.readLine();
                    proposalPrice[k] = Reader.nextDouble();
                    int currLength =  metRequirements[k] =  Reader.nextInt();
              
                    //read the names of each requirement for current proposal
                    for(int i = 0 ; i < currLength ; ++i )
                    {
                        Reader.reader.readLine();
                    }
              
                    // evaluate the compliance
              
                    //since compliance is directly proportional to requirement provided 
                      //by any proposal x
                    //so assighn bestIndex to index of that proposal in such a case
                    if(  currLength  > maxRequirementPresent )
                    {
                        maxRequirementPresent =  currLength  ;
                        bestIndex = k;
                }  
              
                    //else if present proposal is equal to best compliance in previous case ,
                    //select it if it has least price then previous best proposal
                    else if( currLength == maxRequirementPresent && 
                     proposalPrice[k] < proposalPrice[bestIndex]   )
                    {
                        bestIndex = k;
                    }  
              
                }
                System.out.println("RFP #" + c);
                System.out.println(proposalName[bestIndex]);
                noOfRequirements = Reader.nextInt();
                noOfProposals = Reader.nextInt();
                if(noOfRequirements != 0) System.out.println("");
              
            }
       }
    
 }



     /** Class for buffered reading int and double values */
     /** http://www.cpe.ku.ac.th/~jim/java-io.html */
     class Reader {
          static BufferedReader reader;
          static StringTokenizer tokenizer;
          /** call this method to initialize reader for InputStream */
          static void init(InputStream input) {
               reader = new BufferedReader(new InputStreamReader(input));
               tokenizer = new StringTokenizer("");
          }
          /** get next word */
          static String next() throws IOException {
               while (!tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
               }
               return tokenizer.nextToken();
          }
          static int nextInt() throws IOException {
               return Integer.parseInt(next());
          }
          static double nextDouble() throws IOException {
               return Double.parseDouble(next());
          }
     }

No comments:

Post a Comment