问题 A: Make a Rectangle
时间限制: 1 Sec 内存限制: 128 MB
提交: 618 解决: 185
[提交] [状态] [讨论版] [命题人: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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define per(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b,a) for(int i=b;i>a;i--)
using namespace std;/////为何不再弄个数组呢?
#define N 100005
#define LL long long
int n;
long long a[N];
int main(){
scanf("%d",&n);
memset(a,0,sizeof(a));
per(i,0,n){
scanf("%lld",&a[i]);
}
sort(a,a+n);
int flag=0;
long long chang=0,wide=0;
rep(i,n-1,0){
if(a[i]==a[i-1]&&(flag==1)&&(a[i]!=chang)){///长方形
wide=a[i];
break;
}
if(a[i]==a[i-1]&&flag==0){
chang=a[i];
flag=1;
int j=i-2;///正方形
if(a[j]==a[j-1]&&a[j]==chang){
wide=a[j];
break;
}
}
}
if(chang==0||wide==0)
printf("0\n");
else {
long long area=chang*wide;
printf("%lld\n",area);
}
return 0;
}
///错误反思:数据类型错,不要完全相信题意,要有怀疑心态。
///要相信自己,但也要勇敢的怀疑自己