QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#103536#6194. Knapsack ProblemHe_RenTL 26ms7272kbC++141.6kb2023-05-06 15:46:082023-05-06 15:46:12

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:46:12]
  • 评测
  • 测评结果:TL
  • 用时:26ms
  • 内存:7272kb
  • [2023-05-06 15:46:08]
  • 提交

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)

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);
	for(int i=0, cnt=7; 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;
			if(flag)
				useful[i].emplace_back(mask);
		}
		++cnt;
	}
	
	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: 26ms
memory: 7272kb

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: -100
Time Limit Exceeded

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:


result: