QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#321305#7904. Rainbow SubarrayhazeCompile Error//C++232.5kb2024-02-04 13:24:022024-02-04 13:24:03

Judging History

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

  • [2024-06-09 00:00:26]
  • hack成功,自动添加数据
  • (/hack/652)
  • [2024-02-04 13:24:03]
  • 评测
  • [2024-02-04 13:24:02]
  • 提交

answer

/*
 Rainbow Subarray
 https://codeforces.com/gym/104901/problem/K
 Author: haze
*/

#include <bits/stdc++.h>
#define int long long
#define irep(i, l, r) for(int i = (l); i <= (r); ++ i)
#define drep(i, r, l) for(int i = (r); i >= (l); -- i)
#define IOS ios::sync_with_stdio(false), cin.tie(nullptr);
using namespace std;
typedef long long ll;

inline ll read() {
    ll s = 0;
    bool fl = false;
    char ch = (char) getchar();
    while (!isdigit(ch)) {
        if (ch == '-')fl = true;
        ch = (char) getchar();
    }
    while (isdigit(ch)) {
        s = s * 10 + (ch ^ 48);
        ch = (char) getchar();
    }
    return fl ? -s : s;
}

const int mod = 1000000000 + 7;
const int itinf = 1000000999;
const ll llinf = 2e18;
const int N = 500099;

struct DS{
    multiset<ll>sl, sr;
    ll suml, sumr;
    DS(){suml = sumr = 0;}
    void adjust(){
        ll n = sl.size() + sr.size();// l fl n / 2, r ce n / 2;
        while(sl.size() < n / 2){
            suml += *sr.begin();
            sumr -= *sr.begin();
            sl.emplace(*sr.begin());
            sr.erase(sr.begin());
        }
        while(sl.size() > n / 2){
            sumr += *(-- sl.end());
            suml -= *(-- sl.end());
            sr.emplace(*(-- sl.end()));
            sl.erase(-- sl.end());
        }
    }
    void insert(ll x){
        if(sr.empty() || x >= *sr.begin()){
            sr.emplace(x);
            sumr += x;
        }
        else{
            sl.emplace(x);
            suml += x;
        }
        adjust();
    }
    void del(ll x){
        if(sl.contains(x)){
            suml -= x;
            sl.erase(sl.find(x));
        }
        if(sr.contains(x)){
            sumr -= x;
            sr.erase(sr.find(x));
        }
        adjust();
    }
};

void solve() {
    ll n = read(), k = read();
    vector<ll>a(n);
    irep(i, 0, n - 1)
        a[i] = read() - i;
    ll ans = 0;
    ll l = 0, r = 0;
    DS u;
    while(r <= n){
        ll sr = u.sumr - u.suml;
        if((r - l) % 2 == 1)sr -= *u.sr.begin();
        if(sr <= k){
            ans = max(ans, r - l);
            if(r == n)break;
            u.insert(a[r ++]);
        }
        else{
            u.del(a[l ++]);
        }
    }
//    if(ans == 40385){
//        cout << ans - 1 << endl;
//    }
    cout << ans << '\n';
}

int main() {
    // IOS
    int T = read();
    while (T--) {
        solve();
    }
    return 0;
}
//7 1 3 2 0 6 1

詳細信息

cc1plus: error: ‘::main’ must return ‘int’