1427: the number of columns poor

1427 : the number of columns poor

 

[answer]

This is a question I do use priority queue QWQ

Talk about thinking of it

 

This is a greedy title

We have a set of numbers

Removal of two each x, y, then add a new x * y + 1

 

        If you ensure that the final to get a maximum number, then the two lowest number each time removed, in exchange for a higher number (so the final result will be increasing)

        If you ensure that the final to get a minimum number, then remove the two largest numbers each, in exchange for a new number (Since you removed the two largest numbers, then in the end you will lose the maximum number of multiply so that the results of the greatest opportunities , so this operation so that the results are presented decreasing trend, the result is the smallest it)

 

[Code]

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<functional>

using namespace std;

int n,maxn,minn,shu,x,y,ac[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",&ac[i]);
    
    for(int i=1;i<=n;i++)
        a.push(ac[i]);  
    for(int i=1;i<=n;i++)
        b.push(ac[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;
}

 

Guess you like

Origin www.cnblogs.com/xiaoyezi-wink/p/10988008.html