QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#703928#5417. Chat ProgramKdlyhTL 2475ms12736kbC++204.7kb2024-11-02 18:54:342024-11-02 18:54:40

Judging History

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

  • [2024-11-02 18:54:40]
  • 评测
  • 测评结果:TL
  • 用时:2475ms
  • 内存:12736kb
  • [2024-11-02 18:54:34]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <stack>

using namespace std;

#ifdef LOCAL
template <class T, size_t size = std::tuple_size<T>::value> std::string to_debug(T, std::string s = "") requires(not std::ranges::range<T>);
std::string to_debug(auto x) requires requires(std::ostream& os) { os << x; } { return static_cast<std::ostringstream>(std::ostringstream() << x).str(); }
std::string to_debug(std::ranges::range auto x, std::string s = "") requires(not std::is_same_v<decltype(x), std::string>) {
  for (auto xi : x) { s += ", " + to_debug(xi); }
  return "[" + s.substr(s.empty() ? 0 : 2) + "]";
}
template <class T, size_t size> std::string to_debug(T x, std::string s) requires(not std::ranges::range<T>) {
  [&]<size_t... I>(std::index_sequence<I...>) { ((s += ", " + to_debug(get<I>(x))), ...); }(std::make_index_sequence<size>());
  return "(" + s.substr(s.empty() ? 0 : 2) + ")";
}
#define debug(...) std::cerr << __LINE__ << ": (" #__VA_ARGS__ ") = " << to_debug(std::tuple(__VA_ARGS__)) << "\n"
#else
#define debug(x...)
#endif

using i64 = long long;

template<class T>
struct Discreater {
    std::vector<T> elementSet;

    Discreater(const std::vector<T> &a) : elementSet(a) {
        sort(begin(elementSet), end(elementSet));
        elementSet.erase(unique(begin(elementSet), end(elementSet)), end(elementSet));
    }

    vector<int> process(const vector<T>& a) const {
        vector<int> disRes(size(a));
        for (int i = 0; i < size(a); i++) {
            disRes[i] = query(a[i]);
        }
        return disRes;
    }

    int query(const T& x) const {
        auto it{lower_bound(begin(elementSet), end(elementSet), x)};
        return it - begin(elementSet);
    }

    T queryInv(int index) const {
        return elementSet[index];
    }

    int getSize() {return (int)size(elementSet);}

};

inline int __lg(int __n) { return sizeof(int) * __CHAR_BIT__  - 1 - __builtin_clz(__n); }

template<typename T>
struct Fenwick {
    int n;
    vector<T> a;

    Fenwick(int n_ = 0) {init(n_);}

    void init(int n_) {n = n_; a.assign(n, T{});}

    void add(int x, const T& v) {
        for (int i = x + 1; i <= n; i += i & -i) {
            a[i - 1] = a[i - 1] + v;
        }
    }

    T sum(int x) {
        T ans{};
        for (int i = x; i > 0; i -= i & -i) {
            ans = ans + a[i - 1];
        }
        return ans;
    }

    T rangeSum(int l, int r) {
        return sum(r) - sum(l);
    }

};

void solve()
{
    int n, k, m, c, d; std::cin >> n >> k >> m >> c >> d;
    vector<i64> a(n); for (auto& ai : a) {std::cin >> ai;}

    vector<i64> da{a}; i64 add{}; for (int i = 0; i < n; i++) {da[i] += add; add += d;}
    
    auto check = [&](i64 T) {//大于等于 T - c, T - c + d, T - c + 2d... 的数
        
        //扔入离散化
        vector<i64> initHC{da}; 
        {
            i64 tar{T - c}; for (int i = 0; i < n - m + 1; i++) {initHC.push_back(tar); tar += d;} initHC.push_back(T);
        }
        Discreater<i64> discreaterHC(initHC);//给滑窗init

        const int OPHC{discreaterHC.getSize() + 1}; 
        Fenwick<int> segtHC(OPHC);

        //说白了, YS 就是要比较滑窗外 ai >= T 的个数

        for (int i = 0; i < m; i++) {segtHC.add(discreaterHC.query(da[i]), 1);}
        int cntYS{}; for (int i = m; i < n; i++) {cntYS += (a[i] >= T);}

        i64 tarHC{T - c}, tarYS{T}; for (int r = m; r < n; r++) {
            int cntHC{segtHC.rangeSum(discreaterHC.query(tarHC), OPHC)};
            if (cntHC + cntYS >= k) {return true;}
            cntYS -= (a[r] >= tarYS);
            cntYS += (a[r - m] >= tarYS); 
            segtHC.add(discreaterHC.query(da[r]), 1); 
            segtHC.add(discreaterHC.query(da[r - m]), -1);
            tarHC += d;
        }
        if (segtHC.rangeSum(discreaterHC.query(tarHC), OPHC) + cntYS >= k) {
            return true;
        }
        return false;
    };

    i64 lo{}, hi{(i64)1E18}; while (lo <= hi) {
        i64 mid{lo + hi >> 1}; check(mid) ? lo = mid + 1 : hi = mid - 1;
    }

    std::cout << lo - 1 << "\n";

}
 
signed main()
{
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int _{1};
#ifdef tests
    std::cin >> _;
#endif
    while(_--) solve();
    return 0;
}


详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3616kb

input:

6 4 3 1 2
1 1 4 5 1 4

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3840kb

input:

7 3 2 4 0
1 9 1 9 8 1 0

output:

9

result:

ok 1 number(s): "9"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

8 3 5 0 0
2 0 2 2 1 2 1 8

output:

2

result:

ok 1 number(s): "2"

Test #4:

score: 0
Accepted
time: 2475ms
memory: 12708kb

input:

200000 200000 100000 0 1000000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

output:

0

result:

ok 1 number(s): "0"

Test #5:

score: 0
Accepted
time: 1871ms
memory: 12736kb

input:

200000 1 100000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 100000000...

output:

100001000000000

result:

ok 1 number(s): "100001000000000"

Test #6:

score: 0
Accepted
time: 856ms
memory: 11808kb

input:

200000 1 200000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 100000000...

output:

200001000000000

result:

ok 1 number(s): "200001000000000"

Test #7:

score: -100
Time Limit Exceeded

input:

200000 24420 17993 881138881 700368758
231187558 519018952 260661004 740633836 931672020 155904999 647179942 13217847 779799803 382810661 242588977 708308843 309853544 225488875 389115097 588643904 644409212 704920939 231829287 39891424 881158891 341251089 486868469 808002305 629160633 317239613 771...

output:


result: