I-The Crime-solving Plan of Groundhog2020牛客暑期多校训练营(第九场)

I-The Crime-solving Plan of Groundhog2020牛客暑期多校训练营(第九场)
链接:https://ac.nowcoder.com/acm/contest/5674/I
来源:牛客网

题目描述
Today, ZLZX has a mysterious case: Orange lost his down jacket hanging in his dorm room. Under the expectations of everyone, detective Groundhog took his small spoon of the artifact and started the journey to solve the case.

After an in-depth investigation of the northernmost mysterious room on each floor, Groundhog discovered n mysterious numbers. As long as the clues conveyed by these numbers are deciphered, he can reveal the truth of the matter. The deciphering method is: using these numbers to generate two positive integers without leading zeros, and minimizing the product of these two positive integers is the final clue.

Then Groundhog wants to know: What is the smallest product?

As he continued to investigate in the room west of the new building, he gave you the question.

Concise meaning:Given n numbers between 0 and 9, use them to make two positive integers without leading zeros to minimize the product.

输入描述:
The first line of input is a single integer T,the number of test cases.
For each set of data:
Each test case begins with a single integer n , the count of numbers.
The next line are n numbers.
输出描述:
For each set of Case, an integer is output, representing the smallest product.

示例1
输入

1
4
1 2 2 1

输出

122

示例2
输入

2
5
1 3 2 1 2
3
1 1 0

输出

1223
10

备注:

1⩽T⩽1000,2⩽n⩽100000,1⩽∑n⩽1000000 There are at least two Numbers that are guaranteed not to be zero. The Numbers range between [0,9].

题意:
给你n个数,让你实现A*B最小,A、B由给的n个数组成,不能有前导零
题解:
• 把当前的数字拆成4个数 𝑎, 𝑏, 𝑐, 𝑑(𝑎 ≤ 𝑏 ≤ 𝑐 ≤ 𝑑) ,那么我们有两种决策:两位数×两位数,或者三位数×一
位数。
• 10𝑎 + 𝑑 ⋅ 10𝑏 + 𝑐 = 100𝑎𝑏 + 10𝑎𝑐 + 10𝑏𝑑 + 𝑐𝑑
• 100𝑏 + 10𝑐 + 𝑑 ⋅ 𝑎 = 100𝑎𝑏 + 10𝑎𝑐 + 𝑎𝑑 < 10𝑎 + 𝑑 ⋅ 10𝑏 + 𝑐 • 同理类推,
• 可以证明留一个最小的正整数作为第一个数,剩下的所有数字排成最小的数作为第二个数时,答案取到最
小值。
• 注意高精度细节和“正整数”、“整数”的区分,以及前导0的处理。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1.0),eps=1e-8;
const int maxn=5e5+10;
const ll mod = 1e9+7;
int a[100010],c[1000010],d[1000010],e[1000010];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        memset(e,0,sizeof(e));
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        int num=0;
        int k=0;
        while(a[k]==0)
        {
            k++;
            num++;
        }
        int t=a[k];
        k++;
        int cnt=0;
        c[cnt++]=a[k];
        for(int i=0; i<num; i++)
            c[cnt++]=0;
        for(int i=k+1; i<n; i++)
        {
            c[cnt++]=a[i];
        }
        //printf("%d ",t);
        //for(int i=0;i<cnt;i++)
        //{
        //    printf("%d",c[i]);
        //}printf("\n");
        for(int i=0; i<cnt; i++)
        {
            d[i]=c[cnt-1-i];
            //printf("%d",d[i]);
        }
        for(int i=0; i<cnt; i++)
        {
            e[i]+=d[i]*t;
            e[i+1]+=e[i]/10;
            e[i]%=10;
        }
        if(e[cnt]>0)printf("%d",e[cnt]);
        for(int i=cnt-1; i>=0; i--)
        {
            printf("%d",e[i]);
        }
        printf("\n");
    }
    return 0;
}
/*
100
4
1 2 2 1
4
0 0 1 1
6
9 8 7 6 5 1
6
9 8 7 6 5 2
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值