给你一个长度为 n n n 的正整数数组 S S S,按照下标从小到大开始遍历,判断第 i i i 个元素是否为 S [ 1 ] S[1] S[1] ~ S [ i ] S[i ] S[i] 中最大的。
计算符合条件的下标个数。
思路:我们可以保存前 i − 1 i-1 i−1 个元素的最大值,然后与当前第 i i i 个元素进行比较,并更新当前最大值。
时间复杂度: O ( n ) O(n) O(n)
#includeusing namespace std; int n; int main() { cin >> n; int res = 0, j = n + 1; for (int i = 1, x; i <= n; i++) { scanf("%d", &x); if (x < j) { j = x; res++; } } cout << res; return 0; }



