Hangzhou Electric Multi-School——Second Session (Problem Solution)

It’s too difficult to write, because it really doesn’t. . .

1010 Lead of Wisdom

Now 6772Lead of Wisdom can be found in the question list

Topic

There are t test cases.
Each case will first have n, k, n represents that the next n rows will have n weapons, each weapon is of a different type, and each has four attributes: a, b, c, and d. k represents that there are at most k types of weapons that can appear.
You can only take one of each weapon, which means you can take up to k weapons when each weapon is available. If there are no types of weapons, you can’t take this, so the number of weapons must be <= k

The required value isInsert picture description here

analysis

The solution to this problem is very violent. It is to find the value of each situation and compare them to find the maximum value. Don’t worry about timeout, because the time complexity in this case is n to the k/n power, the worst Time is
3 to the power of 16 (50/3), which is not too big.

I don't understand why this sentence degenerates so much. I will try to understand it after I look at the search tree. . .
Insert picture description here
Tools needed:
cnt[k]: the number of weapons of each type
next[k]: the next weapon type of each weapon, mainly for weapons with cnt[i] = 0, stored as its next weapon A non-zero weapon type, it is convenient to directly dfs
a structure to store each weapon and its four attribute values. The first subscript represents the weapon attribute, and the second subscript represents the number of the weapon (<cnt [i])
struct war { int a, b, c, d; }item[55][55];

Code (AC)

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<vector>
#include<stack>
using namespace std;
const int MAX_N = 55;

int n, k;
long long ans;
int cnt[MAX_N];
int nxt[MAX_N];

struct war {
    
    
    int a, b, c, d;
}item[MAX_N][MAX_N];

void solve(int x,int a,int b,int c,int d) {
    
    
    if(x > k) {
    
    
    	//终止条件,到了最后一个属性,计算并比较更新ans 
    	long long temp = (long long)a * b * c * d;
    	if(temp > ans) ans = temp;
    	return;
	}
	
	if(!cnt[x]) {
    
    
		solve(nxt[x],a,b,c,d);
		return;
	}
	for(int i = 1; i <= cnt[x] ; i ++) {
    
    
		solve(x + 1 , a + item[x][i].a , b + item[x][i].b , c + item[x][i].c ,  d + item[x][i].d );
	}
}

int main() {
    
    
	ios::sync_with_stdio(false);
	cin.tie(0);
    int T;
    cin >> T;
    while (T--) {
    
    
    	ans = 0;//每次重新计算清零答案 
    	memset(cnt,0,sizeof(cnt));
        cin >> n >> k;
        int x;
        while(n--) {
    
    
        	cin >> x;
        	cnt[x]++;
        	cin>>item[x][cnt[x]].a>>item[x][cnt[x]].b>>item[x][cnt[x]].c>>item[x][cnt[x]].d;
		}
		//处理next数组
		x = k + 1;
		for(int i = k; i ; i--) {
    
    
			nxt[i] = x;
			if(cnt[i])
				x = i;
		}
        solve(1,100,100,100,100);
        cout<<ans<<endl;
    }
    return 0;
}

There is a very inexplicable point. After putting these header files, the next array will report a compilation error. Change it to nxt and it will be ac. It seems that there is a next method of the same name in a stl iterator header file, so where is it? I don't know one.

1001 Total Eclipse

Original title link

Guess you like

Origin blog.csdn.net/weixin_45203704/article/details/107546085