当前位置: 首页 > news >正文

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;
}

相关文章:

  • shell(6)
  • btrace1.0使用方法
  • 超预期!淘宝闪购提前开放全国全量,联合饿了么扭转外卖战局
  • ARConv的复现流程
  • 算法笔记.分解质因数
  • 值此五一劳动节来临之际,
  • 【Java IO流】字符输入流FileReader、字符输出流FileWriter
  • CMake separate_arguments用法详解
  • 全国信息素养大赛 图形化挑战赛~复赛练习-在正方形内吗?
  • 软件第三方测试:关键部分、意义、流程及方法全解析?
  • 数字智慧方案5876丨智慧交通枢纽智能化系统建设方案(56页PPT)(文末有下载方式)
  • 对解微分方程分离变量法本质的思考
  • 进程与线程:05 内核级线程实现
  • 综合案例:使用vuex对购物车的商品数量和价格等公共数据进行状态管理
  • GNOME扩展:ArcMenu的Brisk布局左右调换
  • C语言与指针3——基本数据类型
  • 大语言模型能力评定探讨
  • AI实现制作logo的网站添加可选颜色模板
  • 【OFDM过程中正交子载波特性的应用及全面解析】
  • FPGA:介绍几款高速ADC及其接口形式
  • 首都航空:太原至三亚航班巡航阶段出现机械故障,已备降南宁机场
  • 魔都眼|买买买,老铺黄金新店开业被挤爆:有人排队5小时
  • 来论|受美国“保护”,日本民众要付出什么代价?
  • 国家医保局副局长颜清辉调任人社部副部长
  • 过去24小时中美是否就关税问题进行过接触?外交部:没有
  • 建设银行南昌分行引金融“活水”,精准灌溉乡村沃土