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

在一个游戏中,tokitsukaze需要在n个士兵中选出一些士兵组成一个团去打副本。 第i个士兵的战力为v[i],团的战力是团内所有士兵的战力之和。 但是这些士兵有特殊的要求:如果选了第i个士兵,这个

链接:https://ac.nowcoder.com/acm/contest/1080/C
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
在一个游戏中,tokitsukaze需要在n个士兵中选出一些士兵组成一个团去打副本。
第i个士兵的战力为v[i],团的战力是团内所有士兵的战力之和。
但是这些士兵有特殊的要求:如果选了第i个士兵,这个士兵希望团的人数不超过s[i]。(如果不选第i个士兵,就没有这个限制。)
tokitsukaze想知道,团的战力最大为多少。
输入描述:

第一行包含一个正整数n(1≤n≤10^5)。
接下来n行,每行包括2个正整数v,s(1≤v≤10^9,1≤s≤n)。

输出描述:

输出一个正整数,表示团的最大战力。

示例1
输入

2
1 2
2 2

输出

3

示例2
输入

3
1 3
2 3
100 1

输出

100

#include<iostream> #include<set> #include<algorithm> using namespace std; struct node { int v, s; }a[100008]; bool comp(node a,node b) { return a.s > b.s; } int main() { multiset<int> S; int n; long long ans = 0, sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].v >>a[i].s; } sort(a, a + n, comp); for (int i = 0; i < n; i++) { S.insert(a[i].v); sum += a[i].v; while (S.size() > a[i].s) { sum -= *S.begin(); S.erase(S.begin()); } ans = max(ans, sum); } cout << ans; return 0; }
#include<iostream> #include<algorithm> #include<queue> using namespace std; struct node { int x, y; }a[100008]; bool comp(node u, node v) { return u.y > v.y; } int main() { priority_queue<int,vector<int>,greater<int> > S; int n; long long ans = 0, sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } sort(a, a + n, comp); for (int i = 0; i < n; i++) { S.push(a[i].x); sum += a[i].x; while (S.size() > a[i].y) { sum -= S.top(); S.pop(); } ans = max(ans, sum); } cout << ans; return 0; }
#include<iostream> #include<algorithm> #include<queue> using namespace std; struct node { int x, y; bool operator<(const node &v)const { return x>v.x; } }a[100008]; bool comp(node u, node v) { return u.y > v.y; } int main() { priority_queue<node> S; int n; long long ans = 0, sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } sort(a, a + n, comp); for (int i = 0; i < n; i++) { S.push(a[i]); sum += a[i].x; while (S.size() > a[i].y) { sum -= S.top().x; S.pop(); } ans = max(ans, sum); } cout << ans; return 0; }
#include<iostream> #include<algorithm> #include<queue> using namespace std; struct node { int x, y; }a[100008]; bool comp(node u, node v) { return u.y > v.y; } struct cmp1 { bool operator()(const node& u, const node& v)const { return u.x > v.x; } }; int main() { priority_queue<node,vector<node>,cmp1> S; int n; long long ans = 0, sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } sort(a, a + n, comp); for (int i = 0; i < n; i++) { S.push(a[i]); sum += a[i].x; while (S.size() > a[i].y) { sum -= S.top().x; S.pop(); } ans = max(ans, sum); } cout << ans; return 0; }
  • 第一个程序用multiset容器,默认从小到大排序。
  • 第二个程序用priority_queue,其默认为大根堆,这里通过priority_queue<int,vector,greater > S改为小根堆。默认的大根堆参数为priority_queue<int,vector,less > S.另外这里的数据类型是基本数据类型。
  • 第三个程序的数据类型是自定义的结构体,可以采用程序中的方法定义小根堆(重载<)。
  • 第四个程序是将定义小根堆的方法写在了结构体外面( 重载() )。
http://www.cnnetsun.cn/news/3711762.html

相关文章:

  • Elasticsearch安装与简单配置
  • BI项目上线90天验收清单:客户成功总监总结的7个里程碑
  • 如何在3分钟内搭建个人专属文件服务器:告别复杂配置的CHFSGUI图形化解决方案
  • Windows 11文件资源管理器性能优化:原理、验证与最佳实践
  • 无人机体系化竞争:从单机对抗到系统集成的技术壁垒与工业逻辑
  • 弹幕盒子:一站式在线弹幕处理工具完整指南
  • 工业物联网设备电源管理方案设计与优化
  • JAVA计算机毕设之基于 SpringBoot 的线上心理答疑互助与情绪疏导管理平台 高校心理健康资讯推送与社区交流系统(完整前后端代码+说明文档+LW,调试定制等)
  • 如何在Android中快速构建专业弹窗:BasePopup完整指南
  • 【Bug已解决】[RFC]: Remove Per-Block KV Transfer Error Handling 解决方案
  • RevokeMsgPatcher:Windows下微信QQ防撤回的智能解决方案
  • GoldHEN金手指管理器:5分钟掌握PS4游戏修改终极指南
  • 获取dom元素操作整理
  • MouseClick技术深度解析:现代C++跨平台自动化解决方案
  • TI TPIC7710EVM评估板深度解析:EPB系统ASIC硬件验证与GUI软件实操指南
  • Java学习手册:(数据结构与算法-数组)Number of Islands(leetcode200)
  • 学生党降重工具怎么挑?按预算和效果排一排
  • 羽毛球馆场地管理系统的设计与实现
  • C 排序算法:快速排序
  • 无主题内容创作方法论:逆向思维与关键词发散技术
  • 集成学习(ensemble learning)
  • 企业400电话办理全流程指南与优化建议
  • 爬虫转大模型,我的旧方法先崩了:权限日志才是真门槛
  • C++实现大小写不敏感字符串集合:自定义比较器与安全字符处理
  • C++ RESTful API 与 Nginx 集成:构建高性能后端服务的完整实践
  • Gitee代码托管平台核心技术解析与企业实践
  • Taskbar-Lyrics:Windows 11任务栏歌词显示插件完整指南
  • NBM5100A与PIC18F86J50在物联网设备中的低功耗设计
  • 智能 PING 与多接口测试,GN-Q10A 应对能源网络痛点
  • Zotero-GPT终极指南:如何用AI插件快速提升文献管理效率