[JAG practice] A - Best Matched Pair 遍历+水题

A - Best Matched Pair


Time limit : 2sec / Memory limit : 512MB

Problem Statement

You are working for a worldwide game company as an engineer in Tokyo. This company holds an annual event for all the staff members of the company every summer. This year's event will take place in Tokyo. You will participate in the event on the side of the organizing staff. And you have been assigned to plan a recreation game which all the participants will play at the same time.

After you had thought out various ideas, you designed the rules of the game as below.

  • Each player is given a positive integer before the start of the game.
  • Each player attempts to make a pair with another player in this game, and formed pairs compete with each other by comparing the products of two integers.
  • Each player can change the partner any number of times before the end of the game, but cannot have two or more partners at the same time.
  • At the end of the game, the pair with the largest product wins the game.

In addition, regarding the given integers, the next condition must be satisfied for making a pair.

  • The sequence of digits obtained by considering the product of the two integers of a pair as a string must be increasing and consecutive from left to right. For example,  2 2 23 23, and  56789 56789 meet this condition, but  21 21 334 334 135 135 or  89012 89012 do not.

Setting the rules as above, you noticed that multiple pairs may be the winners who have the same product depending on the situation. However, you can find out what is the largest product of two integers when a set of integers is given.

Your task is, given a set of distinct integers which will be assigned to the players, to compute the largest possible product of two integers, satisfying the rules of the game mentioned above.


Input

The input consists of a single test case formatted as follows.

N N
a1 a2  aN a1 a2 … aN

The first line contains a positive integer  N N which indicates the number of the players of the game.  N N is an integer between  1 1 and  1,000 1,000. The second line has  N Npositive integers that indicate the numbers given to the players. For  i=1,2,,N1 i=1,2,…,N−1, there is a space between  ai ai and  ai+1 ai+1 ai ai is between  1 1 and  10,000 10,000 for  i=1,2,,N i=1,2,…,N, and if  ij i≠j, then  aiaj ai≠aj.

Output

Print the largest possible product of the two integers satisfying the conditions for making a pair. If any two players cannot make a pair, print  1 −1.



Sample Input 1

Copy
2
1 2

Output for the Sample Input 1

Copy
2

Sample Input 2

Copy
3
3 22 115

Output for the Sample Input 2

Copy
345

Sample Input 3

Copy
2
1 11

Output for the Sample Input 3

Copy
-1

Sample Input 4

Copy
2
5 27

Output for the Sample Input 4

Copy
-1

Sample Input 5

Copy
2
17 53

Output for the Sample Input 5

Copy
-1

Sample Input 6

Copy
10
53 43 36 96 99 2 27 86 93 23

Output for the Sample Input 6

Copy
3456
遍历判断求解,比较简单

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull; 
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n;
int a[1005];
int maxx;
char s[10000005];
int main()
{
    memset(a,0,sizeof(a));
    maxx = -1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            int t = a[i]*a[j];
            int t1=t;
            int len =0,k=0;
            while(t1!=0)
            {
                s[len++]=t1%10+'0';
                t1/=10;
            }
            s[len] = '\0';
            //puts(s);
            for(k=0;k<len-1;k++)
            {
                if(s[k]!=s[k+1]+1)
                    break;
            }
            if(k==len-1)
            {
                if(t>maxx)
                    maxx=t;
            }
        }
    }
    printf("%d",maxx);
    
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值