题目传送门:https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480
思路:
遍历统计每一位左边最大值和右边最小值
注意数量为0的时候也需要打印空行
#include <iostream>
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define scl(x) scanf("%lld",&x)
#define scs(x) scanf("%s",x)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define prs(x) printf("%s\n",(x))
#define prl(x) printf("%lld\n",x)
#define ll long long
#define PII pair<int,int>
#define eps 1e-6
#define inf 1e17
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=1e6+5;
int a[maxn];
int mx[maxn],mn[maxn];
int main(){
int n;
sca(n);
memset(mx,-1,sizeof(mx));
memset(mn,INF,sizeof(mn));
rep(i,0,n){
sca(a[i]);
if(i==0) mx[i] = a[i];
else
mx[i]=max(a[i],mx[i-1]);
}
for(int i=n-1;i>=0;i--){
if(i==n-1) mn[i] = a[i];
else
mn[i]=min(a[i],mn[i+1]);
}
int cnt=0;
vector<int> v;
rep(i,0,n){
if(a[i]>=mx[i]&&a[i]<=mn[i]) {
cnt++;
v.push_back(a[i]);
}
}
pri(cnt);
for(int i=0;i<v.size();i++){
if(i==v.size()-1) printf("%d",v[i]);
else printf("%d ",v[i]);
}
printf("\n");
}