Priority Queue - Xuoi - Nguoc
Nguồn: https://codeforces.com/problemset/problem/218/B
Solve:
- //priority_queue <int> ; less
- //priority_queue <int,vector<int>,greater<int> > ; greater
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- priority_queue <ll> q_max;
- priority_queue<ll,vector<ll>,greater<ll>> q_min;
- int main(){
- ll n,m;
- cin>>n>>m;
- ll i;
- ll a[m];
- for(i=0;i<m;i++){
- cin>>a[i];
- q_max.push(a[i]);
- q_min.push(a[i]);
- }
- ll res_max=0, res_min=0;
- for(i=0;i<n;i++){
- ll q;
- ll p;
- q = q_max.top();
- p = q_min.top();
- q_max.pop();
- q_min.pop();
- if(q>0&&p>0){
- res_max=res_max+q;
- res_min=res_min+p;
- q--;p--;
- if(q>0) q_max.push(q);
- if(p>0) q_min.push(p);
- }
- }
- cout<<res_max<<" "<<res_min<<'\n';
- return 0;
- }
Nhận xét
Đăng nhận xét