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);
        }
    }
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值