QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#316601 | #7904. Rainbow Subarray | gingoo | WA | 174ms | 7324kb | C++20 | 8.6kb | 2024-01-27 22:33:43 | 2024-01-27 22:33:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef int ld;
const int mod=1e9+7;
// struct point
// {
// ld x, y;
// point operator + (const point &a) const
// {
// return {x + a.x, y + a.y};
// }
// point operator - (const point &a) const
// {
// return {x - a.x, y - a.y};
// }
// point operator * (const ld &a) const
// {
// return {x * a, y * a};
// }
// };
// struct line
// {
// point s, t;
// };
// ld sqr(ld x) { return x * x; }
// // // const ld eps = 1e-9;
// // int sgn(ld x) { return x > eps ? 1 : -eps ? -1
// // : 0; }
// ld dot(const point &a, const point &b)
// {
// return a.x * b.x + a.y * b.y;
// }
// ld det(const point &a, const point &b)
// {
// return a.x * b.y - a.y * b.x;
// }
// bool point_on_line(const point &a, const line &l){
// return (det(l.s - a, a - l.t)) == 0;
// }
// bool pol(const point &a,const point &b,const point &c){
// return (a.y-b.y)*(b.x-c.x)==(b.y-c.y)*(a.x-b.x);
// }
// vector<point> v;
// map<point,int> mp;
// int n;
// bool dfs(vector<point> &v1,int now){
// if(v1.size()<=now)return 1;
// for(int i=0;i<=now;i++){
// for(int j=i+1;j<=now;j++){
// vector<point> v2;
// for(auto p:v1){
// if(!point_on_line(p,(line){v1[i],v1[j]})){
// v2.push_back(p);
// }
// }
// if(dfs(v2,now-1))return 1;
// }
// }
// return 0;
// }
typedef long long ll;
set<ll> s1,s2;
ll a[500500];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--){
ll n,k;
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>a[i];
a[i]-=(i+1);
}
s1.clear();s2.clear();
// set<ll> s1,s2;
//cout<<s1.size()<<" "<<s2.size()<<endl;
ll l=0,r=0;
ll sum1=0,sum2=0;
ll ans=0;
a[n]=1e9+100;
ll now=a[0];
while(l<=r){
if(l==n)break;
//if(l==r)cout<<s1.size()<<" "<<s2.size()<<endl;
while(sum1-now*s1.size()+now*s2.size()-sum2<=k){
ll now1,now2;
ans=max(r-l+1,ans);
if(r==n-1)break;
// int flag1=0,,flag2=0;
r++;
if(s1.empty() and s2.empty()){
if(a[r]>=now)s1.insert(a[r]),sum1+=a[r];
else s2.insert(a[r]),sum2+=a[r];
continue;
}
else if(s1.empty()){
if(a[r]>=now)s1.insert(a[r]),sum1+=a[r];
else s1.insert(now),sum1+=now,now=a[r];
continue;
}
else if(s2.empty()){
if(a[r]<=now)s2.insert(a[r]),sum2+=a[r];
else s2.insert(now),sum2+=now,now=a[r];
continue;
}
now1=*s1.begin(),s1.erase(s1.find(now1)),sum1-=now1;
now2=*s2.rbegin(),s2.erase(s2.find(now2)),sum2-=now2;
ll cnt=r-l+1;
if(a[r]>=now1){
s1.insert(a[r]),sum1+=a[r];
if(!(cnt%2)){
//s1.insert(now1),sum1+=now1;
//s2.insert(now2),sum2+=now2;
if(s1.size()>s2.size()){
s2.insert(now2),sum2+=now2;
s2.insert(now),sum2+=now;
now=now1;
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(now2),sum2+=now2;
}
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(now2),sum2+=now2;
}
}
else if(a[r]<=now2){
s2.insert(a[r]),sum2+=a[r];
if(!(cnt%2)){
//s1.insert(now1),sum1+=now1;
//s2.insert(now2),sum2+=now2;
if(s1.size()<s2.size()){
s1.insert(now1),sum1+=now1;
s1.insert(now),sum1+=now;
now=now2;
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(now2),sum2+=now2;
}
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(now2),sum2+=now2;
}
}
else if(a[r]<=now and a[r]>=now2){
s2.insert(now2),sum2+=now2;
if(!(cnt%2)){
//s1.insert(now1),sum1+=now1;
//s2.insert(now2),sum2+=now2;
if(s1.size()<s2.size()){
s1.insert(now1),sum1+=now1;
s1.insert(now),sum1+=now;
now=a[r];
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(a[r]),sum2+=a[r];
}
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(a[r]),sum2+=a[r];
}
}
else if(a[r]>=now and a[r]<=now1){
s1.insert(now1),sum1+=now1;
if(!(cnt%2)){
//s1.insert(now1),sum1+=now1;
//s2.insert(now2),sum2+=now2;
if(s1.size()<s2.size()){
s1.insert(a[r]),sum1+=a[r];
s1.insert(now),sum1+=now;
now=now2;
}
else{
s1.insert(now),sum1+=now;
s2.insert(now2),sum2+=now2;
now=a[r];
}
}
else{
s1.insert(a[r]),sum1+=a[r];
s2.insert(now2),sum2+=now2;
}
}
}
//else ans=max(r-l,ans);
//r--;
// //ans=max(r-l+1,ans);
// cout<<l<<" "<<r<<endl;
// cout<<sum1-now*s1.size()+now*s2.size()-sum2<<endl;
// cout<<s1.size()<<" "<<s2.size()<<endl;
// cout<<sum1<<" "<<now<<" "<<sum2<<endl;
//当前中位数是now
if(s1.empty() and s2.empty()){
now=a[l+1];
}
else if(s1.empty()){
auto p=*s2.rbegin();s2.erase(s2.find(p));
sum2-=p;
if(now==a[l])now=p;
}
else if(s2.empty()){
auto p=*s1.begin();s1.erase(s1.find(p));
sum1-=p;
if(now==a[l])now=p;
}
else{
auto p1=*s1.begin();
auto p2=*s2.rbegin();
if(s1.find(a[l])!=s1.end()){
s1.erase(s1.find(a[l]));
sum1-=a[l];
sum1+=now;
s1.insert(now);
now=p2;
sum2-=p2;
s2.erase(s2.find(p2));
}
else if(s2.find(a[l])!=s2.end()){
s2.erase(s2.find(a[l]));
sum2-=a[l];
sum2+=now;
s2.insert(now);
sum1-=p1;
now=p1;
s1.erase(s1.find(p1));
}
else {
if(s1.size()<s2.size()){
now=p2;
sum2-=p2;
s2.erase(s2.find(p2));
}
else{
now=p1;
sum1-=p1;
s1.erase(s1.find(p1));
}
}
}
l++;
//if(sum1-now*s1.size()+now*s2.size()-sum2<=k)ans=max(r-l+1,ans);
//else ans=max(r-l,ans);
}
cout<<ans<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3676kb
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: 174ms
memory: 7324kb
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 3 6 8 7 7 1 7 7 2 4 3 1 6 6 7 3 4 3 9 3 8 5 6 3 1 6 3 1 3 4 7 4 6 4 1 6 6 1 6 3 5 6 6 1 7 5 3 1 6 5 4 3 3 3 6 2 3 10 1 5 3 2 4 5 1 7 5 5 5 8 5 3 6 3 5 5 8 3 5 5 3 1 5 3 4 3 7 8 1 4 1 2 2 8 3 1 6 8 1 8 4 5 5 6 7 4 8 3 2 8 4 5 6 3 6 2 4 1 5 5 4 3 2 5 1 2 1 4 5 8 5 6 3 3 3...
result:
wrong answer 16th lines differ - expected: '2', found: '3'