您的位置:首页 > 产品设计 > UI/UE

PAT A 1101. Quick Sort (25)

2017-08-22 00:03 211 查看
#include <cstdio>
#include <set>
#include <cmath>
using namespace std;

set<long long> res;

struct node{
long long key,nowmax;
void larger(long long &n){//检查当前元素左侧的元素是否都比此元素小,同时更新当前序列的最大值
if(n>key){
nowmax=n;
}
else {
n=key;
nowmax=key;
}
}
bool check(long long &n){//检查当前元素元素是否为轴
if(key<n){
n=key;
if(nowmax==key)return true;
}
return false;
}
};

node a[100003];

int main(void){
//freopen("in.log","r",stdin);
int n;
scanf("%d",&n);
long long tmpmax=-1;
for(int i=0;i<n;++i){
scanf("%lld",&a[i].key);
a[i].larger(tmpmax); //正向扫描
}
long long tmpmin=pow(10,10)+1;
for(int i=n-1;i>=0;--i){
if(a[i].check(tmpmin))res.insert(a[i].key); //反向扫描
}
printf("%d\n",res.size());
for(set<long long>::iterator it=res.begin();it!=res.end();++it){
if(it!=res.begin())printf(" ");
printf("%lld",*it);
}
printf("\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: