佳佳的老师在黑板上写了一个由 nn 个正整数组成的数列,要求佳佳进行如下操作:每次擦去其中的两个数 aa 和 bb,然后在数列中加入一个数 a\times b+1a×b+1,如此下去直至黑板上剩下一个数为止,在所有按这种操作方式最后得到的数中,最大的为 \maxmax,最小的为 \minmin, 则该数列的极差定义为 M=\max -\minM=max−min。
由于佳佳忙于准备期末考试,现请你帮助他,对于给定的数列,计算出相应的极差 MM。
输入格式
第一行为一个正整数 nn 表示正整数序列的长度;
在接下来的 nn 行中,每行输入一个正整数。
接下来的一行有一个 00,表示数据结束。
输出格式
输出只有一行,为相应的极差 dd。
样例
Inputcopy | Outputcopy |
3 1 2 3 0 | 2 |
ac代码:
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int main()
{ int a,x,y,m,max,min;
while(scanf("%d",&a)&&a!=0)
{
priority_queue<int,vector<int>,greater<int>>q;
priority_queue<int,vector<int>,less<int>>s;
for(int i=0;i<a;i++)
{cin>>m;
q.push(m);
s.push(m);
}
while(q.size()!=1)
{
x=q.top();
q.pop();
y=q.top();
q.pop();
x=x*y+1;
q.push(x);
}
max=q.top();
while(s.size()!=1)
{
int b=s.top();
s.pop();
int c=s.top();
s.pop();
b=b*c+1;
s.push(b);
}
min=s.top();
}
cout<<max-min<<endl;}