QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#686696 | #7904. Rainbow Subarray | Vareal | RE | 0ms | 0kb | C++17 | 2.8kb | 2024-10-29 15:12:12 | 2024-10-29 15:12:13 |
answer
#pragma GCC optimize("Ofast")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
#include <cassert>
#include <random>
#include <chrono>
#include <ctime>
#include <cstdlib>
#define pii pair<int,int>
#define mp make_pair
#define pb emplace_back
#define debug() cout<<"qwq"<<endl
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned long long ull;
const int DMAX = 500000 + 5;
const double INF = 1e9;
const int MOD = 998244353;
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
template<class T> inline void read(T &x){
T f=1; x=0; char ch=getchar();
while(ch<'0' || ch>'9'){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch<='9' && ch>='0'){ x=x*10+ch-'0';ch=getchar(); }
x*=f;
}
int T;
int n;
ll k;
int a[DMAX], b[DMAX];
multiset<int> s1, s2;
int siz1, siz2;
ll sum1, sum2;
void balance(){
while(siz1 < siz2){
int x = *s2.begin();
s1.insert(x);
s2.erase(s2.begin());
siz1 ++, siz2 --;
sum1 += b[x], sum2 -= b[x];
}
while(siz1 > siz2 + 1){
auto it = s1.end();
it --;
int x = *it;
s1.erase(it), s2.insert(x);
siz1 --, siz2 ++;
sum1 -= b[x], sum2 += b[x];
}
}
void add(int val){
int x = *s1.rbegin();
if(!siz1) s1.insert(val), siz1 ++, sum1 += b[val], balance();
else{
int x = *s1.rbegin();
if(val <= x) s1.insert(val), siz1 ++, sum1 += b[val];
else s2.insert(val), siz2 ++, sum2 += b[val];
balance();
}
}
void del(int val){
int x = *s1.rbegin();
if(val <= x) s1.erase(s1.find(val)), siz1 --, sum1 -= b[val];
else s2.erase(s2.find(val)), siz2 --, sum2 -= b[val];
balance();
}
bool check(){
int x = *s1.rbegin();
ll temp = 1ll * siz1 * b[x] - sum1 + sum2 - 1ll * siz2 * b[x];
if(temp <= k) return 1;
return 0;
}
void solve(){
read(n), read(k);
for(int i = 1; i <= n; i++){
read(a[i]);
a[i] -= i;
b[i] = a[i];
}
sort(b + 1, b + n + 1);
int len = unique(b + 1, b + n + 1) - b - 1;
for(int i = 1; i <= n; i++){
a[i] = lower_bound(b + 1, b + len + 1, a[i]) - b;
}
s1.clear(), s2.clear();
siz1 = siz2 = 0;
sum1 = sum2 = 0;
int l = 1, ans = 1;
for(int i = 1; i <= n; i++){
add(a[i]);
while(l <= i && !check()){
del(a[l]), l ++;
}
ans = max(ans, i - l + 1);
}
printf("%d\n", ans);
return ;
}
int main(){
read(T);
while(T--){
solve();
}
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
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