QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#456659 | #7904. Rainbow Subarray | manhao# | WA | 148ms | 5864kb | C++14 | 2.5kb | 2024-06-28 10:34:20 | 2024-06-28 10:34:21 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define dwn(i,x,y) for(int i=x;i>=y;i--)
#define ll long long
using namespace std;
template<typename T>inline void qr(T &x){
x=0;int f=0;char s=getchar();
while(!isdigit(s))f|=s=='-',s=getchar();
while(isdigit(s))x=x*10+s-48,s=getchar();
x=f?-x:x;
}
int cc=0,buf[31];
template<typename T>inline void qw(T x){
if(x<0)putchar('-'),x=-x;
do{buf[++cc]=int(x%10);x/=10;}while(x);
while(cc)putchar(buf[cc--]+'0');
}
const int N=5e5+10;
int n,k,a[N];
multiset<int>s1,s2;//s1.size()>=s2.size()
multiset<int>::iterator it;
ll sum1,sum2;
int mid;
void push(int x){
if(!s1.size()){
s1.insert(x);
sum1+=x;
mid=x;
}
else if(s1.size()==s2.size()){
int y=*s2.begin();
if(x<=y){
s1.insert(x);
sum1+=x;
mid=*(--s1.end());
}
else{
s2.erase(s2.begin());sum2-=y;
s1.insert(y);sum1+=y;mid=y;
s2.insert(x);sum2+=x;
}
}
else{
if(x>=mid){
s2.insert(x);sum2+=x;
}
else{
s1.erase(--s1.end());sum1-=mid;
s2.insert(mid);sum2+=mid;
s1.insert(x);sum1+=x;
mid=*(--s1.end());
}
}
}
void del(int x){
it=s1.find(x);
if(it!=s1.end()){
s1.erase(it);
sum1-=x;
}
else{
s2.erase(s2.find(x));
sum2-=x;
}
while(s1.size()<s2.size()){
int x=*s2.begin();
s2.erase(s2.begin());sum2-=x;
s1.insert(x);sum1+=x;
}
if(!s1.size())mid=-1;
else mid=*(--s1.end());
}
ll calc(){
return (sum2-1ll*mid*s2.size())+(1ll*mid*s1.size()-sum1);
}
void init(){
s1.clear();
s2.clear();
sum1=sum2=0;
mid=-1;
}
void solve(){
qr(n),qr(k);
rep(i,1,n){
qr(a[i]);
a[i]-=i;
}
init();
int ans=0,r=0;
rep(l,1,n){
while(r<n){
push(a[r+1]);
if(calc()<=k)r++;
else{
del(a[r+1]);
break;
}
}
// cout<<l<<" "<<r<<endl;
// cout<<mid<<" "<<sum1<<" "<<sum2<<" "<<s1.size()<<" "<<s2.size()<<endl;
ans=max(ans,r-l+1);
if(r==n)break;
del(a[l]);
// cout<<mid<<" "<<sum1<<" "<<sum2<<" "<<s1.size()<<" "<<s2.size()<<endl;
}
cout<<ans<<endl;
}
int main(){
int tt;qr(tt);
while(tt--)solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3724kb
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: 0
Accepted
time: 148ms
memory: 5864kb
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 4 1 1 3 2 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 6 6 1 7 5 3 1 6 4 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 3 3 4 8 1 3 1 2 2 8 3 1 6 8 1 8 4 5 6 6 8 4 8 3 2 8 4 5 6 2 6 2 4 1 5 4 5 3 2 4 1 2 1 4 5 8 3 7 3 3 3...
result:
ok 11102 lines
Test #3:
score: -100
Wrong Answer
time: 82ms
memory: 5604kb
input:
1 500000 17244641009859 54748096 75475634 204928248 276927808 84875072 103158867 27937890 322595515 186026685 45468307 69240390 139887597 188586447 373764525 121365644 310156469 185188306 60350786 211308832 384695957 370562147 208427221 35937909 267590963 126478310 275357775 55361535 335993561 36696...
output:
14
result:
wrong answer 1st lines differ - expected: '172998', found: '14'