[Codeforces] 218B - Airport

来源:互联网 发布:php字符串查找 编辑:程序博客网 时间:2024/06/06 12:30

题意:

航空公司的票价规定是根据飞机的剩余座位决定,票价=剩余座位数。

求航空公司能赚的最大值和最小值。

最大值也就是每个人都选空座最多的航班,最小值也就是,每个人都选空座最少的航班。

求最大值可以用优先队列。最小值直接把航班剩余空座从小到大排序,依次取座数最小的航班,直到选完n个人即可。

#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <algorithm>#include <iostream>#include <set>#include <map>#include <queue>#include <stack>using namespace std;int main(){    //freopen("test0.in", "r", stdin);    //freopen("test0.out", "w", stdout);    int n, m, a[2000], maxx, minn;    while(~scanf("%d %d", &n, &m))    {        maxx = minn = 0;        priority_queue<int> pq;        for(int i = 0; i < m; i++)        {            scanf("%d", &a[i]);            pq.push(a[i]);        }        sort(a, a+m);        int tmp = n;        for(int i = 0; i < m; i++)        {            if(tmp >= a[i])            {                tmp -= a[i];                minn += (1 + a[i]) * a[i] / 2;            }            else            {                minn += (a[i] + a[i]- tmp + 1) * tmp / 2;                break;            }        }        while(n--)        {            int temp = pq.top();            pq.pop();            maxx += temp;            if(--temp)            {                pq.push(temp);            }        }        printf("%d %d\n", maxx, minn);    }    return 0;}



0 0
原创粉丝点击