QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#177444#4585. Greedy Knapsackucup-team288#WA 235ms5648kbC++204.6kb2023-09-12 23:19:172023-09-12 23:19:17

Judging History

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

  • [2023-09-12 23:19:17]
  • 评测
  • 测评结果:WA
  • 用时:235ms
  • 内存:5648kb
  • [2023-09-12 23:19:17]
  • 提交

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];
	while (true) {
		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({nr+1, pr, pv});
				} else {
					nr = pl-1;
				}
			} else break; 
		} else break;
	}
	{
		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);
			//put(nr+1, r, v);
			pq[k].insert({nr+1 - add_w[k], _r, _v});
			assert(nr+1 - add_w[k] <= _r);
		}
	}
}
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]);
		}
	}
}

ll brute(ll n, ll T, vector<ll> w, vector<ll> v) {
	ll ret = 0;
	for (int i = 1;i <= T;++i) {
		ll cw = 0, cv = 0;
		for (int j = 0;j < n;++j)
			if (cw + w[j] <= i)
				cw += w[j], cv += v[j];
		chmax(ret, cv);
	}
	return ret;
}

random_device rd;
mt19937 gen(rd());

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;

//
//	N = 100, T = 5000;
//
//
//	vector<ll> w(N), v(N);
//	for (auto &i : w) i = gen() % 100 + 1;
//	for (auto &i : v) i = gen() % 100 + 1;
//

	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';
//	DE(brute(N, T, w, v));
//	cout << N << ' ' << T << '\n';
//	for (int i = 0;i < N;++i)
//		cout << w[i] << ' ';
//	cout << '\n';
//	for (int i = 0;i < N;++i)
//		cout << v[i] << ' ';
//	cout << endl;
//			
	//assert(res == brute(N, T, w, v));

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 3556kb

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: 3444kb

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: 0ms
memory: 3516kb

input:

1 1
1
1

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 17ms
memory: 4632kb

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: 0
Accepted
time: 235ms
memory: 5648kb

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:

881743221

result:

ok single line: '881743221'

Test #7:

score: -100
Wrong Answer
time: 69ms
memory: 4832kb

input:

95787 282807627
71790 93372 34507 85540 25871 70820 42496 67633 50879 43192 861 57228 54094 91534 94590 24032 52665 82236 52010 45017 15045 76804 39159 72380 28162 89590 31125 21390 37395 34002 26585 2985 90847 12060 63128 73061 34524 61847 69734 53329 39554 90867 13357 18202 63266 44796 64191 40066...

output:

287154094

result:

wrong answer 1st lines differ - expected: '287223570', found: '287154094'