zoukankan      html  css  js  c++  java
  • 笛卡尔树--牛客第四场(sequence)

    思路:

    O(n)建一颗笛卡尔树,再O(n)dfs向上合并答案就行了。

      1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
      2 #include <cstdio>//sprintf islower isupper
      3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
      4 #include <iostream>//pair
      5 #include <fstream>
      6 #include <bitset>
      7 //#include <map>
      8 //#include<unordered_map>
      9 #include <vector>
     10 #include <stack>
     11 #include <set>
     12 #include <string.h>//strstr substr
     13 #include <string>
     14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
     15 #include <cmath>
     16 #include <deque>
     17 #include <queue>//priority_queue<long long, vector<long long>, greater<long long> > q;//less
     18 #include <vector>//emplace_back
     19 //#include <math.h>
     20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
     21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
     22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
     23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
     24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
     25 #define mem(a,b) memset(a,b,sizeof(a))
     26 #define pr printf
     27 #define sc scanf
     28 void swapp(long long &a,long long &b);
     29 double fabss(double a);
     30 long long maxx(long long a,long long b);
     31 long long minn(long long a,long long b);
     32 long long Del_bit_1(long long n);
     33 long long lowbit(long long n);
     34 long long abss(long long a);
     35 const long long INF=(1LL<<60);
     36 const double E=2.718281828;
     37 const double PI=acos(-1.0);
     38 const long long inf=(1<<29);
     39 const double ESP=1e-9;
     40 const long long mod=(long long)1e9+7;
     41 const long long N=(long long)3e6+10;
     42  
     43 int son[N][2];
     44 long long min_[N],max_[N];
     45 long long a[N];
     46 long long b[N];
     47 int stak[N];
     48 long long ans=-INF;
     49  
     50 void dfs(int rt,int l,int r)
     51 {
     52     if(son[rt][0])dfs(son[rt][0],l,rt-1);
     53     if(son[rt][1])dfs(son[rt][1],rt+1,r);
     54  
     55     long long minl=minn(b[l-1],min_[son[rt][0]]);
     56     long long minr=minn(b[rt],min_[son[rt][1]]);
     57     long long maxl=maxx(b[l-1],max_[son[rt][0]]);
     58     long long maxr=maxx(b[rt],max_[son[rt][1]]);
     59  
     60     max_[rt]=maxx(maxl,maxr);
     61     min_[rt]=minn(minl,minr);
     62  
     63     ans=maxx(ans,(maxr-minl)*a[rt]);
     64     ans=maxx(ans,(minr-maxl)*a[rt]);
     65 }
     66  
     67 int main()
     68 {
     69     int n;
     70     sc("%d",&n);
     71     fo(i,1,n)sc("%lld",&a[i]);
     72     fo(i,1,n)sc("%lld",&b[i]),b[i]+=b[i-1];
     73  
     74     long long top=0;
     75     fo(i,1,n)
     76     {
     77         while(top&&a[i]<a[stak[top]])
     78             son[i][0]=stak[top--];
     79         if(top)
     80             son[stak[top]][1]=i;
     81         stak[++top]=i;
     82     }
     83     min_[0]=INF,max_[0]=-INF;//注意细节;;...
     84     dfs(stak[1],1,n);
     85     pr("%lld
    ",ans);
     86     return 0;
     87 }
     88  
     89 /**************************************************************************************/
     90  
     91 long long maxx(long long a,long long b)
     92 {
     93     return a>b?a:b;
     94 }
     95  
     96 void swapp(long long &a,long long &b)
     97 {
     98     a^=b^=a^=b;
     99 }
    100  
    101 long long lowbit(long long n)
    102 {
    103     return n&(-n);
    104 }
    105  
    106 long long Del_bit_1(long long n)
    107 {
    108     return n&(n-1);
    109 }
    110  
    111 long long abss(long long a)
    112 {
    113     return a>0?a:-a;
    114 }
    115  
    116 double fabss(double a)
    117 {
    118     return a>0?a:-a;
    119 }
    120  
    121 long long minn(long long a,long long b)
    122 {
    123     return a<b?a:b;
    124 }
  • 相关阅读:
    SpringCloud微服务初步认识
    SpringCloud-Hystrix:服务熔断与降级
    List接口下重要集合源码分析
    高频面试题:手写一个LRU
    Java基础面试题面经整理(持续更新)
    Redis高可用之主从复制
    Redis过期键删除和内存淘汰
    Redis持久化(RDB与AOF)
    了解Redis事务
    Redis入门与安装
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/11437198.html
Copyright © 2011-2022 走看看