1427:数列极差
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 1771 通过数: 797
【题目描述】
在黑板上写了N个正整数作成的一个数列,进行如下操作:每一次擦去其中的两个数a和b,然后在数列中加入一个数a×b+1,如此下去直至黑板上剩下一个数,在所有按这种操作方式最后得到的数中,最大的max,最小的为min,则该数列的极差定义为M=max−min。
【输入】
第一行,一个数为N;
第二行,N个数。
【输出】
输出极差。
【输入样例】
3
1 2 3
【输出样例】
2
思路:贪心题,一组数字每次去掉两个 x , y,再加入一个新的 x*y+1,如果保证最后得到一个最大的数字,那么每次去掉两个最小的数字,换来一个较大的数字,这样最后结果会越来越大 如果保证最后得到一个最小的数字,那么每次去掉两个最大的数字,换来一个新数字由于你去掉了两个最大的数字,所以这个操作过程使得结果呈现递减趋势,结果就最小。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int n,maxn,minn,s,x,y,ca[10001];
priority_queue<int,vector<int>,greater<int> >a;//优先队列从小到大
priority_queue<int>b;
int main(){
scanf("%d",&n);
for(int i = 1; i <= n; i++)
scanf("%d",&ca[i]);
for(int i = 1; i <= n; i++)
a.push(ca[i]);
for(int i = 1; i <= n; i++)
b.push(ca[i]);
for(int i = 1; i <= n - 1; i++) //最大值
{
x = a.top();
a.pop();
y = a.top();
a.pop();
a.push(x * y + 1);
}
maxn = a.top();
for(int i = 1; i <= n - 1; i++)//最小值
{
x = b.top();
b.pop();
y = b.top();
b.pop();
b.push(x * y + 1);
}
minn = b.top();
printf("%d",maxn - minn);
return 0;
}