Lead of Wisdom(dfs)

Insert picture description hereInsert picture description here
This question was actually thought to be a multiple backpack dp at first, but later I thought about dfs, but the time complexity is 50^50. I thought it was a dp. This is misguided by the complexity of time. After the question, I realized that my previous analysis was wrong. Because there are n and k types, then the average number of each is n/k; then the time complexity of dfs is k (n/ k) to the power, then find the maximum value of this function.
Take a logarithm + derivation to find the maximum value. When k==e, it is the maximum value. At this time, the time complexity is about 1e8. Then the question was given for 8 seconds, which was enough. The main attention is how to make the types of discontinuities continuous, here can be solved with a book array;
AC code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll book[100];
struct Node{
    
    
	ll a,b,c,d;
	Node(ll x,ll y,ll z,ll k){
    
    
		a=x;b=y;c=z;d=k;
	}
};
vector<Node> V[100];
ll ans;
ll numm=1;
void dfs(ll s,ll a,ll b,ll c,ll d){
    
    
	if(s==numm){
    
    
		ll t=(100+a)*(100+b)*(100+c)*(100+d);
		ans=max(ans,t);
		return ; 
	}
	for(ll i=0;i<V[s].size();i++){
    
    
		dfs(s+1,a+V[s][i].a,b+V[s][i].b,c+V[s][i].c,d+V[s][i].d);
	}
}
int main(){
    
    
	ll T;
	scanf("%lld",&T);
	while(T--){
    
    
		ll n,k;
		for(ll i=0;i<=50;i++)V[i].clear();//清理 
		for(ll i=0;i<=50;i++)book[i]=0;
		scanf("%lld %lld",&n,&k);
		numm=1;
		ll t,a,b,c,d;
		for(ll i=1;i<=n;i++){
    
    
	    	scanf("%lld %lld %lld %lld %lld",&t,&a,&b,&c,&d);
		     if(!book[t])book[t]=numm++;//为了让种类连续以便dfs 
		     V[book[t]].push_back(Node(a,b,c,d));
	    }
	    dfs(1,0,0,0,0);
	    printf("%lld\n",ans);
	    ans=0;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44555205/article/details/107590291