大宝是一个聪明的美食家,当然深谙此道。一次他来到某小吃一条街,准备从街的一头吃到另一头。为了吃得爽,他大费周章,得到了各种食物的“美味度”。他拒绝不爽的经历,不走回头路而且还要爽歪歪(爽的次数尽量多)。
第一行为一个整数n,表示小吃街上小吃的数量
第二行为n个整数,分别表示n种食物的“美味度”
3 18 7 14 10 12 23 41 16 24
过程:其实就是 当前的美食甜度 与 前面的美食的甜度 的比较
| 美食甜度 | 3 | 18 | 7 | 14 | 10 | 12 | 23 | 41 | 16 | 24 |
| 累加,去的次数 | 1 | 2 | 2 | 3 | 3 | 4 | 5 | 6 | 5 | 6 |
所以,有两条路线可执行:
1) 3→7→10→12→23→41(爽了6次)
2) 3→7→10→12→16→24(爽了6次)
注意啦:可以把这道题和https://blog.youkuaiyun.com/xiaodingqq/article/details/79695590的这道题进行来比较!!
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
int[] d = new int[n];
for(int i=0;i<n;i++){
arr[i] = scanner.nextInt();
d[i]=1;
}
int max = 0;
for(int i=1;i<n;i++){
for(int j=i-1;j>=0;j--){
if(arr[i]>=arr[j]){ //等于是为了都是1的情况
d[i]=Math.max(d[i], d[j]+1); //arr[j]+1指包括了arr[i]
if(d[i]>max){
max=d[i];
}
}
}
}
System.out.println(max);
}
}
585



被折叠的 条评论
为什么被折叠?



