QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#485624#5435. Clamped SequenceWangzehao2009WA 1ms5984kbC++142.8kb2024-07-20 21:27:522024-07-20 21:27:53

Judging History

你现在查看的是最新测评结果

  • [2024-07-20 21:27:53]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5984kb
  • [2024-07-20 21:27:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int const N=2e5+3;
int a[N],n,d;
typedef long long ll;
vector <int> val;
ll MAX=-0x3f3f3f3f3f3f3f3f,MIN=-MAX;
pair<int,int> b[N];
ll check(ll l)
{
    int r=l+d;
    long long cnt=0,last=a[1];
    if(a[1]<l) last=l;
    if(a[1]>r) last=r;
    for(int i=2;i<=n;++i)
    {
        int t=a[i];
        if(t<l) t=l;
        if(t>r) t=r;
        cnt+=abs(t-last);
        last=t;
    }
    return cnt;
}
ll solve(ll l,ll r)
{
    if(r-l<=3)
    {
        ll ans=0;
        for(ll i=l;i<=r;++i) ans=max(ans,check(i));
        return ans;
    }
    ll mid=(l+r)>>1,l1=max(l,mid-1),l2=min(r,mid+1),cl=check(l1),cr=check(l2);
    if(cl>cr) return solve(l,l2);
    else return solve(l1,r);
    return 0;
}
priority_queue <int,vector<int>,greater<int> > q;
inline void solve1()
{
    sort(b+1,b+n);
    sort(val.begin(),val.end());
    val.erase(unique(val.begin(),val.end()),val.end());
    long long cnt=0;
    int i=1;
    for(auto l:val)
    {
        int r=l+1;
        while(!q.empty() && q.top()<=l) q.pop();
        while(i<n && b[i].first<r) q.push(b[i].second),i++;
        cnt=max(cnt,(ll)q.size());
    }
    while(!q.empty()) q.pop();
    i=1;
    for(auto r:val)
    {
        int l=r-1;
        while(!q.empty() && q.top()<=l) q.pop();
        while(i<n && b[i].first<r) q.push(b[i].second),i++;
        cnt=max(cnt,(ll)q.size());
    }
    printf("%lld\n",cnt);
}
inline void solve0()
{
    sort(val.begin(),val.end());
    val.erase(unique(val.begin(),val.end()),val.end());
    long long ans=0;
    for(int l:val)
    {
        long long r=l+d;
        if(r>MAX) break;
        long long cnt=0,last=a[1],t;
        if(a[1]<l) last=l;
        if(a[1]>r) last=r;
        for(int i=2;i<=n;++i)
        {
            int t=a[i];
            if(t<l) t=l;
            if(t>r) t=r;
            cnt+=abs(t-last);
            last=t;
        }
        ans=max(ans,cnt);
    }
    for(int r:val)
    {
        long long l=r-d;
        if(r>MAX) break;
        long long cnt=0,last=a[1],t;
        if(a[1]<l) last=l;
        if(a[1]>r) last=r;
        for(int i=2;i<=n;++i)
        {
            int t=a[i];
            if(t<l) t=l;
            if(t>r) t=r;
            cnt+=abs(t-last);
            last=t;
        }
        ans=max(ans,cnt);
    }
    printf("%lld\n",ans);
    exit(0);
}
int main()
{
    scanf("%d%d",&n,&d);
    for(int i=1;i<=n;++i) 
    {
        scanf("%d",&a[i]);
        val.push_back(a[i]);
        MIN=min(MIN,(ll)(a[i]-d)),MAX=max(MAX,1ll*a[i]);
    }
    for(int i=1;i<n;++i) b[i]={min(a[i],a[i+1]),max(a[i],a[i+1])};
    int ll=MIN,rr=MAX;
    if(n<=1000) solve0();
    if(d==1) solve1();
    else printf("%lld\n",solve(ll,rr));
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3920kb

input:

8 3
3 1 4 1 5 9 2 6

output:

15

result:

ok 1 number(s): "15"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3932kb

input:

2 1
-1000000000 1000000000

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3768kb

input:

2 1000000000
-1000000000 1000000000

output:

1000000000

result:

ok 1 number(s): "1000000000"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3968kb

input:

5000 10000
-10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10000 10000 -10...

output:

49990000

result:

ok 1 number(s): "49990000"

Test #5:

score: 0
Accepted
time: 1ms
memory: 3948kb

input:

5000 10000
10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 1...

output:

0

result:

ok 1 number(s): "0"

Test #6:

score: 0
Accepted
time: 1ms
memory: 4044kb

input:

5000 2897
8987 9710 547 6523 -4484 8892 7360 7473 -5435 8122 -1396 -9963 -1884 -5758 -2930 2867 -260 -3947 -7915 -2428 4137 -1202 -1874 7205 -2852 -6126 -9346 -8662 2294 -4453 -2430 2424 9434 -112 2906 -169 -9489 -1977 68 -9072 2785 -2258 9710 271 8817 6251 3120 4765 -8449 -6625 2617 -3931 -4024 144...

output:

7111087

result:

ok 1 number(s): "7111087"

Test #7:

score: 0
Accepted
time: 1ms
memory: 4028kb

input:

5000 6321
-5894 5183 -3582 7556 -2937 2854 -5688 1422 -7283 5712 -3719 3700 -8585 2577 -703 1839 -8871 9819 -1080 6964 -7192 1664 -9414 1863 -3638 7843 -6700 9064 -4209 7225 -2617 180 -9771 401 -3029 7485 -3877 3095 -4176 5830 -1954 8879 -9515 4671 -9834 395 -3489 6706 -5316 9004 -3536 546 -5360 686...

output:

26696461

result:

ok 1 number(s): "26696461"

Test #8:

score: 0
Accepted
time: 1ms
memory: 4028kb

input:

5000 1224
-9995 -9993 -9992 -9990 -9987 -9979 -9977 -9969 -9965 -9963 -9959 -9956 -9944 -9944 -9931 -9930 -9929 -9927 -9926 -9924 -9923 -9921 -9917 -9917 -9907 -9901 -9899 -9890 -9885 -9876 -9871 -9867 -9859 -9859 -9859 -9854 -9854 -9841 -9841 -9836 -9836 -9834 -9831 -9829 -9823 -9823 -9822 -9818 -9...

output:

1224

result:

ok 1 number(s): "1224"

Test #9:

score: 0
Accepted
time: 1ms
memory: 5984kb

input:

5000 1000000
-1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 1000000 -1000000 100000...

output:

4999000000

result:

ok 1 number(s): "4999000000"

Test #10:

score: 0
Accepted
time: 1ms
memory: 4084kb

input:

5000 1000000
1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000...

output:

0

result:

ok 1 number(s): "0"

Test #11:

score: -100
Wrong Answer
time: 1ms
memory: 4080kb

input:

5000 187598
115918 -727930 454749 -650387 -131030 -583797 907610 -314887 -494840 -116261 406466 734257 -410787 342451 -14930 831297 314447 -433165 -777692 -609353 -434131 518832 320933 -199305 154559 932798 930259 214168 -930213 -34688 247952 503849 21404 311008 -785124 -503509 -476434 -654305 -2049...

output:

477551774

result:

wrong answer 1st numbers differ - expected: '477553294', found: '477551774'