【LOJ#10005】数列极差——贪心

好的时隔多年终于来更博客了


题目链接

题意很明确了,一眼就知道是要贪心,但是怎么贪呢?

对于a1,a2,a3,不妨令a1<a2<a3,那么就有三种取法:

s1=(a1*a2+1)*a3+1=a1*a2*a3+a3+1;

s2=(a2*a3+1)*a1+1=a1*a2*a3+a1+1;

s3=(a1*a3+1)*a2+1=a1*a2*a3+a2+1;

显然s2最大,s1最小,推广到一般情况就是每次取最大的两个数可以得到最小值,每次取最小的两个数可以得到最大值

然后维护两个优先队列就行了

数据水的一B

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<algorithm>
 5 using namespace std;
 6 struct node{
 7     int w;
 8     bool operator <(const node&x)const{return w>x.w;}
 9 };
10 int read(){
11     int ans=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){ans=ans*10+c-48;c=getchar();}
14     return ans*f;
15 }
16 priority_queue<int>q;
17 priority_queue<node>p;
18 int main(){
19     int n=read();
20     for(int i=1;i<=n;i++){
21         int x=read();
22         q.push(x);p.push((node){x});
23     }
24     read();
25     for(int i=1;i<n;i++){
26         int x=q.top();q.pop();x*=q.top();q.pop();q.push(x+1);
27         node y=p.top(),z;p.pop();z=p.top();p.pop();
28         p.push((node){y.w*z.w+1});
29     }
30     node pp=p.top();
31     printf("%d\n",pp.w-q.top());
32     return 0;
33 }
View Code

猜你喜欢

转载自www.cnblogs.com/JKAI/p/9829739.html