Hackerrank (2): Snapdeal Hackathon > Cycling

题目:

Problem Statement

The Snapdeal delivery people ride bikes to deliver products to various people around the city. There are N delivery people numbered 1 through N , and M bikes numbered 1 through M . Delivery person x has strength S[x] , and bike y has quality Q[y] .
We know each delivery person's opinion of each bike. OP[x][y] is "Y" if delivery person x likes the bike y , else OP[x][y] is "N".

The delivery person will start riding if, and only if, they have a bike that they like. This is always possible. The manager at Snapdeal wants to calculate the minimum and maximum possible team power. The team power is the sum of each delivery person's powers. The power of a delivery person x is S[x]Q[y] if the delivery person x is riding bike y . Help the manager calculate the minimum and maximum team power possible.

Constraints

1NM20
1S[x]1000
1Q[y]1000

Input Format

The first line will contain two integers N and M .
The second line will contain N integers, denoting the array S .
The third line will contain M integers, denoting the array Q .
The next N lines will contain M characters. The character in x th row and y th column denotes OP[x][y] .

Output Format

Print two integers on a single line separated by a space, the minimum and maximum possible power of the delivery team, respectively.

Sample Input

2 3
1 5
1 5 5
NYY
YYN

Sample Output

10 30

Explanation

The minimum power possible is, when delivery person 1 has bike 2 , and delivery person 2 has bike 1 . Calculation: 15+51=10
The maximum power possible is, when delivery person 1 has bike 3 , and delivery person 2 has bike 2 . Calculation: 15+55=30


题解:

用了个类似B数的结构,不过大数据还是会超时。

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

class Pair {
    int value;
    List<Integer> cols;
    Pair(int v, int c) {
        value = v;
        cols = new ArrayList<Integer>();
        cols.add(c);
    }
    
    Pair(int v, List<Integer> l) {
        value = v;
        cols = l; 
    }
}

class TreeNode {
    HashMap<Pair, TreeNode> elements;
    TreeNode() {
        elements = new HashMap<Pair, TreeNode>();
    }
}

public class Solution {
    
    private static int maxPower = 0;
    private static int minPower = Integer.MAX_VALUE;
    
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int numOfPeople = sc.nextInt();
        int numOfBike = sc.nextInt();
        int[] powers = new int[numOfPeople];
        int[] qualities = new int[numOfBike];
        int[][] scores = new int[numOfPeople][numOfBike];
        for(int i = 0; i < numOfPeople; i++) {
            powers[i] = sc.nextInt();
            //System.out.println(powers[i]);
        }
        //System.out.println(numOfBike);
        for(int i = 0; i < numOfBike; i++) {
            qualities[i] = sc.nextInt();
            //System.out.println(qualities[i]);
            //sc.nextLine();
        }
        sc.nextLine();
        for(int i = 0; i < numOfPeople; i++) {
            
            String s = sc.nextLine();
            for(int j = 0; j < numOfBike; j++) {
                if(s.charAt(j) == 'N')
                    scores[i][j] = 0;
                else
                    scores[i][j] = powers[i] * qualities[j];
                //System.out.print(scores[i][j] + " ");
            }
            //System.out.println();
        }
        

        
        Queue<TreeNode> q1 = new LinkedList<>();
        Queue<TreeNode> q2 = new LinkedList<>();
        TreeNode root = null;
        for(int i = 0; i < numOfPeople; i++) {
            if(i == 0) {
                root = new TreeNode();
                for(int j = 0; j < numOfBike; j++) {
                    if(scores[i][j] != 0) {
                        Pair p = new Pair(scores[i][j], j);
                        root.elements.put(p, new TreeNode());
                    }
                }
                q1.add(root);
            } else {
                while(!q1.isEmpty()) {
                    TreeNode current = q1.poll();
                    for(Pair p : current.elements.keySet()) {
                        TreeNode newNode = new TreeNode();
                        for(int j = 0; j < numOfBike; j++) {
                            if(scores[i][j] != 0) {
                                boolean exist = false;
                                for(int k : p.cols) {
                                    if(j == k)
                                       exist = true; 
                                }
                                if(!exist) {
                                    List<Integer> newList = new ArrayList<>();
                                    newList.addAll(p.cols);
                                    newList.add(j);
                                    newNode.elements.put(new Pair(scores[i][j], newList), new TreeNode());
                                }
                            }
                        }
                        current.elements.put(p, newNode);
                        q2.add(newNode);
                    }
                }
                q1.addAll(q2);
                q2.clear();
            }
        }
        dfs(root, 1, numOfPeople, 0);
        System.out.println(minPower + " " + maxPower);
    }
    
    private static void dfs(TreeNode root, int currentLevel, int level, int sum) {
        if(root.elements.isEmpty())
            return;
        if(currentLevel == level) {
            for(Pair p : root.elements.keySet()) {
                //System.out.println(sum + p.value);
                if(sum + p.value > maxPower)
                    maxPower = sum + p.value;
                if(sum + p.value < minPower)
                    minPower = sum + p.value;
            }
            return;
        }
        for(Pair p : root.elements.keySet()) {
            dfs(root.elements.get(p), currentLevel + 1, level, sum + p.value);
        }
    }
}

你可能感兴趣的:(Algorithm,Hackathon,hackerrank)