QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#177397 | #4585. Greedy Knapsack | ucup-team288# | WA | 20ms | 4552kb | C++20 | 3.8kb | 2023-09-12 22:12:54 | 2023-09-12 22:12:55 |
Judging History
answer
#include <bits/stdc++.h>
#undef KEV
using namespace std;
using i64 = long long;
using ll = long long;
#define pb emplace_back
#define X first
#define Y second
#define AI(i) begin(i), end(i)
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true);}
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true);}
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l++ << " \n"[l==r]; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 300010, MAX_K = 40;
ll add_v[MAX_K], add_w[MAX_K];
// [ l, r, value ]
// small comes out
set< tuple<ll,ll,ll> > pq[MAX_K];
//priority_queue< tuple<ll,ll,ll>, vector<tuple<ll,ll,ll>>, greater<>> > pq[MAX_K];
int safe_lg(ll v) {
return v == 0 ? -1 : __lg(v);
}
ll res;
void put(ll l, ll r, ll v) {
chmax(res, v);
chmax(l, 1ll);
if (l > r) return;
int k = safe_lg(l);
assert(k == safe_lg(r));
ll nl = l - add_w[k], nr = r - add_w[k], nv = v - add_v[k];
{
auto it = pq[k].lower_bound({nl, -1, -1});
if (it != end(pq[k])) {
auto [pl, pr, pv] = *it;
if (nr >= pl) {
if (nv >= pv) {
pq[k].erase(it);
if (pr > nr)
pq[k].insert({pr+1, nr, pv});
} else {
nr = pl-1;
}
}
}
}
{
auto it = pq[k].lower_bound({nl, -1, -1});
if (it != begin(pq[k])) {
--it;
auto [pl, pr, pv] = *it;
if (nl <= pr) {
if (nv >= pv) {
pq[k].erase(it);
if (pl < nl)
pq[k].insert({pl, nl-1, pv});
} else {
nl = pr+1;
}
}
}
}
if (nl <= nr)
pq[k].insert({nl, nr, nv});
}
void chk(int k) {
//DE(k);
while (pq[k].size()) {
auto [_l, _r, _v] = *pq[k].begin();
ll l = _l + add_w[k], r = _r + add_w[k], v = _v + add_v[k];
if (l >= (1ll << k)) break;
//DE(l, r, 1ll << k);
pq[k].erase(pq[k].begin());
if (l == 0) {
chmax(res, v);
if (r > l)
pq[k].insert({_l+1, _r, _v});
continue;
}
int nk = safe_lg(l);
ll nr = min(r, (2ll << nk)-1);
//DE(nr);
put(l, nr, v);
if (nr < r) {
assert(nr+1-add_w[k] <= _r);
pq[k].insert({nr+1 - add_w[k], _r, _v});
}
}
}
void obo(int k, ll dv, ll more) {
//DE("obo");
while (pq[k].size()) {
auto [_l, _r, _v] = *pq[k].rbegin();
//DE(_l, _r, _v);
ll l = _l + add_w[k], r = _r + add_w[k], v = _v + add_v[k];
if (r < dv) break;
pq[k].erase(prev(end(pq[k])));
if (r == dv) {
chmax(res, more + v);
if (r > l)
pq[k].insert({_l, _r-1, _v});
continue;
}
int nk = safe_lg(r - dv);
ll nr = r - dv;
ll nl = max(l - dv, 1ll << nk);
put(nl, nr, v + more);
DE(l, r, v, dv, nl, nr);
if (nl != l - dv) {
pq[k].insert({_l, nl+dv-1 - add_w[k], _v});
DE(_l, nl+dv-1 - add_w[k]);
//DE(_l, nl-1-add_w[k]);
assert(_l <= nl+dv-1-add_w[k]);
}
}
}
void dd() {
DE("DD--------------__");
for (int i = 0;i < MAX_K;++i) if (pq[i].size()) {
DE(i);
for (auto [_l, _r, _v] : pq[i]) {
DE(_l+add_w[i], _r+add_w[i], _v+add_v[i]);
}
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int N;
ll T;
cin >> N >> T;
vector<ll> w(N), v(N);
for (auto &i : w) cin >> i;
for (auto &i : v) cin >> i;
for (int i = 0;;++i) {
ll l = 1ll << i, r = min(T, (1ll<<(i+1))-1);
put(l, r, 0);
if (r == T) break;
}
for (int i = 0;i < N;++i) {
DE(w[i], v[i]);
int k = safe_lg(w[i]);
obo(k, w[i], v[i]);
for (++k; k < MAX_K;++k) {
add_v[k] += v[i];
add_w[k] += -w[i];
chk(k);
}
dd();
}
for (int k = 0;k < MAX_K;++k) {
//DE(k);
for (auto [_1, _2, _v] : pq[k])
chmax(res, _v + add_v[k]);
}
cout << res << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3504kb
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: 0ms
memory: 3520kb
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: 3536kb
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: 3464kb
input:
1 1 1 1
output:
1
result:
ok single line: '1'
Test #5:
score: -100
Wrong Answer
time: 20ms
memory: 4552kb
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:
100003
result:
wrong answer 1st lines differ - expected: '100002', found: '100003'