题目描述
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.
题意:输入一串整数,选出最大且出现多次的两个数,输出两数乘机;
感觉是很水的一道题目,刚开始觉得用数组标记一下出现多次的数,排序就好了
但是发现数组开不了那么大,一直没想出来怎么做,错了好多次
突然想到set的插入,如果插入失败会有一个返回值,
如歌插入失败,则将这个数提取出来,排序,选择最大的就好了、然后就过了。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include<stdlib.h>
#include<cstdio>
#include <vector>
#include <set>
#define LL long long
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const LL maxn=1e6+5;
LL n;
LL a[maxn];
LL b[maxn];
set<LL >s;
int main()
{
cin >> n;
memset(a,0);
int k=0;
for(int i=0;i<n;i++)
{
cin >>a[i];
if(!s.insert(a[i]).second) //插入失败的返回值是0
{
s.erase(a[i]);
/*那个数如果出现多次,我们要先把前一个插入的删除,
不然后面存数据会有点问题*/
b[k]=a[i];
k++;
}
}
sort(b,b+k);
if(k<2)
cout << 0 << endl;
else
cout << b[k-1] *b[k-2] << endl;
}