80pts,求助!
查看原帖
80pts,求助!
773805
wsm52楼主2024/11/1 20:25
#include<bits/stdc++.h>
using namespace std;
int t,n,s[20],c[5],ans;
int num(){
	int tot=0;
	memset(c,0,sizeof c);
	for(int i=0;i<=13;i++) c[s[i]]++;
	while(c[4]&&c[2]>=2){
		c[4]--;c[2]-=2;tot++;
	}
	while(c[4]&&c[1]>=2){
		c[4]--;c[1]-=2;tot++;
	}
	while(c[4]&&c[2]>=1){
		c[4]--;c[2]--;tot++;
	}
	while(c[3]&&c[2]){
		c[3]--;c[2]--;tot++;
	}
	while(c[3]&&c[1]){
		c[3]--;c[1]--;tot++;
	}
	return tot+c[1]+c[2]+c[3]+c[4];
}
void dfs(int x){
	if(x>=ans) return;
	ans=min(ans,x+num());
	for(int i=3;i;i--){
		for(int j=2;j<=13;j++){
			int p=j;
			while(s[p]>=i&&p<=13){
				p++;
				if((i==3&&p-j>=2)||(i==2&&p-j>=3)||(i==1&&p-j>=5)){
					for(int k=j;k<p;k++) s[k]-=i;
					dfs(x+1);
					for(int k=j;k<p;k++) s[k]+=i;
				}
			}
		}
	} 
}
int main(){
	cin>>t>>n;
	while(t--){
		memset(s,0,sizeof s);
		int a,b;
		for(int i=1;i<=n;i++){
			cin>>a>>b;
			if(a==1) a=13;
			else if(a>0) a--;
			s[a]++;
		}
		ans=n;
		dfs(0);
		cout<<ans<<endl;
	}
	return 0;
}
2024/11/1 20:25
加载中...