QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#267273#4585. Greedy KnapsackNYCU_YamadaTL 94ms21800kbC++203.6kb2023-11-27 03:40:372023-11-27 03:40:37

Judging History

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

  • [2023-11-27 03:40:37]
  • 评测
  • 测评结果:TL
  • 用时:94ms
  • 内存:21800kb
  • [2023-11-27 03:40:37]
  • 提交

answer

#ifndef Yamada
#define Yamada
#include Yamada __FILE__ Yamada

const int B = 36;
const int maxc = 1 << 18;

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 = 9; i <= 9; ++i, ++idx) {
        int hb = __lg(i);
        st[hb].ee(i, idx);
    }
    */
    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;
            int c = cur.X - taw[hb];
            if (c < w[i]) break;

            ans[cur.Y] += tav[hb] + v[i];

            if (c > w[i]) {
                int nxtb = __lg(c - w[i]);
                ans[cur.Y] -= tav[nxtb];
                st[nxtb].ee(c - w[i] + taw[nxtb], cur.Y);
            }
            st[hb].erase(cur);
        }
        if (!SZ(st[hb])) taw[hb] = tav[hb] = 0;

        for (int j = hb + 1; j < B; ++j) {
            if (SZ(st[j])) {
                taw[j] += w[i], tav[j] += v[i];
            }
            while (SZ(st[j])) {
                pii cur = *st[j].begin();
                int c = cur.X - taw[j];
                int nxtb = __lg(c);

                if (nxtb == j) break;
                st[j].erase(cur);
                ans[cur.Y] += tav[j] - tav[nxtb];
                st[nxtb].ee(c + taw[nxtb], cur.Y);
            }
            if (!SZ(st[j])) taw[j] = tav[j] = 0;
        }
    }

    for (int i = 0; i < B; ++i) {
        for (auto it : st[i]) {
            ans[it.Y] += tav[i];
            // debug(__lg(it.X - taw[i]), i, it.X, it.Y);
            // assert(it.X - taw[i] >= 0 && __lg(it.X - taw[i]) == 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

详细

Test #1:

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

input:

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

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 59ms
memory: 21800kb

input:

5 10000000000
10 1 2 3 4
30 2 15 7 11

output:

65

result:

ok single line: '65'

Test #3:

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

input:

5 20
4 9 5 1 3
203 175 131 218 304

output:

900

result:

ok single line: '900'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3428kb

input:

1 1
1
1

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 94ms
memory: 18748kb

input:

100000 200000
100000 99998 99996 99994 99992 99990 99988 99986 99984 99982 99980 99978 99976 99974 99972 99970 99968 99966 99964 99962 99960 99958 99956 99954 99952 99950 99948 99946 99944 99942 99940 99938 99936 99934 99932 99930 99928 99926 99924 99922 99920 99918 99916 99914 99912 99910 99908 999...

output:

100002

result:

ok single line: '100002'

Test #6:

score: -100
Time Limit Exceeded

input:

92455 883403359
82193 96168 42737 25557 5699 8922 41136 82789 26249 74241 57030 29989 41743 78491 37281 60598 23165 51802 13911 88911 55220 25398 60154 2879 14519 61138 16806 15952 83710 44076 43551 41425 11055 3321 59105 34722 60133 13735 36785 73444 92250 3613 35787 10798 35612 9564 42531 49012 83...

output:


result: