当前位置: 首页 > 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;
}

http://www.xdnf.cn/news/253387.html

相关文章:

  • libevent库详解:高性能异步IO的利器
  • python 常用web开发框架及使用示例
  • Python 在世界地图上加气泡图
  • 【多线程】六、基于阻塞队列的生产者消费者模型
  • react js 查看字体效果
  • MySQL 中的游标(Cursor)
  • NV162NV172美光固态颗粒NV175NV188
  • SpringBoot癌症患者交流平台设计开发
  • Flutter AppBar 详解
  • gRPC学习笔记记录以及整合gin开发
  • 【云备份】配置文件加载模块
  • 贝叶斯算法(Bayesian Algorithms)详解
  • DBeaver连接人大金仓数据库V9
  • Nginx搭建test服务器
  • 企业级分布式 MCP 方案
  • 文章六:《循环神经网络(RNN)与自然语言处理》
  • 第十六届蓝桥杯 2025 C/C++组 客流量上限
  • 2025五一数学建模竞赛A题完整分析论文(共45页)(含模型、可运行代码、数据)
  • 【服务器通信-socket】——int socket(int domain, int type, int protocol);
  • LangChain入门(五)AI记住聊天历史
  • Android基础控件用法介绍
  • 报文三次握手对么٩(๑^o^๑)۶
  • 开源ERP系统对比:Dolibarr、ERPNext与Odoo
  • 【每日八股】复习 Redis Day5:集群(上)
  • 云原生后端:构建高效、可扩展的现代后端架构
  • HBM的哪些事
  • 【凑修电脑的小记录】vscode打不开
  • React useCallback函数
  • 从爬虫到网络---<基石3> gfw防火墙是怎么保护我们的?
  • Linux系统:进程程序替换以及相关exec接口