QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#314779#7904. Rainbow Subarrayucup-team1087Compile Error//C++142.6kb2024-01-26 10:58:072024-01-26 10:58:07

Judging History

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

  • [2024-06-09 00:00:26]
  • hack成功,自动添加数据
  • (/hack/652)
  • [2024-01-26 10:58:07]
  • 评测
  • [2024-01-26 10:58:07]
  • 提交

answer

#include <iostream>
#include <iomanip>
#include <cmath>
#include <climits>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <random>
#include <set>
#include <map>
#include <vector>
#include <array>
#include <bitset>
#include <queue>
#include <stack>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

template <class T>
T uread() {
    char c = getchar();
    while (c < '0' || c > '9') {
        c = getchar();
    }
    T num = 0;
    while (c >= '0' && c <= '9') {
        num = (num << 1) + (num << 3) + (c ^ 48);
        c = getchar();
    }
    return num;
}

const int N = 5e5 + 1;

struct TwoSet {
    ll sum1, sum2;
    multiset<int> st1, st2;
    TwoSet() {
        sum1 = sum2 = 0;
    }
    void balance() {
        if (st2.size() < st1.size()) {
            int x = *st1.rbegin();
            st2.insert(x); sum2 += x;
            sum1 -= x; st1.erase(--st1.end());
        } else if (st1.size() + 1 < st2.size()) {
            int x = *st2.begin();
            st1.insert(x); sum1 += x;
            sum2 -= x; st2.erase(st2.begin());
        }
    }
    void push(int x) {
        if (empty() || x > *st2.begin()) {
            st2.insert(x); sum2 += x;
        } else {
            st1.insert(x); sum1 += x;
        }
        balance();
    }
    void del(int x) {
        auto itr = st1.find(x);
        if (itr != st1.end()) {
            st1.erase(itr); sum1 -= x;
        } else {
            st2.extract(x); sum2 -= x;
        }
        balance();
    }
    int getMid() const {
        return *st2.begin();
    }
    bool empty() const {
        return st1.empty() && st2.empty();
    }
};

int a[N];

inline ll calc(const TwoSet &ts, int l, int r) {
    if (ts.empty()) {
        return 0;
    }
    int mid = ts.getMid();
    return mid * ts.st1.size() - ts.sum1 + ts.sum2 - mid * ts.st2.size();
}

void solve() {
    int n = uread<int>(); ll k = uread<ll>();
    for (int i = 1; i <= n; ++i) {
        a[i] = uread<int>() - i;
    }
    
    TwoSet ts;
    int ans = 0, r = 0;
    for (int i = 1; i <= n; ++i) {
        while (r <= n && calc(ts, i, r) <= k) {
            ts.push(a[++r]);
        }
        ans = max(ans, r - i);
        ts.del(a[i]);
    }
    printf("%d\n", ans);
}

int main(int argc, const char * argv[]) {
    int T;
    scanf("%d", &T);
    while (T--) {
        solve();
    }
    return 0;
}

詳細信息

answer.code: In member function ‘void TwoSet::del(int)’:
answer.code:71:17: error: ‘class std::multiset<int>’ has no member named ‘extract’
   71 |             st2.extract(x); sum2 -= x;
      |                 ^~~~~~~
answer.code: In function ‘int main(int, const char**)’:
answer.code:113:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  113 |     scanf("%d", &T);
      |     ~~~~~^~~~~~~~~~