QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#177524#4585. Greedy Knapsackucup-team288TL 225ms5768kbC++205.1kb2023-09-13 01:22:542023-09-13 01:22:54

Judging History

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

  • [2023-09-13 01:22:54]
  • 评测
  • 测评结果:TL
  • 用时:225ms
  • 内存:5768kb
  • [2023-09-13 01:22:54]
  • 提交

answer

#include <bits/stdc++.h>
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];

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 (pl == nr+1 && pv == nv) {
        pq[k].erase(it);
        nr = pr;
        break;
      }

			if (nr >= pl) {
				if (nv >= pv) {
					pq[k].erase(it);
					if (pr > nr)
						pq[k].insert({nr+1, pr, pv});
				} else {
          if (nr > pr)
            put(pr+1 + add_w[k], r, v);
					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});
          if (pr > nr)
            pq[k].insert({nr+1, pr, 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());

//#define DBG
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int N;
	ll T;
#ifndef DBG
	cin >> N >> T;
	vector<ll> w(N), v(N);
	for (auto &i : w) cin >> i;
	for (auto &i : v) cin >> i;


#else
  while (true) {
     memset(add_v, 0, sizeof(add_v));
     memset(add_w, 0, sizeof(add_w));
     for (int i = 0;i < MAX_K;++i)
       pq[i].clear();
     res = 0;
     N = 10, T = 20;


     vector<ll> w(N), v(N);
     for (auto &i : w) i = gen() % 10 + 1;
     for (auto &i : v) i = gen() % 10 + 1;
#endif


    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';
#ifdef DBG
    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));
  }
#endif

}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3588kb

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

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

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

input:

1 1
1
1

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 20ms
memory: 4712kb

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: 225ms
memory: 5768kb

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: 0
Accepted
time: 62ms
memory: 4988kb

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:

287223570

result:

ok single line: '287223570'

Test #8:

score: 0
Accepted
time: 15ms
memory: 4644kb

input:

100000 100001
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:

100001

result:

ok single line: '100001'

Test #9:

score: 0
Accepted
time: 19ms
memory: 4716kb

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 #10:

score: 0
Accepted
time: 23ms
memory: 4964kb

input:

100000 101603
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:

100001

result:

ok single line: '100001'

Test #11:

score: 0
Accepted
time: 36ms
memory: 4860kb

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 #12:

score: 0
Accepted
time: 36ms
memory: 4936kb

input:

100000 109258
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:

100001

result:

ok single line: '100001'

Test #13:

score: 0
Accepted
time: 29ms
memory: 5136kb

input:

100000 200000
100000 99997 99994 99991 99988 99985 99983 99980 99977 99974 99972 99969 99966 99963 99961 99958 99955 99952 99950 99947 99944 99942 99939 99936 99933 99930 99928 99926 99923 99920 99917 99914 99912 99909 99906 99904 99901 99899 99897 99894 99891 99889 99886 99883 99881 99879 99876 998...

output:

100003

result:

ok single line: '100003'

Test #14:

score: 0
Accepted
time: 27ms
memory: 5072kb

input:

100000 104441
100000 99997 99994 99991 99988 99985 99983 99980 99977 99974 99972 99969 99966 99963 99961 99958 99955 99952 99950 99947 99944 99942 99939 99936 99933 99930 99928 99926 99923 99920 99917 99914 99912 99909 99906 99904 99901 99899 99897 99894 99891 99889 99886 99883 99881 99879 99876 998...

output:

100002

result:

ok single line: '100002'

Test #15:

score: 0
Accepted
time: 18ms
memory: 4724kb

input:

100000 200000
100000 99993 99987 99978 99972 99965 99960 99954 99948 99944 99941 99931 99929 99919 99910 99904 99894 99888 99877 99871 99863 99858 99852 99850 99843 99839 99829 99823 99812 99807 99795 99791 99780 99774 99766 99755 99750 99742 99731 99725 99718 99711 99702 99696 99693 99689 99678 996...

output:

100002

result:

ok single line: '100002'

Test #16:

score: 0
Accepted
time: 18ms
memory: 4664kb

input:

100000 100220
100000 99993 99987 99978 99972 99965 99960 99954 99948 99944 99941 99931 99929 99919 99910 99904 99894 99888 99877 99871 99863 99858 99852 99850 99843 99839 99829 99823 99812 99807 99795 99791 99780 99774 99766 99755 99750 99742 99731 99725 99718 99711 99702 99696 99693 99689 99678 996...

output:

100001

result:

ok single line: '100001'

Test #17:

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

input:

1 1
2
100000

output:

0

result:

ok single line: '0'

Test #18:

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

input:

100000 200000
100000 99991 99969 99958 99945 99932 99930 99912 99899 99891 99875 99857 99845 99830 99814 99802 99783 99766 99754 99733 99729 99726 99713 99696 99675 99665 99646 99642 99625 99621 99612 99605 99590 99585 99569 99566 99559 99552 99543 99528 99513 99497 99485 99478 99476 99457 99435 994...

output:

100003

result:

ok single line: '100003'

Test #19:

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

input:

100000 100012
100000 99991 99969 99958 99945 99932 99930 99912 99899 99891 99875 99857 99845 99830 99814 99802 99783 99766 99754 99733 99729 99726 99713 99696 99675 99665 99646 99642 99625 99621 99612 99605 99590 99585 99569 99566 99559 99552 99543 99528 99513 99497 99485 99478 99476 99457 99435 994...

output:

100002

result:

ok single line: '100002'

Test #20:

score: 0
Accepted
time: 15ms
memory: 4648kb

input:

100000 104768
42198 63876 70289 97679 99129 99383 99678 99811 99998 99999 99999 99999 100000 100000 100000 12433 8169 3583 222 37 35 9 2 1 1 1 1 1 1 1 65163 81308 91468 93404 95453 99894 99909 99998 99999 99999 99999 99999 100000 100000 100000 23740 11045 4364 2504 1858 1225 1157 1071 505 76 5 2 2 2...

output:

45

result:

ok single line: '45'

Test #21:

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

input:

100000 104790
66799 77219 97947 99699 99727 99872 99924 99980 99989 99991 100000 100000 100000 100000 100000 37709 33873 24409 1661 1157 1097 410 252 224 122 94 14 7 4 4 58118 97059 99179 99309 99324 99815 99829 99932 99933 99942 99971 99972 99992 99995 99995 71139 43023 15781 14293 3416 975 325 163...

output:

104790

result:

ok single line: '104790'

Test #22:

score: 0
Accepted
time: 14ms
memory: 4808kb

input:

100000 200000
59927 72467 75688 92437 93614 94992 97911 98151 98487 98938 99612 99947 99956 99995 99999 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 ...

output:

632

result:

ok single line: '632'

Test #23:

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

input:

100000 200000
86758 94096 95161 96612 98162 98338 98548 99356 99854 99996 99999 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100...

output:

200000

result:

ok single line: '200000'

Test #24:

score: 0
Accepted
time: 16ms
memory: 5240kb

input:

100000 150000
3841 4238 30552 47243 50833 93202 96257 97547 98352 98668 99000 99932 99987 99991 99992 99997 99998 99998 99998 99999 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 ...

output:

3855

result:

ok single line: '3855'

Test #25:

score: 0
Accepted
time: 24ms
memory: 5520kb

input:

100000 150000
10723 22346 89189 97960 99966 99968 99969 99985 99994 99994 99997 99997 99997 99999 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000...

output:

150000

result:

ok single line: '150000'

Test #26:

score: -100
Time Limit Exceeded

input:

90108 9829673297
3 3 5 5 5 5 5 7 9 11 13 15 15 17 17 19 20 22 22 22 22 24 25 25 25 27 28 30 30 31 32 34 34 35 35 36 37 38 39 41 42 44 44 46 48 49 49 50 51 52 53 55 55 56 57 59 61 63 65 67 69 70 71 72 72 73 75 76 77 77 78 78 80 80 80 80 81 81 81 81 82 82 82 84 86 87 89 89 90 90 90 90 92 94 95 97 99 1...

output:


result: