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

5月18总结

一.算法题总结

1.

 解题思路:对于这个题,我最开始想到就是二分,但是头痛的是有三个解,如果我在-100到100之间二分,那么只能得出一个解,然后我就想了一下,这个要求精度,那么0.01这么小,好像可以在0-1之间或者-1-1之间二分,然后我就觉得好像可以遍历这俩百个格子,每个格子长度为一,进行二分求解

#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;double a, b, c, d;
double f(double x) {return a * x * x * x + b * x * x + c * x + d;
}int main() {cin >> a >> b >> c >> d;int ans = 0;for (int i = -100; i < 100; i++) {if (ans == 3)break;double l = i;double r = i + 1;double f1 = f(l);double f2 = f(r);if (f1 == 0) {ans++;printf("%.2f ", l);continue;}if (f1 * f2 < 0) {while (r - l >= 0.001) {double mid = (l + r) / 2;if (f(mid) * f(l) <= 0)r = mid;elsel = mid;}printf("%.2f ", l);ans++;}}return 0;
}

2.

解题思路:对于这个题,我写过Section I,所以就看出了要用贪心, 但在这个贪心要怎么用呢,也没给我个具体的数,让区间的和小于某个数,这个时候,我想起了二分,用二分来猜数的大小,而这个数的大小,就是看这个区间的是否大于它,这就可以贪心了

#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;bool isPossible(int mid, const vector<int>& nums, int m) {int count = 1;int current_sum = 0;for (int num : nums) {if (current_sum + num <= mid) {current_sum += num;}else {current_sum = num;count++;if (count > m) {return false;}}}return true;
}int main() {int n, m;cin >> n >> m;vector<int> nums(n);int max_num = 0;int total_sum = 0;for (int i = 0; i < n; ++i) {cin >> nums[i];max_num = max(max_num, nums[i]);total_sum += nums[i];}int left = max_num;int right = total_sum;int answer = total_sum;while (left <= right) {int mid = left + (right - left) / 2;if (isPossible(mid, nums, m)) {answer = mid;right = mid - 1;}else {left = mid + 1;}}cout << answer << endl;return 0;
}

3.

解题思路:今天写的最难的一个题,其实怪自己眼挫,没注意到(尽可能让前面的人少抄),其实这个题的本质也是贪心+二分,我就是这么写的,思路和上面的题一样,但是一直只能过4个,然后看了一下别人的题解,发现漏了个条件(尽可能让前面的人少抄),有了这个以后,那就反过来在对答案抄一边就行了

#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;int m, k;
struct lr {ll l, r;
};
vector<lr> len;bool check(ll x, vector<ll>& nums) {ll sum = 0;int cnt = 1;vector<lr> temp;lr current; current.l = 1;for (int i = 1; i <= m; i++) {if (nums[i] > x) return false;if (sum + nums[i] <= x) {sum += nums[i];} else {current.r = i - 1;temp.push_back(current);cnt++;sum = nums[i];current.l = i;}if (cnt > k) return false;}current.r = m;temp.push_back(current);if (cnt == k) {len = temp;return true;}return cnt<=k;
}void check2(ll x, vector<ll>& nums) {ll sum = 0;int cnt = 1;vector<lr> temp;lr current; current.r = m;for (int i = m; i >= 1; i--) {if (sum + nums[i] <= x) {sum += nums[i];} else {current.l = i + 1;temp.push_back(current);cnt++;sum = nums[i];current.r = i;}}current.l = 1;temp.push_back(current);reverse(temp.begin(), temp.end());len = temp;
}int main() {cin >> m >> k;vector<ll> nums(m + 1);ll left = 0, right = 0;for (int i = 1; i <= m; i++) {cin >> nums[i];right += nums[i];if (nums[i] > left) left = nums[i];}ll ans = right;while (left <= right) {ll mid = (left + right) / 2;if (check(mid, nums)) {ans = mid;right = mid - 1;} else {left = mid + 1;}}// 确保前面的人尽可能少抄写check2(ans, nums);for (int i = 0; i < k; i++) {cout << len[i].l << " " << len[i].r << endl;}return 0;
}

相关文章:

  • leetcode报错原因总结需要背下来的程序片 [更新中]
  • 三:操作系统线程管理之线程概念
  • 2025年全国青少年信息素养大赛C++小学全年级初赛试题
  • 逻辑与非逻辑的弥聚
  • 【Linux】第二十章 管理基本存储
  • 双紫擒龙紫紫红指标源码学习,2025升级版紫紫红指标公式-重点技术
  • 基于单片机路灯自动控制仪仿真设计
  • 创建型:工厂方法模式
  • TASK03【Datawhale 组队学习】搭建向量知识库
  • 10.9 LangChain LCEL革命:43%性能提升+声明式语法,AI开发效率飙升实战指南
  • STM32H562----------启动时钟分析
  • Listener method could not be invoked with the incoming message
  • Linux之基础IO
  • 非线性1无修
  • python + pip 独家秘籍
  • C++ map容器: 插入操作
  • 5.18 day24
  • 新电脑软件配置三 pycharm
  • 【应用开发十】pwm
  • ffmpeg -vf subtitles添加字幕绝对路径问题的解决方法
  • 天问二号探测器顺利转入发射区
  • 新时代,新方志:2025上海地方志论坛暨理论研讨会举办
  • 天算星座二期首批卫星成功发射,将助力6G空天信息基础设施建设
  • 张巍任中共河南省委副书记
  • 秦洪看盘|风格有所转变,热钱回流高弹性品种
  • 江苏省委组织部副部长高颜已任南京市委常委、组织部部长