Link: https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 4542
There are n peaks (n <= 10 ^ 5). Each mountain has a height of H, which corresponds to a D value for each mountain, each mountain to all other mountain that is strictly higher than it will go through a minimum value (Valley ), d Represents the difference between H and the maximum values in these minimum values (if there is no higher mountain than it, then D is its own height) and asks how many mountain peaks D> = 150000 meters.
Idea: it is still quite difficult to read the question. For the mountain P, the highest Valley required by the value of D in the question must appear between the P and the two adjacent peaks higher than it. In this way, the problem is transformed into determining the first mountain on both sides that is higher than it, and finding the minimum value (Valley) between the two ). Find the first mountain that is higher than it with DP and ask the minimum value with rmq.
Code:
#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 100005#define PI acos(-1.0)#define seed 31//131,1313#define lson i<<1,l,mid#define rson i<<1|1,mid+1,rtypedef long long LL;typedef unsigned long long ULL;using namespace std;int val[maxn*4],aa[maxn];int t;void build(int i,int l,int r){ if(l==r) { scanf("%d",&val[i]); aa[l]=val[i]; return ; } int mid=(l+r)>>1; build(lson),build(rson); val[i]=min(val[i<<1],val[i<<1|1]);}int query(int i,int l,int r,int q_l,int q_r){ if(q_l<=l&&r<=q_r) return val[i]; int mid=(l+r)>>1; if(q_r<=mid) return query(lson,q_l,q_r); else if(q_l>mid) return query(rson,q_l,q_r); else return min(query(rson,mid+1,q_r),query(lson,q_l,mid));}int L[maxn],R[maxn];void init(){ memset(L,0,sizeof(L)); memset(R,0,sizeof(R)); memset(aa,-1,sizeof(aa)); memset(val,0,sizeof(val)); build(1,1,t);}int main(){ while(~scanf("%d",&t)) { init(); L[1]=0; for(int i=2; i<=t; i++) { if(aa[i-1]>aa[i]) L[i]=i-1; else { int from=i-1; while(aa[i]>=aa[L[from]]&&from!=0) from=L[from]; L[i]=from; } } R[t]=t+1; for(int i=t-1;i>=1;i--) { if(aa[i+1]>aa[i]) R[i]=i+1; else { int from=i+1; while(aa[i]>=aa[R[from]]&&from!=t+1) from=R[from]; R[i]=from; } } bool flag=0; for(int i=1; i<=t; i++) { int res=-1; if(query(1,1,t,L[i],i)>res&&L[i]!=0) res=query(1,1,t,L[i],i); if(query(1,1,t,i,R[i])>res&&R[i]!=t+1) res=query(1,1,t,i,R[i]); if((res==-1&&aa[i]>=150000)||(aa[i]-res>=150000)) { if(flag==0) { printf("%d",i); flag = 1; } else printf(" %d",i); } } printf("\n"); } return 0;}
Uvalive 6531 go up the ultras