首页 > 试题广场 >

马戏团

[编程题]马戏团
  • 热度指数:12257 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
搜狐员工小王最近利用假期在外地旅游,在某个小镇碰到一个马戏团表演,精彩的表演结束后发现团长正和大伙在帐篷前激烈讨论,小王打听了下了解到, 马戏团正打算出一个新节目“最高罗汉塔”,即马戏团员叠罗汉表演。考虑到安全因素,要求叠罗汉过程中,站在某个人肩上的人应该既比自己矮又比自己瘦,或相等。 团长想要本次节目中的罗汉塔叠的最高,由于人数众多,正在头疼如何安排人员的问题。小王觉得这个问题很简单,于是统计了参与最高罗汉塔表演的所有团员的身高体重,并且很快找到叠最高罗汉塔的人员序列。 现在你手上也拿到了这样一份身高体重表,请找出可以叠出的最高罗汉塔的高度,这份表中马戏团员依次编号为1到N。

输入描述:
首先一个正整数N,表示人员个数。 
之后N行,每行三个数,分别对应马戏团员编号,体重和身高。


输出描述:
正整数m,表示罗汉塔的高度。
示例1

输入

6
1 65 100
2 75 80
3 80 100
4 60 95
5 82 101
6 81 70

输出

4
显然,做出这个题目比放屁困难
发表于 2024-05-16 14:46:01 回复(0)
这道题纯阅读理解,这句“站在某个人肩上的人应该既比自己又比自己瘦,或相等。”指身高满足矮,体重满足瘦或相等。
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int[][] body = new int[n][];
            for (int i = 0; i < n; i++) {
                scanner.nextInt();
                body[i] = new int[] {scanner.nextInt(), scanner.nextInt()};
            }
            Arrays.sort(body, (o1, o2) -> {
                if (o1[1] - o2[1] == 0) {
                    return o1[0] - o2[0];
                }
                return o1[1] - o2[1];
            });//从矮->高,从轻->重排序
            int height = func1(body, n);
            System.out.println(height);
        }
    }

    private static int func1(int[][] body, int n) {
        int[] dp = new int[n];
        //dp[i]表示第i个人是最后一人时的最大高度
        int max = 0;
        for (int i = 0; i < n; i++) {
            dp[i] = 1;
            for (int j = i - 1; j >= 0; j--) {
                //体重要严格比较
                if (body[j][0] < body[i][0] || (body[j][1] == body[i][1] &&
                                                body[j][0] == body[i][0])) {
                    dp[i] = Math.max(dp[j] + 1, dp[i]);
                }
            }
            max = Math.max(max, dp[i]);
        }
        return max;
    }
}


发表于 2023-05-27 21:08:36 回复(0)
测试用例有问题吧????要么就是我太菜了看不懂这个测试用例
发表于 2022-04-17 17:39:12 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        SH1();
    }
    static class Persion{
        public int id;
        public int weight;
        public int height;

    }
    public static void SH1() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String ln;
        while ((ln = br.readLine()) != null){
            List<Persion> personList = new ArrayList<>();
            int n = Integer.parseInt(ln);
            int[] p = new int[n];
            for (int i = 0; i < n; i++) {
                String[] str = br.readLine().split(" ");
                int id = Integer.parseInt(str[0]);
                int weight = Integer.parseInt(str[1]);
                int heigth = Integer.parseInt(str[2]);
                Persion persion = new Persion();
                persion.id = id;
                persion.weight = weight;
                persion.height = heigth;
                personList.add(persion);
            }
            personList.sort((a,b)->{
                if (a.weight - b.weight == 0){
                    return b.height - a.height;
                }
                return a.weight - b.weight;
            });
            int size = personList.size();
            for (int i = 1; i < size; i++) {
                for (int j = i - 1 ; j >= 0; j--) {
                    if (personList.get(j).height <= personList.get(i).height){
                        if (p[i] < p[j] + 1){
                            p[i] = p[j] + 1;
                        }
                    }
                }
            }
            int max = 0;
            for (int i = 0; i < n; i++) {
                if (p[i] > max) {
                    max = p[i];
                }
            }
            System.out.println(max+1);
        }

    }
}

发表于 2022-01-28 00:20:31 回复(1)
 import java.util.*;

public class Main {
    
    static class People {
        int height;
        int weight;

        public People(int weight, int height) {
            this.height = height;
            this.weight = weight;
        }
    }

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            int n = scan.nextInt();
            People[] array = new People[n];
            for (int i = 0; i < n; ++i) {
                int index = scan.nextInt();
                array[index - 1] = new People(scan.nextInt(), scan.nextInt());
            }

            Arrays.sort(array, new Comparator<People>() {
                public int compare(People p1, People p2) {
                    int result = Integer.compare(p1.height, p2.height);
                    if (result != 0)
                        return result;
                    else
                        return Integer.compare(p1.weight, p2.weight);
                }
            });

            int[] dp = new int[n];
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < dp.length; ++i) {
                dp[i] = 1;
                for (int j = i - 1; j >= 0; --j) {
                    if (array[i].weight > array[j].weight
                        || (array[i].weight == array[j].weight && array[i].height == array[j].height)) {
                        dp[i] = Math.max(dp[i], dp[j] + 1);
                    }
                }
                max = Math.max(dp[i], max);
            }
            System.out.println(max);
        }
    }
}
妈蛋,切记,身高和体重要一起相等时,才能往上叠,其他情况下,都是要身高更矮,体重更轻才能往上叠。
编辑于 2016-08-30 13:42:45 回复(8)

问题信息

难度:
5条回答 20280浏览

热门推荐

通过挑战的用户

查看代码