QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#267246#4585. Greedy KnapsackNYCU_YamadaRE 0ms0kbC++203.7kb2023-11-27 03:04:452023-11-27 03:04:45

Judging History

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

  • [2023-11-27 03:04:45]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-11-27 03:04:45]
  • 提交

answer

#ifndef Yamada
#define Yamada
#include Yamada __FILE__ Yamada

const int B = 35;
const int maxc = 1 << 17;

set<pii> st[B];
int taw[B], tav[B];
int ans[maxc];

void solve() {
    int n, t; cin >> n >> t;
    vector<int> w(n), v(n);
    for (auto &x : w) cin >> x;
    for (auto &x : v) cin >> x;

    int idx = 0;
    for (int i = max(int(1), t - maxc); i <= t; ++i, ++idx) {
        int hb = __lg(i);
        st[hb].ee(i, idx);
    }

    for (int i = 0; i < n; ++i) {
        int hb = __lg(w[i]);
        while (SZ(st[hb])) {
            auto it = st[hb].end(); it = prev(it);
            pii cur = *it;
            if (cur.X - taw[hb] < w[i]) break;
            else {
                int nxtw = cur.X - w[i] - taw[hb];
                st[hb].erase(it);
                ans[cur.Y] += v[i] + tav[hb];
                // assert(nxtw >= 0);
                if (nxtw == 0) continue;

                int nxthb = __lg(nxtw);
                st[nxthb].ee(nxtw + taw[nxthb], cur.Y);
                ans[cur.Y] -= tav[nxthb];
            }
        }

        for (int j = hb + 1; j < B; ++j) {
            taw[j] += w[i], tav[j] += v[i];
            while (SZ(st[j])) {
                auto it  = st[j].begin();
                pii cur = *it;
                if (cur.X - taw[j] < (int(1) << j)) {
                    int nxtw = cur.X - taw[j];
                    st[j].erase(it);
                    ans[cur.Y] += tav[j];
                    // assert(nxtw >= 0);
                    if (nxtw == 0) continue;

                    int nxthb = __lg(nxtw);
                    st[nxthb].ee(nxtw + taw[nxthb], cur.Y);
                    ans[cur.Y] -= tav[nxthb];
                }
                else break;
            }
        }
    }

    for (int i = 0; i < B; ++i) {
        for (auto it : st[i]) {
            ans[it.Y] += tav[i];
            assert(__lg(it.X) == i);
            // assert(it.X - taw[i] >= 0 && __lg(it.X) == i);
        }
    }
    int tot = 0;
    for (int i = 0; i < idx; ++i) chmax(tot, ans[i]);
    print(tot);
}

signed main() {
    IOS();
    int t = 1; // cin >> t;
    for (int i=1;i<=t;++i) solve();
    return 0;
}

#else

#ifdef local 
#define _GLIBCXX_DEBUG 1
#endif
#pragma GCC optimize ("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define int int64_t
#define double __float80
using pii = pair<int, int>;
template <typename T> using MaxHeap = std::priority_queue<T>;
template <typename T> using MinHeap = std::priority_queue<T, vector<T>, greater<T>>;

#define SZ(a) ((int)(a).size())
#define ALL(a) begin(a), end(a)
#define RALL(a) rbegin(a), rend(a)
#define ee emplace
#define eb emplace_back
#define ef emplace_front
#define pb pop_back
#define pf pop_front
#define X first
#define Y second

#ifdef local
#define IOS() void()
#define debug(...) \
    fprintf(stderr, "\e[1;93m"), \
    fprintf(stderr, "At [%s], line %d: (%s) = ", __FUNCTION__, __LINE__, #__VA_ARGS__), \
    _do(__VA_ARGS__), \
    fprintf(stderr, "\e[0m")
template <typename T> void _do(T &&_t) {cerr << _t << '\n';}
template <typename T, typename ...U> void _do(T &&_t, U &&..._u) {cerr << _t << ", ", _do(_u...);}
#define print(...) \
    fprintf(stderr, "\e[1;96m"), \
    _P(__VA_ARGS__), \
    fprintf(stderr, "\e[0m")
#else
#define IOS() ios_base::sync_with_stdio(0); cin.tie(0)
#define debug(...) void()
#define print(...) _P(__VA_ARGS__)
#define endl '\n'
#endif

template <typename U, typename V> bool chmin(U &u, V v) {return u > v ? u = v, 1 : 0;}
template <typename U, typename V> bool chmax(U &u, V v) {return u < v ? u = v, 1 : 0;}

template <typename T> void _P(T &&_x) {cout << _x << '\n';}
template <typename T, typename ...S> void _P(T &&_x, S &&..._t) {cout << _x << " "; _P(_t...);}

#endif

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

5 10
10 1 2 3 4
1 1 1 1 1

output:


result: