QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#103537#6194. Knapsack ProblemHe_RenTL 1917ms5596kbC++141.7kb2023-05-06 15:53:472023-05-06 15:53:51

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-06 15:53:51]
  • 评测
  • 测评结果:TL
  • 用时:1917ms
  • 内存:5596kb
  • [2023-05-06 15:53:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 15 + 5;
const int ALL = (1<<(4*4)) + 5;

#define bbit(i) (1<<(i))
#define bdig(x,i) (((x)>>(i))&1)
#define lowbit(x) ((x)&-(x))

ll a[MAXN], c[MAXN];

void solve(void)
{
	int d;
	scanf("%d",&d);
	for(int i=1; i<(1<<d); ++i)
		scanf("%lld",&c[i]);
	for(int i=0; i<d; ++i)
		scanf("%lld",&a[i]);
	
	static int trans[MAXN];
	for(int i=1; i<(1<<d); ++i)
	{
		trans[i] = 0;
		for(int j=0; j<d; ++j) if(bdig(i,j))
			trans[i] |= 1 << (4 * j);
	}
	
	int all = 1 << (4 * d);
	
	vector< vector<int> > useful(1<<d);
	{
		vector<int> cnt(d, 7);
		for(int i=1; i<(1<<d); ++i)
		{
			for(int mask=all-1; mask>=0; --mask)
			{
				bool flag = 1;
				for(int j=0; j<d; ++j)
					flag &= ((mask >> (4 * j)) & 15) <= cnt[j];
				if(flag)
					useful[i].emplace_back(mask);
			}
			for(int j=0; j<d; ++j)
				cnt[j] += bdig(i, j);
		}
	}
	
	const int lb = 30;
	
	static ll f[ALL];
	memset(f, 0xc0, sizeof(f));
	f[0] = 0;
	for(int k=0; k<lb; ++k)
	{
		for(int i=1; i<(1<<d); ++i)
			for(int mask: useful[i]) if(f[mask] >= 0)
				f[mask + trans[i]] = max(f[mask + trans[i]], f[mask] + (c[i] << k));
		
		static ll nf[ALL];
		memset(nf, 0xc0, sizeof(nf));
		for(int mask=0; mask<all; ++mask) if(f[mask] >= 0)
		{
			bool flag = 1;
			int nxt = 0;
			for(int i=0; i<d; ++i)
			{
				int x = (mask >> (4 * i)) & 15;
				if((x & 1) != ((a[i] >> k) & 1))
				{
					flag = 0;
					break;
				}
				nxt |= (x >> 1) << (4 * i);
			}
			if(flag)
				nf[nxt] = f[mask];
		}
		memcpy(f, nf, sizeof(f));
	}
	
	printf("%lld\n",f[0]);
}

int main(void)
{
	int T;
	scanf("%d",&T);
	while(T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 20ms
memory: 5596kb

input:

3
2
1 2 4
4 5
3
3226252 19270256 2430652 1187613 12496062 10286165 17494834
24 85 34
4
901133 6693218 13245489 14740234 16186210 11382783 19277321 3855635 16523184 10168517 16195031 971041 10303441 8395899 11618555
529321239 218214127 92942310 207467810

output:

18
1949753378
7832404777567179

result:

ok 3 number(s): "18 1949753378 7832404777567179"

Test #2:

score: 0
Accepted
time: 1917ms
memory: 5556kb

input:

100
4
4488409 9842893 14331290 11289097 15777479 21131954 25620334 6146092 10634508 15988956 20477387 17435190 21923584 27278027 31766493
200477197 258791439 590664017 982949628
4
5484152 19851747 25335961 6555937 12039831 26407861 31891996 10897184 16381166 30749333 36233145 17453055 22937114 37304...

output:

16156444320083421
24565535429631234
29418802996321901
30685727712782520
21026118053726857
27272339796311010
28831028447377015
34577176834491128
9058894962996375
21525085254465501
6657186892229297
16750628911275484
11217846865301187
12674726744043780
33873234230125448
3890237279573366
319038768614465...

result:

ok 100 numbers

Test #3:

score: 0
Accepted
time: 1905ms
memory: 5576kb

input:

100
4
11618334 18295966 29914477 5116382 16734869 23412289 35030567 15002473 26620994 33298395 44916782 20118655 31737127 38414456 50032872
52573649 733715025 105771527 204824011
4
2142433 13679527 15822005 7304783 9447231 20984363 23126840 6271380 8413827 19950941 22093291 13576168 15718643 2725577...

output:

17648887427676796
21625331519839488
5123483163674640
20332898488476516
10882410276737710
25799406290369942
9021794681785918
14079661025657823
19651479225495798
19108340996997031
7502197529195960
14859286025000990
26534486448012504
21196964793845212
17222108748663918
19112873745810973
150116234459639...

result:

ok 100 numbers

Test #4:

score: 0
Accepted
time: 1839ms
memory: 5548kb

input:

100
4
19912840 5584811 25497404 10173442 30086113 15758284 35670730 10055784 29968204 15640365 35553036 20228929 40141706 25813818 45726645
199637398 648830099 620879036 721665690
4
7571016 7507237 15078004 19283701 26854580 26790914 34361725 2809630 10380467 10316661 17887650 22093187 29664080 2960...

output:

21172351446279597
12216316207781859
31685774482329793
36066732654517313
19340822375673782
17094361082328640
19075803268324489
23946411427126244
15750520513128720
10839993372177143
28038252774564427
12098359408203383
16661717061725678
16141878677697603
10422881545500853
23195259150880456
161145733965...

result:

ok 100 numbers

Test #5:

score: 0
Accepted
time: 1868ms
memory: 5548kb

input:

100
4
16976932 14037962 31014864 10197492 27174564 24235473 41212439 18912005 35888926 32949719 49926702 29109585 46086485 43147299 60124270
491925338 268977876 990762353 88764265
4
12999234 10104744 23104060 1196740 14196165 11301451 24300745 3216693 16215855 13321348 26320916 4413380 17412748 1451...

output:

23909367397934013
13788681506670460
23733351389222421
14554685971742458
21700214066873395
14675080284915330
33895419440563729
29492673616768327
11303756472296747
12482610599433273
14710253245102988
30271020171688425
9134330469412563
33975992434656396
17398716116891428
24545212510786538
3194464768874...

result:

ok 100 numbers

Test #6:

score: 0
Accepted
time: 1871ms
memory: 5568kb

input:

100
4
19074076 1326772 20400846 10221739 29295815 11548536 30622598 8932278 28006379 10259062 29333181 19154053 38228108 20480879 39554936
638989087 594158358 505869863 605605944
4
4624862 3932439 8557338 1945542 6570437 5877999 10502871 4787691 9412557 8720153 13345006 6733262 11358122 10665669 152...

output:

23556800109725722
10537032002627174
14660751629073194
20011815419177543
22625965024389053
11033496376968227
17546765754659334
27441279970436335
34448070575909396
25397224562147617
10757782582321602
16008699146950819
10145064828196714
41523392253608928
20626446413714925
32072152342903207
720635465608...

result:

ok 100 numbers

Test #7:

score: -100
Time Limit Exceeded

input:

100
4
1171312 9779739 10950939 15278965 16449994 25058569 26229921 2821649 3992612 12601354 13772604 18100248 19271474 27880146 29051284
786052835 69081944 20977372 827480328
4
15086110 17760131 32846218 13924280 29010738 31684453 46770885 15128745 30215077 32888809 47975302 29053233 44139622 468134...

output:


result: