跳到正文

题解

题解:AT_donuts_2015_2 Tokyo 7th シスターズ

朱梓煊ZZX约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年10月1日
收录
2026年10月19日
更新
2026年11月17日
篇幅
约 3 分钟
获赞
3

版本

  1. v1当前

前言

题面中输入格式 KateX 炸了,是下面这样的。

NMA1A2…ANB1C1I1,1I1,2…I1,C1B2C2I2,1I2,2…I2,C2:BMCMIM,1IM,2…IM,CM\begin{array}{llllllll} N & M & & & & & \\ A_{1} & A_{2} & \ldots & A_{N} & & \\ B_{1} & C_{1} & I_{1,1} & I_{1,2} & \ldots & I_{1, C_{1}} \\ B_{2} & C_{2} & I_{2,1} & I_{2,2} & \ldots & I_{2, C_{2}} \\ : & & & & & & \\ B_{M} & C_{M} & I_{M, 1} & I_{M, 2} & \ldots & I_{M, C_{M}} \end{array}

题意简化

给定 NN 个元素,每个元素若选择可获得 AiA_i。有 MM 个组合,若选择了 ≥3\geq 3 个第 ii 组合中的元素,则可以获得 BiB_i。求选择 99 个元素,获得价值的最大值。

思路

显然是有回溯的深搜,对于每个元素枚举选或不选的情况,计算每种情况的能力值。

复杂度 O(2N⋅NM)O(2^N \cdot NM)

code

C++
#include<bits/stdc++.h>
using namespace std;
int N,M,A[20],B[51],C[51];
int I[51][20];
int maxn;
bool f[20];
void dfs(int d,int cnt){
	if(N-d+1+cnt<9) return;//剪枝:以后偶像全选仍达不到9个 
	if(cnt==9){//计算当前方案能力值 
		int ans=0;
		for(int i=1;i<=N;i++){
			if(f[i]) ans+=A[i];
		}
		for(int i=1;i<=M;i++){
			int ok=0;
			for(int j=1;j<=C[i];j++){
				if(f[I[i][j]]) ok++;
			}if(ok>=3) ans+=B[i];
		}
		maxn=max(maxn,ans);
		return;
	}
	dfs(d+1,cnt);//不取该偶像 
	f[d]=true;
	dfs(d+1,cnt+1);//取该偶像 
	f[d]=false;//回溯 
}
int main(){
	cin>>N>>M;
	for(int i=1;i<=N;i++) cin>>A[i];
	for(int i=1;i<=M;i++){
		cin>>B[i]>>C[i];
		for(int j=1;j<=C[i];j++){
			cin>>I[i][j];
		}
	}
	memset(f,false,sizeof(f));
	dfs(1,0);
	cout<<maxn<<endl;
	return 0;
}
原文