A - Gaby And Addition
Gaby is a little baby who loves playing with numbers. Recently she has learned how to add 2 numbers using the standard addition algorithm which we summarize in 3 steps:
- Line up the numbers vertically matching digits places.
- Add together the numbers that share the same place value from right to left.
- Carry if necessary.
it means when adding two numbers we will get something like this:
Unfortunately as Gaby is too young she doesn't know what the third step means so she just omitted this step using her own standard algorithm (Gaby's addition algorithm). When adding two numbers without carrying when necessary she gets something like the following:
Gaby loves playing with numbers so she wants to practice the algorithm she has just learned (in the way she learned it) with a list of numbers adding every possible pair looking for the pair which generates the largest value and the smallest one.
She needs to check if she is doing it correctly so she asks for your help to find the largest and the smallest value generated from the list of numbers using Gaby's addition algorithm.
Input
The input starts with an integer n (2 ≤ n ≤ 106) indicating the number of integers Gaby will be playing with. The next line contains n numbers ni (0 ≤ ni ≤ 1018) separated by a single space.
Output
Output the smallest and the largest number you can get from adding two numbers from the list using Gaby's addition algorithm.
Examples
Input
6 17 5 11 0 42 99
Output
0 99
Input
7 506823119072235413 991096248449924896 204242310783332529 778958050378192979 384042493592684633 942496553147499866 410043616343857825
Output
52990443860776502 972190360051424498
Note
In the first sample input this is how you get the minimum and the maximum value
字典树的题目
把每个数都化成一位一位的保存在字典树中,个位在最下面。
然后输入一个数就找一次最大最小值
#include<bits/stdc++.h>
using namespace std;
long long a[1000005],tree[13000005][10],cnt,flag,aa[55];
void insert(long long x,long long t)//字典树的插入操作
{
for(long long i=0; i<=18; i++)//分解
{
aa[i]=x%10;
x/=10;
}
for(long long i=18; i>=0; i--)
{
if(tree[t][aa[i]]==0)
{
tree[t][aa[i]]=++cnt;
}
t=tree[t][aa[i]];
}
}
long long find(long long x,long long t)//查找最大值的操作
{
long long sum=0;
for(long long i=0; i<=18; i++,x/=10)//分解
aa[i]=x%10;
for(long long i=18; i>=0; i--)
{
long long mx=-1;//給一个最小值
for(long long j=0; j<=9; j++)
{
if(tree[t][j]&&mx<(aa[i]+j)%10)//这个数存在 并且加起来最大
{
mx=(aa[i]+j)%10;
flag=j;
}
}
sum=sum*10+mx;
t=tree[t][flag];
}
return sum;
}
long long find2(long long x,long long t)//查找最小值的操作 同上
{
long long sum=0;
for(long long i=0; i<=18; i++,x/=10)
aa[i]=x%10;
for(long long i=18; i>=0; i--)
{
long long mx=15;//赋一个最大值
for(long long j=0; j<=9; j++)
{
if(tree[t][j]&&mx>(aa[i]+j)%10)
{
mx=(aa[i]+j)%10;
flag=j;
}
}
sum=sum*10+mx;
t=tree[t][flag];
}
return sum;
}
int main()
{
long long n,maxn=0,minn=0x3f3f3f3f3f3f3f3f;
scanf("%lld",&n);
for(long long i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
if(i>1)//只有大于2个数才能相加
{
maxn=max(maxn,find(a[i],0));
minn=min(minn,find2(a[i],0));//求出最大最小值
}
insert(a[i],0);//先求再插入 ,因为不能和自己相加
}
printf("%lld %lld\n", minn, maxn);
return 0;
}