Wednesday 31 December 2014

Cooling Pies Codechef Problem

Cooling Pies[Java Program]


The chef has just finished baking several pies, and it's time to place them on cooling racks.
The chef has exactly as many cooling racks as pies. Each cooling rack can only hold one pie, and each pie may only be held by one cooling rack,
but the chef isn't confident that the cooling racks can support the weight of the pies.
The chef knows the weight of each pie,
and has assigned each cooling rack a maximum weight limit.
What is the maximum number of pies the chef can cool on the racks?

Input:

Input begins with an integer T≤30, the number of test cases.
Each test case consists of 3 lines.
The first line of each test case contains a positive integer N≤30,
the number of pies (and also the number of racks).
The second and third lines each contain exactly positive N integers not exceeding 100.
The integers on the second line are the weights of the pies, and the integers on the third line
are the weight limits of the cooling racks.

Output:

For each test case, output on a line the maximum number of pies the chef can place on the racks.

Sample input:

2
3
10 30 20
30 10 20
5
9 7 16 4 8
8 3 14 10 10
 

Sample output:

3
4

Program:-

/** * @(#)CoolP.java * * * @author Suyash Bhalla * @version 1.00 2014/12/31 */ import java.util.Arrays; import java.io.*; class CoolP { public static void main(String ar[])throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int T=Integer.parseInt(br.readLine()); while(T--!=0){ int n=Integer.parseInt(br.readLine()); String t1[]=br.readLine().split(" "); String t2[]=br.readLine().split(" "); int a[]=new int[n]; int b[]=new int[n]; for(int i=0;i<n;i++){ a[i]=Integer.parseInt(t1[i]); b[i]=Integer.parseInt(t2[i]); } int res=0; Arrays.sort(a); Arrays.sort(b); for(int i=n-1;i>=0;i--){ for(int j=0;j<n;j++){ if(a[i]<=b[j]&&b[j]!=-1){ res++; b[j]=-1; break; } } } System.out.println(res); } } }

---------------X----------------X-----------------X-----------------X-----------------X------------------X----------------------

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home