hiho 1269 优化延迟 二分 优先队列

题目

题目链接:http://hihocoder.com/problemset/problem/1269

题目来源:hiho的比赛。

简要题意:给定计算惩罚值的公式,求出不超过某阈值最小的缓存大小。

题解

题目里头说的东西很明白,就是个堆。

惩罚值应该是单调递减的,然后直接二分缓存大小再去用堆来模拟,进行判断。

做的时候某个LL写成了int,然后后面被64位整数的输入输出坑了,换cin过掉。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 1e5+5;

int a[N];

LL cal(int len, int n) {
    priority_queue<int> q;
    LL cnt = 1;
    LL ans = 0;
    for (int i = 0; i < n; i++) {
        if (q.size() == len) {
            ans += cnt * q.top();
            cnt++;
            q.pop();
        }
        q.push(a[i]);
    }
    while (!q.empty()) {
        ans += cnt * q.top();
        cnt++;
        q.pop();
    }
    return ans;
}

int solve(int n, LL q) {
    int l = 1, r = n, ans = n;
    while (l <= r) {
        int mid = (l+r) / 2;
        LL temp = cal(mid, n);
        if (temp <= q) {
            ans = mid;
            r = mid-1;
        } else {
            l = mid+1;
        }
    }
    return ans;
}

int main() {
    int n;
    LL q;
    while (cin >> n >> q) {
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        if (cal(n, n) > q) {
            cout << -1 << endl;
        } else {
            cout << solve(n, q) << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值