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 ... ANOutput
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 1Sample Output 1
2
1×2 rectangle can be formed.Sample Input 2
4
1 2 3 4Sample Output 2
0
No rectangle can be formed.Sample Input 3
10
3 3 3 3 4 4 4 5 5 5Sample 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;
}