笔试的时候没写好,后来完善了一下(没有验证,不保证正确)。
思路是单调栈,栈中存放每一组的最大值。
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long a[] = new long[n]; for(int i=0;i<n;i++){ a[i] = sc.nextLong(); } int top = -1; long [] st= new long[n]; st[++top] = a[0]; for(int i=1;i<n;i++){ // 顶部元素 long tmp = st[top]; if(a[i]>=tmp){ st[++top] = a[i]; }else if(!(top>0&&a[i]>=st[top-1])){ while(top>=0&&st[top]>a[i]) { top--; } st[++top] = tmp; } } System.out.println(top+1); }