您的位置:首页 > 其它

hiho week 136(二分+优先队列)

2017-02-11 14:35 197 查看
题目链接:http://hihocoder.com/contest/hiho136/problem/1

题意:中文题诶~

思路:直接对缓存区长度二分就好了,注意计算当前长度的"延迟惩罚值"我们可以用优先队列。

时间复杂度为O(nlogn)

代码:

1 #include <bits/stdc++.h>
2 #define ll long long
3 using namespace std;
4
5 const int MAXN=1e5+10;
6 ll a[MAXN], key;
7 int n;
8
9 bool geloutingyu(int k){
10     ll ans=0;
11     int cc=1;
12     priority_queue<int, vector<int>, less<int> > q;
13     for(int i=0; i<k; i++){
14         q.push(a[i]);
15     }
16     for(int i=k; i<n; i++){
17         ans+=q.top()*(cc++);
18         q.pop();
19         q.push(a[i]);
20     }
21     while(!q.empty()){
22         ans+=q.top()*(cc++);
23         q.pop();
24     }
25     if(ans>key){
26         return false;
27     }else{
28         return true;
29     }
30 }
31
32 int main(void){
33     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
34     cin >> n >> key;
35     for(int i=0; i<n; i++){
36         cin >> a[i];
37     }
38     int l=1, r=n, gg, mid=(l+r)>>1;
39     bool flag=true;
40     while(mid>=1&&mid<=n&&l<r){
41         if(geloutingyu(mid)){
42             flag=false;
43             gg=mid;
44             r=mid;
45         }else{
46             l=mid+1;
47         }
48         mid=(l+r)>>1;
49     }
50     cout << gg << endl;
51     return 0;
52 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: