HDU 5805 NanoApe Loves Sequence(ST 表 or 预处理)
2016-08-06 22:37
375 查看
题目链接
St表bc上吃T,后来HDU上交了800ms过,然后又写了一个预处理的
mxl[i]:表示i左边的差值的最大值,mxr[i]:i右边的差值的最大值
ST表
方法二的
St表bc上吃T,后来HDU上交了800ms过,然后又写了一个预处理的
mxl[i]:表示i左边的差值的最大值,mxr[i]:i右边的差值的最大值
ST表
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<cstdlib> #include<vector> using namespace std; #define cl(a,b) memset(a,b,sizeof(a)) #define LL long long #define pb push_back #define gcd __gcd #define For(i,j,k) for(int i=(j);i<k;i++) #define lowbit(i) (i&(-i)) #define _(x) printf("%d\n",x) const int maxn = 1e6+200; const int inf = 1 << 28; LL mx[maxn][30],mi[maxn][30]; LL a[maxn]; LL b[maxn]; void initRmq(int n){ for(int j=1;j<20;j++){ for(int i=0;i+(1<<j)-1<n;i++){ mx[i][j]=max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]); mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]); } } } LL query(int x,int y){ if(x>y)return 0; int k=log(y-x+1)/log(2.0); LL mxx=max(mx[x][k],mx[y-(1<<k)+1][k]); LL mii=min(mi[x][k],mi[y-(1<<k)+1][k]); return max(abs(mxx),abs(mii)); } int main() { int T; scanf("%d",&T); int n; while(T--) { scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); } a[0]=a[1];a[n+1]=a ;n++; int k=0; for(int i=1;i<=n;i++,k++){ b[k]=a[i]-a[i-1]; mx[k][0]=mi[k][0]=b[k]; } initRmq(k); LL ans = query(2,k-1); for(int i=1;i+1<k;i++){ LL t = max(abs(b[i+1]+b[i]),max(query(0,i-1),query(i+2,k-1))); ans+=t; } printf("%lld\n",ans); } return 0; }
方法二的
#include<cstdio> #include<algorithm> #include<iostream> #include<vector> #include<queue> #include<cstring> #include<map> #include<string> #include<set> #include<cstdlib> using namespace std; #define cl(a,b) memset(a,b,sizeof(a)) #define LL long long #define pb push_back #define For(i,j,k) for(int i=(j);i<k;i++) #define lowbit(i) (i&(-i)) #define _(x) printf("%d\n",x) const int maxn = 1e6+200; const int inf = 1 << 28; LL a[maxn]; LL mxl[maxn],mxr[maxn]; int main(){ int T;scanf("%d",&T); while(T--){ int n;scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); mxl[i]=mxr[i]=0; } a[0]=a[1]; mxl[0]=0; for(int i=1;i<=n;i++){ mxl[i]=max(mxl[i-1],abs(a[i]-a[i-1])); } mxr =0; for(int i=n-1;i>=1;i--){ mxr[i]=max(mxr[i+1],abs(a[i+1]-a[i])); } /*for(int i=1;i<=n;i++){ printf("%lld ",mxl[i]); }puts(""); for(int i=1;i<=n;i++){ printf("%lld ",mxr[i]); } puts(""); */ LL ans = 0; for(int i=1;i<=n;i++){ if(i==1)ans+=mxr[i+1]; else if(i==n)ans+=mxl[i-1]; else ans+=max(abs(a[i+1]-a[i-1]),max(mxl[i-1],mxr[i+1])); } printf("%lld\n",ans); } return 0; }
相关文章推荐
- BestCoder Round #86 1002 &HDU 5805 ——NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence(水~)
- HDU 5805 NanoApe Loves Sequence
- hdu 5805 NanoApe Loves Sequence
- HDU-5805-NanoApe Loves Sequence(思维题)
- (模拟/线段树)HDU - 5805 NanoApe Loves Sequence
- hdu 5805 NanoApe Loves Sequence (水题)
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- hdu:5805:前缀和后缀NanoApe Loves Sequence
- hdu-5805 NanoApe Loves Sequence(线段树+概率期望)
- HDU 5805 NanoApe Loves Sequence(思维)
- Hdu 5805 NanoApe Loves Sequence【思维题】
- 【HDU】5805 - NanoApe Loves Sequence(模拟)
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence 线段树
- hdu_5805_NanoApe Loves Sequence(xjb搞)
- HDU 5805 NanoApe Loves Sequence (BC #86)
- HDU 5805 - NanoApe Loves Sequence