Make a Rectangle(Atcoder-2696)

博客围绕从给定的 N 根棍子中选 4 根组成矩形求最大面积的问题展开。给出了问题描述、约束条件、输入输出格式及示例。解题思路是选两组每组个数为 2 的长度最大的棍子,因棍子长度大,用 multiset 统计,将个数大于 2 的棍子加入优先队列,取队首前两个元素求积。

Problem Description

We have N sticks with negligible thickness. The length of the i-th stick is Ai.

Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides. Find the maximum possible area of the rectangle.

Constraints

  • 4≤N≤105
  • 1≤Ai≤109
  • Ai is an integer.

Input

Input is given from Standard Input in the following format:

N
A1 A2 ... AN

Output

Print the maximum possible area of the rectangle. If no rectangle can be formed, print 0.

Example

Sample Input 1

6
3 1 2 4 2 1

Sample Output 1

2
1×2 rectangle can be formed.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
No rectangle can be formed.

Sample Input 3

10
3 3 3 3 4 4 4 5 5 5

Sample Output 3

20

题意:给出 n 个棍子的长度,要在这 n 个棍子中选 4 个的棍子组成,求矩形的最大面积

思路:实质是要选两组每组个数为 2 的长度最大的棍子,由于棍子的长度最大到 1E9,无法使用数组进行桶排,因此可以使用 multiset 来进行统计,然后将所有个数大于 2 的棍子加入到优先队列中,取队首前两个元素求积即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
struct cmp{
    bool operator()(const LL &a,const LL &b){
        return a>b;
    }
};
multiset<LL,cmp> s;
int main() {
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        LL x;
        scanf("%lld",&x);
        s.insert(x);
    }

    multiset<LL>::iterator it;
    priority_queue<LL> Q;
    LL maxx=0;
    int num=0;
    for(it=s.begin();it!=s.end();it++){
        if(*it==maxx)
            num++;
        else{
            maxx=*it;
            num=1;
        }
        if(num>=2){
            num=0;
            Q.push(*it);
        }
    }
    if(Q.size()<2)
        printf("0\n");
    else{
        LL w=Q.top();Q.pop();
        LL h=Q.top();Q.pop();
        printf("%lld\n",w*h);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值