其他分享
首页 > 其他分享> > 优先队列 - big先

优先队列 - big先

作者:互联网

优先队列

优先大的元素出,或者优先小的元素出

https://blog.csdn.net/yuewenyao/article/details/88764046

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
vector<long long> s, s1, e, e1;
void sort(int left, int right);
bool cmp(int wl, int hl, int i);
priority_queue<long long> q;
long long ee = 1e+9 + 7;


int main()
{
    int n(0), k(0);
    cin >> n >> k;
    for(int i = 0; i < n; i++) 
    {
        int tmp1(0);
        cin >> tmp1;
        s.push_back(tmp1);
        // s1.push_back(tmp1);
    }
    for(int i = 0; i < n; i++) 
    {
        int tmp2(0);
        cin >> tmp2;
        e.push_back(tmp2);
        // e1.push_back(tmp2);
    }
    int len = e.size();
    sort(0, len-1);
    // for(int i = 0; i < e.size(); i++) cout << e[i] << " ";
    // cout << endl;
    long long sum = 0;
    long long ans = 0;
    for(int i = 0; i < len; i++) // 从效率最高的工人降序选择,第一次只能选	一个,然后两个,三个,...,k个(最多),这个时候用优先队列来进行维护
    {
        sum += s[i];
        q.push(-s[i]);  // 填入负值是和上一个sum语句相关的
        while(q.size() > k)  // 保证最低效率情况下能获得最高的速度
        {
            sum += q.top();
            q.pop();
        }
        ans = max(ans, sum*e[i]);

    }
    // cout << ee << endl;
    cout << ans%ee;
    return 0;
}


void sort(int left, int right)  
{
if(left >= right) return;

int i = left;
int j = right;
int wl = s[left];
int hl = e[left];
while(i < j)
{
    // cout << i << " " << j << " " << left << endl;
    while(i < j && cmp(wl, hl, j)) j--;
    s[i] = s[j];
    e[i] = e[j];

    while(i < j && !cmp(wl, hl, i)) i++;
    s[j] = s[i];
    e[j] = e[i];
}
// cout << 1 << endl;
s[i] = wl;
e[i] = hl;
sort(left, i-1);
sort(i+1, right);
}
bool cmp(int wl, int hl, int i)
{
    // if(wl < w[i]) return true;
    // if(wl > w[i]) return false;
    if(hl > e[i]) return true;
    else return false;
}

标签:优先,return,队列,big,back,int,include,tmp1,tmp2
来源: https://www.cnblogs.com/SKEZhi/p/14660176.html