飞步A轮笔试题1 ABC

题目1 : ABC

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

杂货铺老板一共有N件物品,每件物品具有ABC三种属性中的一种或多种。从杂货铺老板处购得一件物品需要支付相应的代价。  

现在你需要计算出如何购买物品,可以使得ABC三种属性都在购买的物品中出现,并且支付的总代价最小。

输入

第一行包含一个整数N。  

以下N行,每行包含一个整数C和一个只包含"ABC"的字符串,代表购得该物品的代价和其具有的属性。  

对于50%的数据,1 ≤ N ≤ 20  

对于100%的数据,1 ≤ N ≤ 1000 1 ≤ C ≤ 100000

输出

一个整数,代表最小的代价。如果无论如何凑不齐ABC三种属性,输出-1。

样例输入

5
10 A
9 BC
11 CA
4 A
5 B

样例输出

13

比赛已经结束,去题库提交。

 

import java.util.ArrayList;

import java.util.Scanner;

 

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();

        ArrayList<Good> goods = new ArrayList<>();

        for (int i = 0; i < n; ++i) {

            goods.add(new Good(scanner.nextInt(), scanner.nextLine().trim( )));

        }

        int min = 400000;

        for (int i = 0; i < n; ++i) {

            Good g1 = goods.get(i);

            String str = g1.attr;

            if (str.contains("A") && str.contains("B") && str.contains("C")) {

                if (min > g1.cost) {

                    min = g1.cost;

                }

            } else {

                for (int j = i + 1; j < n; ++j) {

                    Good g2 = goods.get(j);

                    str = g1.attr + g2.attr;

                    if (str.contains("A") && str.contains("B") && str.contains("C")) {

                        min = Math.min(min, g1.cost + g2.cost);

                    } else {

                        for (int k = j + 1; k < n; ++k) {

                            Good g3 = goods.get(k);

                            str = g1.attr + g2.attr + g3.attr;

                            if (str.contains("A") && str.contains("B") && str.contains("C")) {

                                min = Math.min(min, g1.cost + g2.cost + g3.cost);

                            }

                        }

                    }

                }

            }

        }

        if (min == 400000) {

            min = -1;

        }

        System.out.println(min);

    }

}

 

class Good  {

    public int cost;

    public String attr;

 

    public Good(int cost, String attr) {

        this.cost = cost;

        this.attr = attr;

    }

}

猜你喜欢

转载自blog.csdn.net/weixin_38970751/article/details/83410889
ABC
今日推荐