40分求助!!!
查看原帖
40分求助!!!
538427
czy0323楼主2023/7/12 19:43
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 4005;
int n, T, point;
int a[N], Max[N];
int dis[N][2];
vector<int> g[N];

struct node{
	int to;
	bool typ;
	node(int _to, int _typ){
		to = _to, typ = _typ;
	}
};
queue<node> q;

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
	
    cin >> n >> T;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        point = max(point, a[i]);
    }
    point *= 2;
	sort(a + 1, a + 1 + n);
	
	for(int i = 0; i <= point; i++){
		for(int j = 1; j <= n; j++){
			if( a[j] > i )
				g[i].push_back(i + a[j]);
			else
				g[i].push_back(i - a[j]);
		}
	}
	q.push(node(0, 0));
	dis[0][0] = 1;
	while( !q.empty() ){
		node h = q.front();
		q.pop();
		for(auto i : g[h.to]){
			if( dis[i][h.typ ^ 1] )
				continue;
			dis[i][h.typ ^ 1] = dis[h.to][h.typ] + 1;
			q.push(node(i, h.typ ^ 1));
		}
	}
	for(int i = 1; i <= point; i++){
		if( dis[i][0] )
			Max[dis[i][0] - 1] = max(Max[dis[i][0] - 1], i);
		if( dis[i][1] )
			Max[dis[i][1] - 1] = max(Max[dis[i][1] - 1], i);
	}
	for(int i = 3; i <= 4000; i++)
		Max[i] = max(Max[i], Max[i - 2]);
	while( T-- ){
		int m;
		cin >> m;
		if( m <= 4000 )
			cout << Max[m] << "\n";
		else if( m & 1 )
			cout << Max[3999] << "\n";
		else
			cout << Max[4000] << "\n";
	}
    return 0;
}
2023/7/12 19:43
加载中...