CF1132D Streessful Training(二分+贪心+优先队列*2300)

简介: CF1132D Streessful Training(二分+贪心+优先队列*2300)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
typedef long long ll;
using namespace std;
const int N = 3e5 + 10;
ll n, k;
ll a[N], b[N];
struct node {
  ll a;
  ll b;
  ll c;
  bool operator<(const node &w)const {
    return c > w.c;  //从堆底到根 从大到小 即 小根堆
  }
};
bool check(ll x) {
  priority_queue<node>q;
  for (int i = 1; i <= n; i++) {
    if (a[i] / b[i] < k)
      q.push({a[i], b[i], a[i] / b[i]});
  }
  if (q.empty()) return true;
  for (int i = 0; i < k; i++) {
    auto t = q.top();
    q.pop();
    if (t.c < i) return false;
    if ((t.a + x) / t.b  < k) {
      q.push({t.a + x, t.b, (t.a + x) / t.b });
    }
    if (q.empty()) return true;
  }
  return true;
}
int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
  //枚举最小答案
  ll l = 0, r = 3e12 + 10;
  bool f = 0;
  while (l < r) {
    ll mid = l + r >> 1;
    if (check(mid)) {
      r = mid;
      f = 1;
    } else l = mid + 1;
  }
  if (f == 1)
    cout << l << "\n";
  else cout << -1 << "\n";
  return  0;
}
目录
相关文章
poj 1088 记忆化搜索||动态规划
记忆化搜索也也是采用递归深搜的对数据进行搜索,但不同于直接深搜的方式,记忆化搜索是在每次搜索时将得到的结果保存下来,避免了重复计算,这就是所谓的记忆化。记忆化应该是属于动态规划。
53 0
|
人工智能 算法 BI
剑指offer(C++)-JZ66:构建乘积数组(算法-其他)
剑指offer(C++)-JZ66:构建乘积数组(算法-其他)
|
算法 C++ Python
算法笔记(1)—— 搜索算法:线性搜索(LS)、二分搜索(BS)、记忆化搜索(MS)
算法笔记(1)—— 搜索算法:线性搜索(LS)、二分搜索(BS)、记忆化搜索(MS)
4037 0
|
机器学习/深度学习
[LeeCode][动态规划][简单] 杨辉三角
[LeeCode][动态规划][简单] 杨辉三角
81 0
51nod 1255 字典序最小的子序列 (贪心 + stack)
51nod 1255 字典序最小的子序列 (贪心 + stack)
110 0
|
人工智能 BI
cf 489B(贪心)
cf 489B(贪心)
109 0
|
存储 C++
【 LeetCode 热题 HOT 100】2. 两数相加 (C++ 链表 模拟)
【 LeetCode 热题 HOT 100】2. 两数相加 (C++ 链表 模拟)
169 0
【 LeetCode 热题 HOT 100】2. 两数相加 (C++ 链表 模拟)
|
人工智能
CF1315 C.Restoring Permutation(构造 二分 贪心)
CF1315 C.Restoring Permutation(构造 二分 贪心)
82 0
CF1310A. Recommendations(贪心 优先队列 并查集)
CF1310A. Recommendations(贪心 优先队列 并查集)
102 0
|
机器学习/深度学习 人工智能 算法
CF1446D Frequency Problem(思维 前缀和 根号分治 双指针)
CF1446D Frequency Problem(思维 前缀和 根号分治 双指针)
103 0

热门文章

最新文章