您的位置:首页 > 其它

Problem - 218B - Codeforces

2014-03-16 14:03 2416 查看
http://codeforces.com/problemset/problem/218/B

Description

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 Input

Input
4 3
2 1 1


Output
5 5


Input
4 3
2 2 2


Output
7 6


Hint

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.

 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
int n,m,i;
int c,t;
int min,max;
int a[1001];
scanf("%d %d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+m);
t=n;
min=0;
max=0;
for(i=0;i<m;i++)
{
c=a[i];
while(c&&t>0)
{
min+=c;
c--;
t--;
}
if(t<=0)
{
break;
}
}

while(n)
{
max+=a[m-1];
a[m-1]--;
n--;
sort(a,a+m);
}

printf("%d %d\n",max,min);
return 0;
}


下面是一个错误的代码,在考虑最大值时未考虑全面。代码粘贴在这里,警醒后人:

#include <stdio.h>//错误的代码
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int a[1005],b[1005];
int cmp1(const int &x,const int &y)
{
if(x>y)
return 1;
else
return 0;
}
int cmp2(const int &x,const int &y)
{
if(x<y)
return 1;
else
return 0;
}
int main()
{
int  n,m;
while(~scanf("%d%d",&n,&m))
{
int x=n;
int sum=0;
int sum2=0;
for(int i=0; i<m; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(a,a+m,cmp1);
/*for(int i=0; i<m; i++)
printf("%d ",a[i]);
printf("\n");*/
int i=0;
while(n>0)//未考虑当a[i]已经减小到与a[i-1]相比等于或小于时就要换
{
if(a[i]>=a[i+1])
{
sum+=a[i];
a[i]--;
n--;
}
else
i++;
}
sort(b,b+m,cmp2);
i=0;
while(x>0)
{
if(b[i]>0)
{
sum2+=b[i];
b[i]--;
x--;
}
else
i++;
}
printf("%d %d\n",sum,sum2);
}
return 0;
}


利用优先队列的写法:

#include<iostream>
#include<functional>
#include<queue>
#include<memory.h>
using namespace std;

int main()
{
int n,m,max,min;
int a[1001];
while(cin>>n>>m)
{
max=0;//最大值
min=0;//最小值
int i;
memset(a,0,sizeof(a));
//创建一个空队列qi,用来求最大值
priority_queue<int> qi;
//创建队列一个空队列qi2,用来求最小值
priority_queue<int, vector<int>, greater<int> >qi2;
for(i=0; i<m; i++)
{
cin>>a[i];
qi.push(a[i]); //把m个数压到队列qi中
qi2.push(a[i]); //把m个数压到队列qi2中
}
for(i=0; i<n; i++)
{
int z=qi.top(); //把队列中最大值赋给z
qi.pop();  //把最大值丢掉
max+=z;  //把这个数加入到max中
z--;  //把z加入到max中后减去1
qi.push(z); //把z减去1后压入到队列中
}
for(i=0; i<n; i++)
{
int k=qi2.top();  //把队列中最小的值赋给k
if(k!=0)  //如果k不等于0,把k加入到min中,然后k减去1
{
min+=k;
k--;
//如果k减去1后为0,踢出队列;否则踢掉原来的k,
//然后把k减去1后的值压入到队列中
if(k==0)
qi2.pop();
else
{
qi2.pop();
qi2.push(k);
}
}
}
cout<<max<<" "<<min<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: