HDU 5805/BC 86B NanoApe Loves Sequence

题解:首先计算出每两个相邻数之间的差值,统计出其中最大的三个。然后枚举删每一个点,对于删除当前点,首先删除当前点与左右两个点之间的距离 (只有删除的距离为最大的两个时才会有影响),然后处理没被删掉的最大值与新生成的左右两个点之间的差值作比较,取最大值加到答案上。

对于前三大的值可以开三个变量统一,为了方便也可以sort,我比赛的时候脑抽用multiset维护的 ,也就是6倍sort的时间,终判就T了...

也不明白为什么才10个样例 n才1e5 O(nlogn)的算法也能T ...又一次败在水题上..


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#define mem(x,y) memset(x,y,sizeof(x))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define bug puts("===========");
#define zjc puts("");
const double pi=(acos(-1.0));
const double eps=1e-8;
const ll INF=1e18+10;
const ll inf=1e9+10;
const int mod=998244353;
const int maxn=100000+5;
/*=======================================*/
namespace fastIO{
    #define ll long long
    inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
    //getchar->read
    inline void read1(int &x){
        char ch;int bo=0;x=0;
        for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1;
        for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
        if (bo)x=-x;
    }
    inline void read1(ll &x){
        char ch;int bo=0;x=0;
        for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1;
        for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
        if (bo)x=-x;
    }
    inline void read1(double &x){
        char ch;int bo=0;x=0;
        for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1;
        for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
        if (ch=='.'){
            double tmp=1;
            for (ch=getchar();ch>='0'&&ch<='9';tmp/=10.0,x+=tmp*(ch-'0'),ch=getchar());
        }
        if (bo)x=-x;
    }
    inline void read1(char *s){
        char ch=getchar();
        for (;blank(ch);ch=getchar());
        for (;!blank(ch);ch=getchar())*s++=ch;
        *s=0;
    }
    inline void read1(char &c){for (c=getchar();blank(c);c=getchar());}
    //scanf->read
    inline void read2(int &x){scanf("%d",&x);}
    inline void read2(ll &x){
        #ifdef _WIN32
            scanf("%I64d",&x);
        #else
        #ifdef __linux
            scanf("%lld",&x);
        #else
            puts("error:can't recognize the system!");
        #endif
        #endif
    }
    inline void read2(double &x){scanf("%lf",&x);}
    inline void read2(char *s){scanf("%s",s);}
    inline void read2(char &c){scanf(" %c",&c);}
    inline void readln2(char *s){gets(s);}

    //puts->write
    inline void print1(int x){
        static char buf[15];
        char *p1=buf;if (!x)*p1++='0';if (x<0)putchar('-'),x=-x;
        while(x)*p1++=x%10+'0',x/=10;
        while(p1--!=buf)putchar(*p1);
    }
    inline void println1(int x){print1(x);putchar('\n');}
    inline void print1(ll x){
        static char buf[25];
        char *p1=buf;if (!x)*p1++='0';if (x<0)putchar('-'),x=-x;
        while(x)*p1++=x%10+'0',x/=10;
        while(p1--!=buf)putchar(*p1);
    }
    inline void print1(char c){putchar(c);}
    inline void print1(char *s){while (*s)putchar(*s++);}
    inline void pe(){putchar('\n');}
    inline void pk(){putchar(' ');}
    inline void print2(int x){printf("%d",x);}
    inline void print2(char x){printf("%c",x);}
    inline void print2(ll x){
        #ifdef _WIN32
            printf("%I64d",x);
        #else
        #ifdef __linux
            printf("%lld",x);
        #else
            puts("error:can't recognize the system!");
        #endif
        #endif
    }
    #undef ll
};
using namespace fastIO;
/*==================================*/
int a[maxn];
int b[maxn];
int main()
{
    int T_T;
    read1(T_T);
    while(T_T--){
        int n;
        read1(n);
        for(int i=1;i<=n;i++) {
            read1(a[i]);
            if(i!=1) b[i-1]=abs(a[i]-a[i-1]);
        }
        ll ans=0;
        sort(b+1,b+n);
        for(int i=1;i<=n;i++){
            int l=0,r=0,k=0;
            if(i!=1) l=abs(a[i]-a[i-1]);
            if(i!=n) r=abs(a[i]-a[i+1]);
            if(i!=1&&i!=n) k=abs(a[i+1]-a[i-1]);
            if(l>r) swap(l,r);
            if(r==b[n-1]){
                if(l==b[n-2]) ans+=max(b[n-3],k);
                else ans+=max(b[n-2],k);
            }else ans+=max(b[n-1],k);
        }
        printf("%lld\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值