5.2刷题

P1064 [NOIP 2006 提高组] 金明的预算方案

背包+附属品DP

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n, m, v, p, q;
struct node{
	int id, v, s, f;
}a[100];
int b[32010], dp[32010];
bool cmp(node a, node b){
	if(a.id == b.id)return a.f < b.f;
	return a.id < b.id;
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		cin >> v >> p >> q;
		if(q == 0)a[i].id = i, a[i].v = v, a[i].f = 0, a[i].s = v * p;
		else a[i].id = q, a[i].v = v, a[i].f = ++b[q], a[i].s = v * p;
	}
	sort(a + 1, a + m + 1, cmp);
	
	for(int i = 1; i <= m; i++){
		if(a[i].f)continue;
		for(int j = n; j >= a[i].v; j--){
			dp[j] = max(dp[j], dp[j - a[i].v] + a[i].s);
			if(a[i].id == a[i + 1].id && j >= a[i].v + a[i + 1].v){
				dp[j] = max(dp[j], dp[j - a[i].v - a[i + 1].v] + a[i].s + a[i + 1].s);
			}
			if(a[i].id == a[i + 2].id && j >= a[i].v + a[i + 2].v){
				dp[j] = max(dp[j], dp[j - a[i].v - a[i + 2].v] + a[i].s + a[i + 2].s);
			}
			if(a[i].id == a[i + 2].id && j >= a[i].v + a[i + 1].v + a[i + 2].v){
				dp[j] = max(dp[j], dp[j - a[i].v - a[i + 1].v - a[i + 2].v] + a[i].s + a[i + 1].s + a[i + 2].s);
			}
		}
	}
	cout << dp[n] << endl;
	
	
	return 0;
}

P1156 垃圾陷阱

做法一:DFS

#include<bits/stdc++.h>
using namespace std;
#define int long long
int d, n;
struct node{
	int t, f, h;
}a[105];
bool cmp(node a, node b){
	return a.t < b.t;
}
int ans1 = 1e9, ans2;
void dfs(int t, int h, int pos){
	if(h >= d){
		ans1 = min(ans1, a[pos - 1].t);
		return;
	}
	if(a[pos].t > t || pos > n){
		ans2 = max(ans2, t);
		return;
	}
	dfs(t, h + a[pos].h, pos + 1);
	dfs(t + a[pos].f, h, pos + 1);
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> d >> n;
	for(int i = 1; i <= n; i++)cin >> a[i].t >> a[i].f >> a[i].h;
	sort(a + 1, a + n + 1, cmp);
	dfs(10, 0, 1);
	if(ans1 == 1e9)cout << ans2 << endl;
	else cout << ans1 << endl;
	
	return 0;
}

记忆化改进

#include<bits/stdc++.h>
using namespace std;
#define int long long
int d, n;
struct node{
	int t, f, h;
}a[105];
bool cmp(node a, node b){
	return a.t < b.t;
}
int ans1 = 1e9, ans2;
bool dp[1010][110][110];
void dfs(int t, int h, int pos){
	if(h >= d){
		ans1 = min(ans1, a[pos - 1].t);
		return;
	}
	if(a[pos].t > t || pos > n){
		ans2 = max(ans2, t);
		return;
	}
	if(dp[t][h][pos])return;
	dp[t][h][pos] = 1;
	dfs(t, h + a[pos].h, pos + 1);
	dfs(t + a[pos].f, h, pos + 1);
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> d >> n;
	for(int i = 1; i <= n; i++)cin >> a[i].t >> a[i].f >> a[i].h;
	sort(a + 1, a + n + 1, cmp);
	dfs(10, 0, 1);
	if(ans1 == 1e9)cout << ans2 << endl;
	else cout << ans1 << endl;
	
	return 0;
}

mp改进

#include<bits/stdc++.h>
using namespace std;
#define int long long
int d, n;
struct node{
	int t, f, h;
}a[105];
bool cmp(node a, node b){
	return a.t < b.t;
}
int ans1 = 1e9, ans2;
map<int, map<int, map<int, bool> > >dp;
void dfs(int t, int h, int pos){
	if(h >= d){
		ans1 = min(ans1, a[pos - 1].t);
		return;
	}
	if(a[pos].t > t || pos > n){
		ans2 = max(ans2, t);
		return;
	}
	if(dp[t][h][pos])return;
	dp[t][h][pos] = 1;
	dfs(t, h + a[pos].h, pos + 1);
	dfs(t + a[pos].f, h, pos + 1);
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> d >> n;
	for(int i = 1; i <= n; i++)cin >> a[i].t >> a[i].f >> a[i].h;
	sort(a + 1, a + n + 1, cmp);
	dfs(10, 0, 1);
	if(ans1 == 1e9)cout << ans2 << endl;
	else cout << ans1 << endl;
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值