70分求助
查看原帖
70分求助
689146
mushezi楼主2023/3/15 22:40

谢谢

#include <bits/stdc++.h>
using namespace std;

int n, b;
int v[25], maxv;
int farm[105], temp;
int dp[100005];
int ans;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> b;
	for(int i = 1; i <= b; i++){
		cin >> v[i];
		maxv = max(maxv, v[i]);
	}
	for(int i = 1; i <= n; i++) cin >> farm[i];
	
	for(int i = 1; i <= n; i++){
		temp = farm[i] - farm[i - 1];
		if(farm[i - 1] > 0) temp++;
		memset(dp, 0x3f, sizeof(dp));
		dp[0] = 0;
		for(int i = 1; i <= b; i++) dp[v[i]] = 1;
		for(int i = maxv + 1; i <= temp; i++){
			for(int j = 1; j <= b; j++){
				dp[i] = min(dp[i], dp[i - v[j]] + 1);
			}
		}
		if(dp[temp] < 0){
			cout << -1;
			return 0;
		}
		ans += dp[temp];
	}
	
	if(ans > 1e7) cout << -1;
	else cout << ans ;
	
	return 0;
}
2023/3/15 22:40
加载中...