Make a Rectangle(Atcoder-2696)

Problem Description

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.

Example

Sample Input 1

6
3 1 2 4 2 1

Sample Output 1

2
1×2 rectangle can be formed.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
No rectangle can be formed.

Sample Input 3

10
3 3 3 3 4 4 4 5 5 5

Sample Output 3

20

题意:给出 n 个棍子的长度,要在这 n 个棍子中选 4 个的棍子组成,求矩形的最大面积

思路:实质是要选两组每组个数为 2 的长度最大的棍子,由于棍子的长度最大到 1E9,无法使用数组进行桶排,因此可以使用 multiset 来进行统计,然后将所有个数大于 2 的棍子加入到优先队列中,取队首前两个元素求积即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
struct cmp{
    bool operator()(const LL &a,const LL &b){
        return a>b;
    }
};
multiset<LL,cmp> s;
int main() {
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        LL x;
        scanf("%lld",&x);
        s.insert(x);
    }

    multiset<LL>::iterator it;
    priority_queue<LL> Q;
    LL maxx=0;
    int num=0;
    for(it=s.begin();it!=s.end();it++){
        if(*it==maxx)
            num++;
        else{
            maxx=*it;
            num=1;
        }
        if(num>=2){
            num=0;
            Q.push(*it);
        }
    }
    if(Q.size()<2)
        printf("0\n");
    else{
        LL w=Q.top();Q.pop();
        LL h=Q.top();Q.pop();
        printf("%lld\n",w*h);
    }
    return 0;
}

 

展开阅读全文

Make a Sequence

04-06

Your company's next product will be a new game, which is a three-dimensional variant of the classic game "Tic-Tac-Toe". Two players place balls in a three-dimensional space (board), and try to make a sequence of a certain length.nnPeople believe that it is fun to play the game, but they still cannot fix the values of some parameters of the game. For example, what size of the board makes the game most exciting? Parameters currently under discussion are the board size (we call it n in the following) and the length of the sequence (m). In order to determine these parameter values, you are requested to write a computer simulator of the game.nnYou can see several snapshots of the game in Figures 3-5. These figures correspond to the three datasets given in the Sample Input.nnnnHere are the precise rules of the game.nn 1. Two players, Black and White, play alternately. Black plays first.nn 2. There are n * n vertical pegs. Each peg can accommodate up to n balls. A peg can be specified by its x- and y-coordinates (1 <= x, y <= n). A ball on a peg can be specified by its z-coordinate (1 <= z <= n). At the beginning of a game, there are no balls on any of the pegs.nnnn 3. On his turn, a player chooses one of n * n pegs, and puts a ball of his color onto the peg. The ball follows the law of gravity. That is, the ball stays just above the top-most ball on the same peg or on the floor (if there are no balls on the peg). Speaking differently, a player can choose x- and y-coordinates of the ball, but he cannot choose its z-coordinate.nn 4. The objective of the game is to make an m-sequence. If a player makes an m-sequence or longer of his color, he wins. An m-sequence is a row of m consecutive balls of the same color. For example, black balls in positions (5, 1, 2), (5, 2, 2) and (5, 3, 2) form a 3-sequence. A sequence can be horizontal, vertical, or diagonal. Precisely speaking, there are 13 possible directions to make a sequence, categorized as follows.nnnn (a) One-dimensional axes. For example, (3, 1, 2), (4, 1, 2) and (5, 1, 2) is a 3-sequence. There are three directions in this category.nn (b) Two-dimensional diagonals. For example, (2, 3, 1), (3, 3, 2) and (4, 3, 3) is a 3-sequence. There are six directions in this category.nn (c) Three-dimensional diagonals. For example, (5, 1, 3), (4, 2, 4) and (3, 3, 5) is a 3- sequence. There are four directions in this category.nn Note that we do not distinguish between opposite directions.nnAs the evaluation process of the game, people have been playing the game several times changing the parameter values. You are given the records of these games. It is your job to write a computer program which determines the winner of each recorded game.nnSince it is difficult for a human to find three-dimensional sequences, players often do not notice the end of the game, and continue to play uselessly. In these cases, moves after the end of the game, i.e. after the winner is determined, should be ignored. For example, after a player won making an m-sequence, players may make additional m-sequences. In this case, all m-sequences but the first should be ignored, and the winner of the game is unchanged.nnA game does not necessarily end with the victory of one of the players. If there are no pegs left to put a ball on, the game ends with a draw. Moreover, people may quit a game before making any m-sequence. In such cases also, the game ends with a draw.nnInputnnThe input consists of multiple datasets each corresponding to the record of a game. A dataset starts with a line containing three positive integers n, m, and p separated by a space. The relations 3 <= m <= n <= 7 and 1 <= p <= n^3 hold between them. n and m are the parameter values of the game as described above. p is the number of moves in the game.nnThe rest of the dataset is p lines each containing two positive integers x and y. Each of these lines describes a move, i.e. the player on turn puts his ball on the peg specified. You can assume that 1 <= x <= n and 1 <= y <= n. You can also assume that at most n balls are put on a peg throughout a game.nnThe end of the input is indicated by a line with three zeros separated by a space.nnOutputnnFor each dataset, a line describing the winner and the number of moves until the game ends should be output. The winner is either "Black" or "White". A single space should be inserted between the winner and the number of moves. No other extra characters are allowed in the output.nnIn case of a draw, the output line should be "Draw".nnSample Inputnn3 3 3n1 1n1 1n1 1n3 3 7n2 2n1 3n1 1n2 3n2 1n3 3n3 1n4 3 15n1 1n2 2n1 1n3 3n3 3n1 1n3 3n3 3n4 4n1 1n4 4n4 4n4 4n4 1n2 2n0 0 0nSample OutputnnDrawnWhite 6nBlack 15 问答

没有更多推荐了,返回首页