[Java] 蓝桥杯ADV-166 算法提高 聪明的美食家

版权声明:【https://github.com/liuchuo】大四在校生,水平有限,还望学长们多多包涵,Github真诚求Star~不甚感激!!!(卖萌脸ヾ(=^▽^=)ノ https://blog.csdn.net/liuchuo/article/details/82919373

问题描述
如果有人认为吃东西只需要嘴巴,那就错了。
都知道舌头有这么一个特性,“由简入奢易,由奢如简难”(据好事者考究,此规律也适合许多其他情况)。具体而言,如果是甜食,当你吃的食物不如前面刚吃过的东西甜,就很不爽了。
大宝是一个聪明的美食家,当然深谙此道。一次他来到某小吃一条街,准备从街的一头吃到另一头。为了吃得爽,他大费周章,得到了各种食物的“美味度”。他拒绝不爽的经历,不走回头路而且还要爽歪歪(爽的次数尽量多)。

输入格式
两行数据。
第一行为一个整数n,表示小吃街上小吃的数量
第二行为n个整数,分别表示n种食物的“美味度”

输出格式
一个整数,表示吃得爽的次数
样例输入
10
3 18 7 14 10 12 23 41 16 24
样例输出
6
数据规模和约定
美味度为0到100的整数
n<1000

package adv166;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] g = new int[n];
        for (int i = 0; i < n; i++) {
            g[i] = in.nextInt();
        }
        in.close();

        int[][] dp = new int[n][2];
        dp[0][0] = 1;
        dp[0][1] = 1;
        for (int i = 1; i < n; i++) {
            dp[i][1] = 1;
            for (int j = i - 1; j >= 0; j--) {
                if (g[i] >= g[j]) {
                    dp[i][1] = Integer.max(dp[i][1], dp[j][1] + 1);
                }
            }
            
            dp[i][0] = Integer.max(dp[i - 1][0], dp[i][1]);
        }
        System.out.println(dp[n - 1][0]);
    }

}

猜你喜欢

转载自blog.csdn.net/liuchuo/article/details/82919373