QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#681150#7904. Rainbow SubarraysiyueWA 1411ms6744kbC++232.8kb2024-10-27 01:57:072024-10-27 01:57:09

Judging History

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

  • [2024-10-27 01:57:09]
  • 评测
  • 测评结果:WA
  • 用时:1411ms
  • 内存:6744kb
  • [2024-10-27 01:57:07]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

int read(){
    int res=0,sign=1;
    char ch=getchar();
    for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-'){sign=-sign;}
    for(;ch>='0'&&ch<='9';ch=getchar()){res=(res<<3)+(res<<1)+(ch^'0');}
    return res*sign;
}

#define rep(i,l,r) for(int i=l;i<=r;++i)
#define dep(i,r,l) for(int i=r;i>=l;--i)

template<typename T>
class Median{
    public:
    multiset<T> mn,mx;
    void equal();
    void insert(T cur);
    void delet(T cur);
    T find();
};
 
template<typename T>   
void Median<T>:: equal(){
    int n1=mn.size();
    int n2=mx.size();
    if(n2==n1) return;
    if(n2+1==n1) return;
    int ad=0;
    if((n2+n1)%2==1) ad++; 
    while(n1<n2+ad){
        T fr=*(mx.begin());
        mx.erase(mx.begin());
        mn.insert(fr);
        n1++;
        n2--;
    }
    while(n2+ad<n1){
        auto it=mn.rbegin();
        T bc=*(it);
        auto itt=it.base();
        mn.erase(--itt);
        mx.insert(bc);
        n2++;
        n1--;
    }
}
template<typename T>   
void Median<T>:: insert(T cur){
    equal(); 
    if(mn.size()==0){
        mn.insert(cur);
        return;
    }
    int bc=*(mn.rbegin());
    if(cur<=bc){
        mn.insert(cur);
    }
    else{
        mx.insert(cur);
    }
    equal(); 
}
 
template<typename T>   
void Median<T>:: delet(T cur){
    auto it=mn.find(cur);
    if(it!=mn.end()){
        mn.erase(it);
        equal();
        return;
    }
    it=mx.find(cur);
    if(it!=mx.end()){
        mx.erase(it);
        equal();
        return;
    }
}
template<typename T>   
T  Median<T>:: find(){
    equal();
    int n1=mn.size();
    int n2=mx.size();
    if(n2==0) return *(mn.rbegin()); 
    T fr=*(mx.begin());
    T bc=*(mn.rbegin());
    if(n1==n2) return (fr+bc)/2;
    else{
        if(n1>n2) return bc;
        else  return fr;
    }
}

typedef long long ll;

const int N=5e5+10;

int n;
ll k;

int a[N];  

bool check(int len){
    ll res=0;
    Median<int> M;
    rep(i,1,len){
        M.insert(a[i]);
    }
    int f=M.find();
    rep(i,1,len){
        res+=abs(a[i]-f);
    }
    ll ans=res;
    rep(i,len+1,n){
        M.delet(a[i-len]),M.insert(a[i]);
        res=res-abs(a[i-len]-f)+abs(a[i]-f);
        int _f=M.find();
        res-=abs(_f-f);
        f=_f;
        ans=min(ans,res);
    }
    return ans<=k;
}

void solve(){
    // n=read<int>(),k=read<ll>();
    scanf("%d%lld",&n,&k);
    // rep(i,1,n)a[i]=read<int>()-i;
    rep(i,1,n)scanf("%d",&a[i]),a[i]-=i;
    int l=1,r=n;
    while(l<=r){
        int mid=l+r>>1;
        if(check(mid))l=mid+1;
        else r=mid-1;
    }
    printf("%d\n",r);
}
 
int main(){
    int t;scanf("%d",&t);
    while(t--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
7 5
7 2 5 5 4 11 7
6 0
100 3 4 5 99 100
5 6
1 1 1 1 1
5 50
100 200 300 400 500
1 100
3

output:

4
3
5
1
1

result:

ok 5 lines

Test #2:

score: -100
Wrong Answer
time: 1411ms
memory: 6744kb

input:

11102
2 167959139
336470888 134074578
5 642802746
273386884 79721198 396628655 3722503 471207868
6 202647942
268792718 46761498 443917727 16843338 125908043 191952768
2 717268783
150414369 193319712
6 519096230
356168102 262263554 174936674 407246545 274667941 279198849
9 527268921
421436316 3613460...

output:

1
4
3
2
6
5
7
2
4
1
3
1
1
3
1
2
7
8
7
7
1
7
6
2
4
3
1
6
7
7
3
4
3
9
3
8
6
6
3
1
6
3
1
2
4
6
4
6
4
1
4
7
1
6
3
5
5
6
1
7
5
3
1
6
3
5
3
2
2
6
2
3
10
1
4
3
2
4
5
1
7
5
5
5
8
5
3
6
3
5
5
8
5
4
5
2
1
5
2
4
3
4
8
1
3
1
2
2
8
3
1
6
8
1
8
4
5
6
6
7
4
8
3
2
8
4
5
5
2
6
2
4
1
5
4
5
3
2
4
1
2
1
4
5
8
3
7
3
3
3...

result:

wrong answer 11th lines differ - expected: '4', found: '3'