My flatmate, Sayan, once invited all his relatives and friends from
all over the city to join him on his birthday party.
He is also famous for boasting that all his friends and relatives belong
to "proper" families.
Now, he appointed a gaurd to allow only his relatives and friends into
the party.
It is gauranteed that all the relatives and friends come with their
families all at once ,i.e, all members belonging to the same family
reach all at once.
Now gaurd's task is to count the number of "proper" families who came in
the party.
All families have a unique "special number" assigned to them ,i.e., all
members of the same family share the same "special number".
A family is said to be proper only if the number of members in the
familiy is a number which can be made by adding numbers from a given set
of "valid" numbers N. Further constraint is that each valid number can
be used only once.
Take an example:-
if N={1,2,8} then all the families with 1,2,3,8,9,10 , or 11 members are allowed in the party , where as families with 4,5,6,7, etc members are not allowed.
The gaurd being a lazy fellow came to you for help.You need to write a program that computes the number of "proper families" in who turned up.
INPUT.
First line of input contains a number T denoting the number of test cases.T test cases follow. In each test case, the first line contains the number(P) of total number of people coming for the party and the number (M) denoting the number of "valid" numbers. The 2nd line contains M numbers, the set N ,containing all distinct "valid" numbers separated with spaces. The 3rd line contains P numbers, the "special" number of the members separated with spaces.
OUTPUT.
Output a single line for each of the test cases that denotes the number of "proper families" who turned up for the party.
CONSTRAINTS:
1<=T<=5
1<=P,N,M<=1000
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class BirthdayParty {
static PrintWriter out = new PrintWriter(System.out);
static InputReader Reader = new InputReader(System.in);
public static void main(String[] args) {
int numTests = Reader.nextInt();
Map<Integer, Integer> familyMemCount = new HashMap<Integer, Integer>();
for (int index = 0; index < numTests; index++) {
familyMemCount.clear();
int NumPeople = Reader.nextInt();
int NumSpecialNumbers = Reader.nextInt();
int[] splNumbers = new int[NumSpecialNumbers];
for (int i = 0; i < splNumbers.length; i++) {
splNumbers[i] = Reader.nextInt();
}
for (int i = 0; i < NumPeople; i++) {
int spl = Reader.nextInt();
if (familyMemCount.containsKey(spl)) {
int val = familyMemCount.get(spl);
familyMemCount.put(spl, ++val);
} else {
familyMemCount.put(spl, 1);
}
}
int properFamily = 0;
for (Iterator<Map.Entry<Integer, Integer>> it = familyMemCount.entrySet().iterator(); it.hasNext();) {
Map.Entry<Integer, Integer> entry = it.next();
if (isSubSetSum(splNumbers, NumSpecialNumbers - 1, entry.getValue())) {
properFamily++;
}
}
out.println(properFamily);
out.flush();
}
out.close();
}
/**
*
* @param i
* @param i0
* @param value
* @return
*/
private static boolean isSubSetSum(int[] array, int index, Integer sum) {
// Base Cases
if (sum == 0) {
return true;
}
if(sum < 0) {
return false;
}
if (index < 0 && sum != 0) {
return false;
}
return isSubSetSum(array, index - 1, sum) || isSubSetSum(array, index - 1, sum - array[index]);
}
/** Class for buffered reading int and double values */
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
Take an example:-
if N={1,2,8} then all the families with 1,2,3,8,9,10 , or 11 members are allowed in the party , where as families with 4,5,6,7, etc members are not allowed.
The gaurd being a lazy fellow came to you for help.You need to write a program that computes the number of "proper families" in who turned up.
INPUT.
First line of input contains a number T denoting the number of test cases.T test cases follow. In each test case, the first line contains the number(P) of total number of people coming for the party and the number (M) denoting the number of "valid" numbers. The 2nd line contains M numbers, the set N ,containing all distinct "valid" numbers separated with spaces. The 3rd line contains P numbers, the "special" number of the members separated with spaces.
OUTPUT.
Output a single line for each of the test cases that denotes the number of "proper families" who turned up for the party.
CONSTRAINTS:
1<=T<=5
1<=P,N,M<=1000
Sample Input
2 8 3 1 2 8 2 2 2 3 4 4 4 4 10 3 2 4 6 2 9 9 9 19 19 19 19 19 20
Sample Output
2 0
Explanation
In the first case:
Clearly, The families with 1,2,3,8,9,10 and 11 members are allowed in the party.
The family with "Special" number 2 has 3 members in it which is allowed.Same for the family with "special" number 3.
But for the family with "special" number 4 has 4 members, which is not allowed.
So the total number of "proper" families = 2.
Time Limit:
3 sec(s)
for all input files combined.
Memory Limit:
256 MB
Source Limit:
1024 KB
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class BirthdayParty {
static PrintWriter out = new PrintWriter(System.out);
static InputReader Reader = new InputReader(System.in);
public static void main(String[] args) {
int numTests = Reader.nextInt();
Map<Integer, Integer> familyMemCount = new HashMap<Integer, Integer>();
for (int index = 0; index < numTests; index++) {
familyMemCount.clear();
int NumPeople = Reader.nextInt();
int NumSpecialNumbers = Reader.nextInt();
int[] splNumbers = new int[NumSpecialNumbers];
for (int i = 0; i < splNumbers.length; i++) {
splNumbers[i] = Reader.nextInt();
}
for (int i = 0; i < NumPeople; i++) {
int spl = Reader.nextInt();
if (familyMemCount.containsKey(spl)) {
int val = familyMemCount.get(spl);
familyMemCount.put(spl, ++val);
} else {
familyMemCount.put(spl, 1);
}
}
int properFamily = 0;
for (Iterator<Map.Entry<Integer, Integer>> it = familyMemCount.entrySet().iterator(); it.hasNext();) {
Map.Entry<Integer, Integer> entry = it.next();
if (isSubSetSum(splNumbers, NumSpecialNumbers - 1, entry.getValue())) {
properFamily++;
}
}
out.println(properFamily);
out.flush();
}
out.close();
}
/**
*
* @param i
* @param i0
* @param value
* @return
*/
private static boolean isSubSetSum(int[] array, int index, Integer sum) {
// Base Cases
if (sum == 0) {
return true;
}
if(sum < 0) {
return false;
}
if (index < 0 && sum != 0) {
return false;
}
return isSubSetSum(array, index - 1, sum) || isSubSetSum(array, index - 1, sum - array[index]);
}
/** Class for buffered reading int and double values */
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
0 comments:
Post a Comment