中国石油大学 个人训练赛第五场A:Make a Rectangle

题目描述

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;
}

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值