QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#216460#1773. Breaking BarsPhantomThresholdTL 594ms239268kbC++203.1kb2023-10-15 18:40:272023-10-15 18:40:27

Judging History

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

  • [2023-10-15 18:40:27]
  • 评测
  • 测评结果:TL
  • 用时:594ms
  • 内存:239268kb
  • [2023-10-15 18:40:27]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int INF=1e9;
const int inf=1e9;

map<pair<int,int>,int> dict;
int mxid=0;
int id[10][10];
int sz[25];
pair<int,int> rec[25];
int w[(1<<21)+50];

int n,W;
int x[55],y[55];
int sum[55];
vector<pair<int,int>> v[25];
int dp[25][(1<<21)+50];

int dfs(int dep,int mask){
	cerr << "dfs:" << dep << " " << mask << endl;
	if (sum[dep-1]-w[mask]>=W) return 0;
	if (dep==n+1) return INF;
	
	auto PAIR=make_pair(dep,mask);
	if (dict.count(PAIR)!=0) return dict[PAIR];
	int ans=INF;
	
	{
		int nowid=id[x[dep]][y[dep]];
		for (auto [m1,s1]:v[nowid]) ans=min(ans,dfs(dep+1,mask^m1)+s1); 
	}
	
	return dict[PAIR]=ans;
}

int f[1<<21][2];
int g[2][1<<22],gc[1<<22],gn[2];

int p[55];
inline bool cmp(const int k1,const int k2){ return id[x[k1]][y[k1]] < id[x[k2]][y[k2]]; }

int main(){
	ios_base::sync_with_stdio(false);
	
	mxid=0;
	for (int i=1;i<=6;i++){
		for (int j=i;j<=6;j++){
			id[i][j]=id[j][i]=mxid;
			sz[mxid]=i*j;
			rec[mxid]=make_pair(i,j);
			++mxid;	
		}
	}
	for (int mask=0;mask<(1<<mxid);mask++){
		for (int j=0;j<mxid;j++){
			if ((mask>>j)&1) w[mask]+=sz[j];
		}
	}
	
	for (int i=1;i<=6;i++){
		for (int j=i;j<=6;j++){
		//	cerr << i << " " << j << endl;
			for (int mask=0;mask<(1<<mxid);mask++) dp[id[i][j]][mask]=INF;
			dp[id[i][j]][1<<id[i][j]]=0;
			
			for (int k=1;k<=i-k;k++){
				int id1=id[k][j];
				int id2=id[i-k][j];
				for (auto [m1,s1]:v[id1]){
					for (auto [m2,s2]:v[id2]){
						dp[id[i][j]][m1^m2]=min(dp[id[i][j]][m1^m2],s1+s2+1);
					}
				}
			}
			for (int l=1;l<=j-l;l++){
				int id1=id[i][l];
				int id2=id[i][j-l];
				for (auto [m1,s1]:v[id1]){
					for (auto [m2,s2]:v[id2]){
						dp[id[i][j]][m1^m2]=min(dp[id[i][j]][m1^m2],s1+s2+1);
					}
				}	
			}
			for (int mask=0;mask<(1<<mxid);mask++) if (dp[id[i][j]][mask]!=INF){
			//	if (__builtin_popcount(mask)>6) continue;
				v[id[i][j]].emplace_back(mask,dp[id[i][j]][mask]);	
			}
		//	cerr << i << " " << j << endl;
		//	cerr << v[id[i][j]].size() << endl;
		}
	}
	
	cin >> n >> W;
	W<<=1;
	for (int i=1;i<=n;i++){
		char ch;
		cin >> x[i] >> ch >> y[i];	
	}
	for(int i=1;i<=n;i++) p[i]=i;
	sort(p+1,p+n+1,cmp);
	for (int i=1;i<=n;i++) sum[i]=sum[i-1]+x[p[i]]*y[p[i]];
	
	
	int ans=9;
	memset(g,0,sizeof g);
	memset(f,-1,sizeof f);
	int now=0;
	g[now][++gn[now]]=0; f[0][0]=0,f[0][1]=0;
	for(int i=1;i<=n+1;i++)
	{
		for(int j=1;j<=gn[now];j++)
		{
			int s=g[now][j];
			gc[j]=f[s][1];
			if(f[s][1]<ans)
			{
				int temp=sum[i-1];
				for(int k=1;k<=mxid;k++) if(s>>(k-1)&1)
					temp-=sz[k];
				
				if(temp>=W) 
				{
					ans=min(ans,f[s][1]);
				}
			}
		}
		
		if(i==n+1) break;
		now=!now; gn[now]=0;
		for(int j=1;j<=gn[!now];j++)
		{
			int s=g[!now][j];
			int c=gc[j];
			
			for(auto [si,ci]:v[id[x[p[i]]][y[p[i]]]])
			{
				int ns=s^si;
				if(f[ns][0]!=i) f[ns][0]=i,f[ns][1]=inf;
				
				if(f[ns][1]>c+ci)
				{
					if(f[ns][1]>=ans && c+ci<ans)
					{
						g[now][++gn[now]]=ns;
					}
					f[ns][1]=c+ci;
				}
			}
		}
	}
	
	cout << ans << "\n";
	
	return 0;	
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 594ms
memory: 239268kb

input:

16 118
5x6 3x5 4x5 6x3 6x1 1x1 4x5 4x5 2x3 1x2 5x3 5x3 6x2 3x6 5x6 4x2

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 142ms
memory: 235872kb

input:

6 30
2x3 3x3 1x5 2x5 3x5 3x5

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 143ms
memory: 236216kb

input:

3 2
1x1 1x1 1x2

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 187ms
memory: 236748kb

input:

4 25
2x3 3x3 2x5 5x5

output:

2

result:

ok single line: '2'

Test #5:

score: 0
Accepted
time: 182ms
memory: 237060kb

input:

5 10
1x1 1x1 1x1 1x1 4x4

output:

1

result:

ok single line: '1'

Test #6:

score: 0
Accepted
time: 171ms
memory: 237032kb

input:

6 34
1x1 1x2 2x3 3x3 5x5 5x5

output:

2

result:

ok single line: '2'

Test #7:

score: 0
Accepted
time: 149ms
memory: 236092kb

input:

15 70
1x1 1x2 1x3 1x4 1x5 2x2 2x3 2x4 2x5 3x3 3x4 3x5 4x4 4x5 5x5

output:

7

result:

ok single line: '7'

Test #8:

score: 0
Accepted
time: 149ms
memory: 237036kb

input:

11 40
1x1 1x2 1x3 2x3 2x4 4x5 2x2 2x2 1x4 2x4 4x5

output:

3

result:

ok single line: '3'

Test #9:

score: 0
Accepted
time: 150ms
memory: 235492kb

input:

20 74
4x3 5x1 4x2 3x3 1x2 2x1 4x2 1x5 1x1 1x4 1x1 5x5 1x4 4x5 3x2 3x5 1x2 3x4 1x1 2x3

output:

6

result:

ok single line: '6'

Test #10:

score: 0
Accepted
time: 197ms
memory: 236724kb

input:

20 104
4x2 2x3 4x5 5x1 1x3 4x3 1x2 1x1 5x2 5x4 5x5 4x1 5x5 3x5 4x2 3x1 3x1 5x4 2x1 4x4

output:

6

result:

ok single line: '6'

Test #11:

score: 0
Accepted
time: 167ms
memory: 236444kb

input:

13 44
5x4 3x2 3x2 4x1 4x4 1x2 1x5 1x2 1x1 3x5 1x2 1x3 3x2

output:

5

result:

ok single line: '5'

Test #12:

score: 0
Accepted
time: 155ms
memory: 235756kb

input:

5 21
1x3 1x2 5x4 4x4 1x1

output:

3

result:

ok single line: '3'

Test #13:

score: 0
Accepted
time: 155ms
memory: 236988kb

input:

18 77
5x4 4x2 5x5 1x4 3x1 4x3 2x3 1x1 3x4 5x2 5x3 2x2 2x1 2x1 1x2 5x3 3x3 1x4

output:

6

result:

ok single line: '6'

Test #14:

score: 0
Accepted
time: 175ms
memory: 236984kb

input:

9 30
5x2 5x3 1x4 1x4 2x3 1x2 3x3 2x3 4x1

output:

5

result:

ok single line: '5'

Test #15:

score: 0
Accepted
time: 188ms
memory: 236920kb

input:

8 37
2x4 1x3 5x4 5x5 2x4 1x4 1x2 1x4

output:

2

result:

ok single line: '2'

Test #16:

score: 0
Accepted
time: 173ms
memory: 239204kb

input:

19 103
1x5 5x2 2x2 5x4 1x5 1x1 5x5 2x2 2x5 5x4 3x4 3x2 4x4 5x4 5x3 2x2 2x4 4x3 3x3

output:

7

result:

ok single line: '7'

Test #17:

score: 0
Accepted
time: 171ms
memory: 235716kb

input:

19 75
2x1 1x1 5x5 2x4 1x3 2x3 2x2 2x3 4x5 4x3 3x1 4x1 4x2 4x4 5x1 1x4 1x5 5x3 3x1

output:

7

result:

ok single line: '7'

Test #18:

score: 0
Accepted
time: 187ms
memory: 236528kb

input:

20 81
2x3 2x5 5x3 2x1 3x1 5x2 4x5 2x1 1x5 5x2 2x5 1x5 3x2 1x5 1x2 4x2 4x2 5x4 3x2 3x3

output:

2

result:

ok single line: '2'

Test #19:

score: -100
Time Limit Exceeded

input:

47 297
3x5 3x2 1x5 5x6 5x5 5x5 4x2 5x4 4x1 6x2 6x6 5x3 1x2 2x6 6x2 3x3 2x2 2x2 1x4 2x5 5x3 4x4 6x3 3x6 5x4 3x6 3x1 6x1 3x1 1x2 3x4 1x6 6x6 5x3 1x1 5x5 2x1 1x4 5x1 5x6 2x1 4x6 2x2 6x6 2x3 6x1 3x1

output:


result: