Saturday, 11 February 2012

Codechef -> Practice -> Easy -> DiscrepanciesInTheVotersList



Solution : 


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 *
 * @author XCoder
 */
class DiscrepanciesInTheVotersList {

    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter pw = new PrintWriter(System.out);

    public static int readIntLine() throws IOException {
        return Integer.parseInt(br.readLine());
    }

    public static void main(String[] args) throws IOException {
        Map<Integer, Integer> resultMap = new HashMap<Integer, Integer>();
        String str = br.readLine();
        String split[] = str.split(" ");

        int asize = Integer.parseInt(split[0]);
        int bsize = Integer.parseInt(split[1]);
        int csize = Integer.parseInt(split[2]);
        int size = asize + bsize + csize;

        int arr[] = new int[size];

        for (int i = 0; i < size; i++) {
            int x = readIntLine();
            resultMap.put(x, resultMap.get(x) == null ? 1 : (resultMap.get(x).intValue() + 1));
        }

        ArrayList<Integer> lst = new ArrayList<Integer>();
        for (Map.Entry<Integer, Integer> entry : resultMap.entrySet()) {
            if (entry.getValue().intValue() > 1) {
                lst.add(entry.getKey());
            }
        }

        pw.println(lst.size());
        Collections.sort(lst);
        for (int x : lst) {
            pw.println(x);
        }
        pw.flush();
        pw.close();
    }
}

No comments:

Post a Comment