问题 A: Make a Rectangle
时间限制: 1 Sec 内存限制: 128 MB
提交: 617 解决: 184
[提交] [状态] [讨论版] [命题人:admin]
题目描述
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 is given from Standard Input in the following format:
N
A1 A2 ... AN
输出
Print the maximum possible area of the rectangle. If no rectangle can be formed, print 0.
样例输入
6
3 1 2 4 2 1
样例输出
2
提示
1×2 rectangle can be formed.
参考代码:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int N;
while(cin>>N)
{
long long a[N+10];
for(int i=0; i<N; i++) cin>>a[i];
sort(a,a+N);
long long cnt=0,n,m;
for(int i=N-1; i>0; i--)
{
if(a[i]==a[i-1])
{
cnt++,i--;
if(cnt==1) n=a[i];
if(cnt==2){
m=a[i];
break;
}
}
}
if(cnt==2) cout<<n*m<<endl;
else cout<<0<<endl;
}
}