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

来源:互联网 发布:淘宝被投诉未生产 编辑:程序博客网 时间:2024/04/27 01:57

题目

题目链接: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 secondusing namespace std;typedef long long LL;typedef pair<int,int> PII;// headconst 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
原创粉丝点击