QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#103552#6194. Knapsack ProblemHe_RenWA 165ms3764kbC++141.8kb2023-05-06 16:42:362023-05-06 16:42:40

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 16:42:40]
  • 评测
  • 测评结果:WA
  • 用时:165ms
  • 内存:3764kb
  • [2023-05-06 16:42:36]
  • 提交

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 << (3 * 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 << (3 * j);
	}
	
	int all = 1 << (3 * d);
	
	vector< vector<int> > useful(1<<d);
	{
		vector<int> cnt(d, 3);
		
		for(int i=1; i<(1<<d); ++i) if(i != lowbit(i))
		{
			for(int mask=all-1; mask>=0; --mask)
			{
				bool flag = 1;
				for(int j=0; j<d; ++j)
				{
					int x = (mask >> (3 * j)) & 7;
					flag &= x + bdig(i, j) <= cnt[j];
				}
				if(flag) useful[i].emplace_back(mask);
			}
			
			for(int j=0; j<d; ++j)
				cnt[j] = min(7, 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) if(i != lowbit(i))
			for(int mask: useful[i])
				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)
		{
			int nxt = 0;
			ll cur = f[mask];
			for(int i=0; i<d; ++i)
			{
				int x = (mask >> (3 * i)) & 7;
				
				if((x & 1) != ((a[i] >> k) & 1))
					++x, cur += c[1 << i] << k;
				
				nxt |= (x >> 1) << (3 * i);
			}
			nf[nxt] = max(nf[nxt], cur);
		}
		memcpy(f, nf, sizeof(f));
	}
	
	printf("%lld\n",f[0]);
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 3764kb

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
Wrong Answer
time: 165ms
memory: 3764kb

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:

wrong answer 41st numbers differ - expected: '11903927979855337', found: '11903927979237277'