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

技能升级--二分例题

1.题目

2. 暴力解法

直接遍历每一种情况,选择攻击力最高的解法

#include<iostream>
using namespace std;
int a[50010], b[50010];
int main()
{long long ans = 0;long long m;int n;cin >> n >> m;for (int i = 0; i < n; i++){cin >> a[i] >> b[i];}int i;//因为for循环外面的while里面还要用到while (m--){int max1 = 0;int maxsite=-1;for (i = 0; i < n; i++){if (a[i] > max1){max1 = a[i];maxsite = i;}}ans += max1;;a[maxsite] -= b[maxsite];}cout << ans << endl;return 0;
}

 

3.暴力法+优先队列

1.前情提要(优先队列)

优先队列是一种使用二叉堆优化结构的队列,分为最大值优先队列和最小值优先队列

最大值优先队列:

std::priority_queue<int> q;

最小值优先队列:

 priority_queue<int, vector<int>, greater<int>> minHeap;

 他的时间复杂度只有O(log2n)

2.思路分析

通过使用对组改变优先队列的数据结构实现

3.代码呈现

#include<iostream>
#include<queue>
using namespace std;
typedef pair<int, int> PII;
priority_queue<PII>q;//默认是大根堆,即最大值优先队列
PII p;
int main()
{long long m;int n;cin >> n >> m;for (int i = 0; i < n; i++){long long a, b;cin >> a >> b;q.push(PII(a, b));}long long ans = 0;while (m--){if (q.empty()){break;}p = q.top();q.pop();ans += p.first;p.first -= p.second;if (p.first > 0){q.push(p);}}cout << ans << endl;return 0;
}

3.二分优化

1.思路分析

这道题用二分做起来思辨性比较强,首先用作二分的数值便是一个难点,这里选用的是最后一次增加的最大值

2.代码呈现

#include<iostream>
#include<queue>
using namespace std;
long long m;
int n;
int a[100100], b[100100];
bool check(int mid)
{long long cnt = 0;for (int i = 0; i < n; i++){if (a[i] < mid){continue;}
//这里加1是因为首次增加攻击力的时候cnt += (a[i] - mid) / b[i] + 1;if (cnt >= m){return true;}}return false;
}
int main()
{cin >> n >> m;for (int i = 0; i < n; i++){cin >> a[i] >> b[i];}//二分int L = 1, R = 1000000;long long mid = 0;while (L <= R){//二分枚举最后一次能增加多少mid = (L + R) / 2;if (check(mid)){L = mid + 1;}else{R = mid - 1;}}//注意二分结束后得到的结果是R而不是midlong long attack = 0;long long cnt = m;for (int i = 0; i < n; i++){if (a[i] < R)continue;long long t = (a[i] - R) / b[i] + 1;attack += (a[i] * 2 - (t - 1) * b[i]) * t / 2;cnt -= t;}//最后的结果得出的t的总和一定是大于等于m的if(cnt<=0)cout << attack+cnt*R<< endl;else{cout << "-1" << endl;}return 0;
}

3.注意

之所以最后的次数一定是大于等于m的(二分底层原理)

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

相关文章:

  • 新手向:Python自动化办公批量重命名与整理文件系统
  • ECUs、ZCUs、CCUs:产生的软件栈(SW stack)也有所不同
  • 事物生效,订单类内部更新订单
  • MFC/C++语言怎么比较CString类型最后一个字符
  • gitignore添加后如何生效?
  • Spark 单机模式安装与测试全攻略​
  • 考完数通,能转云计算/安全方向吗?转型路径与拓展路线分析
  • ThreadLocal结构
  • windows11系统安装nginx1.28.0
  • 【无标题】11维模型几何引擎拓扑量子计算机的推想
  • 【C++篇】:告别手动内存管理!——C++智能指针的快速上手指南
  • 宝塔面板常见问题
  • 驱动开发系列60- Vulkan 驱动实现-SPIRV到HW指令的实现过程(1)
  • 开疆智能EtherCAT转CANopen网关连接磁导航传感器配置案例
  • 空间智能-李飞飞团队工作总结(至2025.07)
  • spark广播表大小超过Spark默认的8GB限制
  • redis面试高频问题汇总(一)
  • wpf 实现窗口点击关闭按钮时 ​​隐藏​​ 而不是真正关闭,并且只有当 ​​父窗口关闭时才真正退出​​ 、父子窗口顺序控制与资源安全释放​
  • NAT原理与实验指南:网络地址转换技术解析与实践
  • ubuntu之坑(十五)——设备树
  • 【论文阅读】Thinkless: LLM Learns When to Think
  • .net天擎分钟降水数据统计
  • 【飞牛云fnOS】告别数据孤岛:飞牛云fnOS私人资料管家
  • React 第六十九节 Router中renderMatches的使用详解及注意事项
  • JMeter 连接与配置 ClickHouse 数据库
  • Mysql用户管理及在windows下安装Mysql5.7(压缩包方式)远程连接云服务器(linux)上的Mysql数据库
  • 【一维 前缀和+差分】
  • ether.js—6—contractFactory以部署ERC20代币标准为例子
  • CSS手写题
  • 详解彩信 SMIL规范