HDU-6592 Posted on 2019-08-07 | In hdu | Views: 题意给一个序列 $a[]$ ,找出最长的单峰序列,输出该序列的下标,输出字典序最大和最小的情况 题解易证,当取第一个峰的时候字典序最小,当取最后一个峰的时候字典序最大。 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153#include <bits/stdc++.h>using namespace std;#define forl(i, l, r) for (int i = l; i <= r; i++)#define forr(i, r, l) for (int i = r; i >= l; i--)#define for1(i, n) for (int i = 1; i <= n; i++)#define fro1(i, n) for (int i = 1; i <= n; i++)#define for0(i, n) for (int i = 0; i < n; i++)#define fro0(i, n) for (int i = 0; i < n; i++)#define meminf(a) memset(a, inf, sizeof(a))#define mem_1(a) memset(a, -1, sizeof(a))#define mem0(a) memset(a, 0, sizeof(a))#define memcp(a,b) memcpy(a,b,sizeof(b))#define oper(type) bool operator <(const type y)const#define mp make_pair#define pu_b push_back#define pu_f push_front#define po_b pop_back#define po_f pop_front#define fi first#define se second#define whiel while#define retrun returntypedef pair<long long, long long> pll;typedef vector<long long> vll;typedef pair<int, int> pii;typedef unsigned long long ull;typedef vector<int> vii;typedef double db;typedef long long ll;typedef int itn;int in(int &a,int &b,int &c,int &d){return scanf("%d%d%d%d",&a,&b,&c,&d);}int in(int &a,int &b,int &c){return scanf("%d%d%d",&a,&b,&c);}int in(int &a,int &b){return scanf("%d%d",&a,&b);}int in(ll &a){return scanf("%lld",&a);}int in(int &a){return scanf("%d",&a);}int in(char *s){return scanf("%s",s);}int in(char &s){return scanf("%c",&s);}int in(db &a){return scanf("%lf",&a);}void out(int a){printf("%d ",a);}void outln(int a){printf("%d\n",a);}void out(ll a){printf("%lld ",a);}void outln(ll a){printf("%lld\n",a);}const db pi = acos((db)-1);const ll inf =0x3f3f3f3f;const db eps = 1e-8;const int N = 3.1e5;const int M = 2.1e5;const ll mod = 1e9+7;int sign(db a) { return a < -eps ? -1 : a > eps;}int db_cmp(db a, db b){ return sign(a-b); }int a[N],f[N],g[N],pre[N],ans[N];int main() {#ifdef PerpEternal // freopen("/Users/perpeternal/Documents/Sketch/data/in.dat", "r", stdin); // freopen("/Users/perpeternal/Documents/Sketch/data/out.dat", "w", stdout);#endif //ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; while(~in(n)){ for1(i,n)in(a[i]); int len=0; for1(i,n){ int posi=upper_bound(pre+1,pre+1+len,a[i])-pre; // out(posi); if(posi==len+1){ if(a[i]>pre[len]){ pre[++len]=a[i]; f[i]=len; }else f[i]=len; }else{ if(a[i]>pre[posi-1]){ pre[posi]=a[i]; f[i]=posi; }else{ f[i]=posi-1; } } } // puts(""/); len=0; for1(i,n){ int j=n-i+1; int posi=upper_bound(pre+1,pre+1+len,a[j])-pre; if(posi==len+1){ if(a[j]>pre[len]){ pre[++len]=a[j]; g[j]=len; }else g[j]=len; }else{ if(a[j]>pre[posi-1]){ pre[posi]=a[j]; g[j]=posi; }else{ g[j]=posi-1; } } } int maxx=0,Fi=0,La=0; for1(i,n)maxx=max(maxx,f[i]+g[i]-1); for1(i,n){ if(f[i]+g[i]-1==maxx){ if(!Fi)Fi=i; La=i; } } int tail=f[Fi]; ans[tail]=Fi; for(int i=Fi-1;i>0;i--){ if(f[i]+1>=tail&&f[i]<f[Fi]){ if(a[ans[f[i]+1]]>a[i]){ tail=f[i]; ans[f[i]]=i; } } } // assert(tail==1); tail=f[Fi]; // cout<<tail<<endl; for(int i=Fi+1;i<=n;i++){ if(a[i]<a[ans[tail]]&&g[i]+1==g[ans[tail]]){ ans[++tail]=i; } } for1(i,maxx){ printf("%d%c",ans[i]," \n"[i==maxx]); } tail=maxx-g[La]+1; ans[tail]=La; for(int i=La+1;i<=n;i++){ if(g[i]+tail>=maxx&&g[i]<g[La]){ if(a[ans[maxx-g[i]]]>a[i]){ tail=maxx-g[i]+1; ans[tail]=i; } } } // assert(tail==maxx); tail=maxx-g[La]+1; for(int i=La-1;i>0;i--){ if(a[i]<a[ans[tail]]&&f[i]+1==f[ans[tail]]){ ans[--tail]=i; } } for1(i,maxx){ printf("%d%c",ans[i]," \n"[i==maxx]); } } return 0;}