C - Make a Rectangle
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
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.
Sample Input 1
Copy
6 3 1 2 4 2 1
Sample Output 1
Copy
2
1×2 rectangle can be formed.
Sample Input 2
Copy
4 1 2 3 4
Sample Output 2
Copy
0
No rectangle can be formed.
Sample Input 3
Copy
10 3 3 3 3 4 4 4 5 5 5
Sample Output 3
Copy
20
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int maxn=1e5+10; struct node{ int id; int nu; }b[maxn]; int n; int a[maxn]; int main() { int i,j; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) { scanf("%d",&a[i]); } sort(a,a+n); i=n-1; int cnt=0; int num=0; while(i>=0) { num++; for(j=i;j>=0;j--) { if(a[j]!=a[i]) break; else cnt++; } b[num].id=a[i]; b[num].nu=cnt; cnt=0; i=j; } int c[2]; int s=0; c[0]=-1; c[1]=-1; // for(i=1;i<=num;i++) // cout<<b[i].nu<<endl; for(i=1;i<=num;i++) { // cout<<"!"<<endl; if(b[i].nu>=4) { c[s]=b[i].id; s++; if(s>=2) break; c[s]=b[i].id; break; } if(b[i].nu>=2) { // cout<<"!"<<endl; c[s]=b[i].id; s++; } if(s>=2) break; } //cout<<c[0]<<" "<<c[1]<<endl; if(c[0]==-1||c[1]==-1) printf("0\n"); else printf("%lld\n",1ll*c[0]*c[1]); } }