QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#699101#7904. Rainbow SubarrayKotomiWA 134ms5772kbC++172.2kb2024-11-02 01:14:512024-11-02 01:14:53

Judging History

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

  • [2024-11-02 01:14:53]
  • 评测
  • 测评结果:WA
  • 用时:134ms
  • 内存:5772kb
  • [2024-11-02 01:14:51]
  • 提交

answer

#include <bits/stdc++.h>

#define multi_test 1
#define DEBUG 0

using LL = long long;
using LD = long double;
using PII = std::pair<int, int>;

template <typename T> void chkmax(T &a, const T &b) {
  if (a < b) {
    a = b;
  }
}

struct midnum {
  std::multiset<int> big, small;
  LL bigsum, smallsum;

  midnum() : bigsum(0), smallsum(0) {}

  void balance() {
    while (big.size() > small.size()) {
      smallsum += *big.begin();
      bigsum -= *big.begin();
      small.insert(big.extract(big.begin()).value());
    }
    while (small.size() > big.size() + 1) {
      bigsum += *small.rbegin();
      smallsum -= *small.rbegin();
      big.insert(small.extract(small.find(*small.rbegin())).value());
    }
  }

  void insert(int x) {
    if (small.empty() or x <= *small.rbegin()) {
      small.insert(x);
      smallsum += x;
    } else {
      big.insert(x);
      bigsum += x;
    }
    balance();
  }

  void erase(int x) {
    if (small.count(x)) {
      small.erase(x);
      smallsum -= x;
    } else {
      big.erase(x);
      bigsum -= x;
    }
    balance();
  }

  int getmid() { return *small.rbegin(); }
};

void solve() {
  int n;
  LL k;
  std::cin >> n >> k;

  std::vector<int> a(n + 1);
  for (int i = 1; i <= n; ++i) {
    std::cin >> a[i];
    a[i] -= i;
  }

  midnum m_mid;

  auto check = [&](int l, int r) -> bool {
    if (l == r)
      return true;
    int mid = m_mid.getmid();
    int smallsize = m_mid.small.size(), bigsize = m_mid.big.size();
    LL smallsum = m_mid.smallsum, bigsum = m_mid.bigsum;
    LL cost = (LL)smallsize * mid - smallsum + bigsum - (LL)bigsize * mid;
    return cost <= k;
  };

  int res = 0;
  int l = 1, r = 1;
  auto upd = [&]() { chkmax(res, r - l + 1); };

  while (r <= n) {
    m_mid.insert(a[r]);
    while (not check(l, r)) {
      m_mid.erase(a[l++]);
    }
    upd();
    ++r;
  }

  std::cout << res << '\n';
}

int main() {
  std::cin.tie(nullptr)->sync_with_stdio(false);
  if constexpr (DEBUG) {
    std::cerr.tie(nullptr);
    freopen("data.in", "r", stdin);
  }

  int T = 1;
  if constexpr (multi_test) {
    std::cin >> T;
  }

  while (T--) {
    solve();
  }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3608kb

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: 134ms
memory: 5772kb

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
2
7
8
7
7
1
7
6
2
4
3
1
6
7
7
3
4
3
9
3
8
6
6
3
1
6
3
1
2
4
6
4
6
4
1
4
7
1
6
3
5
6
6
1
7
5
3
1
6
4
5
3
2
2
6
2
3
10
1
4
3
2
4
5
1
7
5
5
5
8
5
3
6
3
5
5
8
5
4
5
2
1
5
2
3
3
4
8
1
3
1
2
2
8
3
1
6
8
1
8
4
5
6
6
8
4
8
3
2
8
4
5
6
2
6
2
4
1
5
4
5
3
2
4
1
2
1
4
5
8
3
7
3
3
3...

result:

wrong answer 11002nd lines differ - expected: '517', found: '515'