zoukankan      html  css  js  c++  java
  • hiho week 136(二分+优先队列)

    题目链接: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 }
  • 相关阅读:
    第二十一章 PHP编译安装(centos7)
    第二十章 nginx常见问题
    第十九章 keepalived高可用
    dijkstra
    求逆序对
    A
    P2014 [CTSC1997]选课
    樱花 混合背包
    1401D
    CF1343D
  • 原文地址:https://www.cnblogs.com/geloutingyu/p/6389137.html
Copyright © 2011-2022 走看看