Codeforces 218B Airport

来源:互联网 发布:telnet 端口失败解决 编辑:程序博客网 时间:2024/06/05 16:51
B. Airport
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Lolek and Bolek are about to travel abroad by plane. The local airport has a special "Choose Your Plane" offer. The offer's conditions are as follows:

  • it is up to a passenger to choose a plane to fly on;
  • if the chosen plane has x (x > 0) empty seats at the given moment, then the ticket for such a plane costs x zlotys (units of Polish currency).

The only ticket office of the airport already has a queue of n passengers in front of it. Lolek and Bolek have not stood in the queue yet, but they are already wondering what is the maximum and the minimum number of zlotys the airport administration can earn if all npassengers buy tickets according to the conditions of this offer?

The passengers buy tickets in turn, the first person in the queue goes first, then goes the second one, and so on up to n-th person.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of passengers in the queue and the number of planes in the airport, correspondingly. The next line contains m integers a1, a2, ..., am (1 ≤ ai ≤ 1000) — ai stands for the number of empty seats in the i-th plane before the ticket office starts selling tickets.

The numbers in the lines are separated by a space. It is guaranteed that there are at least n empty seats in total.

Output

Print two integers — the maximum and the minimum number of zlotys that the airport administration can earn, correspondingly.

Sample test(s)
input
4 32 1 1
output
5 5
input
4 32 2 2
output
7 6
Note

In the first test sample the number of passengers is equal to the number of empty seats, so regardless of the way the planes are chosen, the administration will earn the same sum.

In the second sample the sum is maximized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person — to the 2-nd plane, the 3-rd person — to the 3-rd plane, the 4-th person — to the 1-st plane. The sum is minimized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person — to the 1-st plane, the 3-rd person — to the 2-nd plane, the 4-th person — to the 2-nd plane.


____________________________________________

思路:贪心加优先队列。
求最大值时,即每次都挑选空位最多的飞机。最小值即每次都挑选空位最少的飞机。
题目很水。但是priority_queue是我比赛时现学现用的,所以贴上来纪念一下。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <vector>#include <list>#include <deque>#include <queue>#include <iterator>#include <stack>#include <map>#include <set>#include <algorithm>#include <cctype>using namespace std;typedef long long LL;const int N = 1010;const int INF = 0x3f3f3f3f;int a[N];int main(){    int n, m, maxm = 0, minm = 0;    cin>>n>>m;    memset(a, 0, sizeof(a));    priority_queue<int> Q1;//缺省是大顶堆    priority_queue<int, vector<int>, greater<int> >Q2;    for(int i = 1; i <= m; i++){        cin>>a[i];        Q1.push(a[i]);        Q2.push(a[i]);    }    for(int i = 0; i < n; i++){        int t = Q1.top();        Q1.pop();        maxm+=t;        t--;        Q1.push(t);    }    for(int i = 0; i < n; i++){        int t = Q2.top();        Q2.pop();        minm+=t;        t--;        if(t!=0)        Q2.push(t);    }    printf("%d %d", maxm, minm);    return 0;}




原创粉丝点击