QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#316998 | #7904. Rainbow Subarray | gingoo | WA | 196ms | 8184kb | C++20 | 8.6kb | 2024-01-28 10:56:41 | 2024-01-28 10:56:41 |
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;
multiset<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(ans,r-l+1);
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(now),sum1+=now;
now=a[r];
s2.insert(now2),sum2+=now2;
}
}
}
//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;
// if(sum1-now*s1.size()+now*s2.size()-sum2<=k)ans=max(r-l+1,ans);
// else ans=max(r-l,ans);
//当前中位数是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++;
}
cout<<ans<<'\n';
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3748kb
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: 196ms
memory: 8184kb
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 7 8 7 8 1 7 7 2 4 3 1 6 7 7 3 4 3 9 3 8 6 6 3 1 6 5 1 3 4 8 4 6 4 1 6 7 1 6 3 5 7 6 1 7 5 3 1 6 5 5 3 3 3 7 2 3 10 1 5 3 2 4 5 1 7 5 5 5 8 6 3 6 3 5 7 8 3 4 5 3 1 5 3 6 3 6 8 1 4 1 2 2 8 3 1 6 8 1 8 4 5 8 6 8 3 8 3 2 8 4 5 6 3 7 2 5 1 5 5 5 3 2 6 1 2 1 4 5 8 4 7 3 3 3...
result:
wrong answer 16th lines differ - expected: '2', found: '3'