cf Gaby And Addition (字典树 + 贪心)

http://codeforces.com/gym/101466/problem/A

A. Gaby And Addition

time limit per test

6.0 s

memory limit per test

1024 MB

input

standard input

output

standard output

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:

  1. Line up the numbers vertically matching digits places.
  2. Add together the numbers that share the same place value from right to left.
  3. 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 nnumbers 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

Copy

6
17 5 11 0 42 99

output

Copy

0 99

input

Copy

7
506823119072235413 991096248449924896 204242310783332529 778958050378192979 384042493592684633 942496553147499866 410043616343857825

output

Copy

52990443860776502 972190360051424498

Note

In the first sample input this is how you get the minimum and the maximum value


题意是给你n个数,两个数相加不进位, 求任意两个数的最大和最小的和

首先将高位补零,让每个数都变成长度为20的字符串,

每插入一个新的字符串

从高位开始,每一位, 都在这一位对应的层数中,找到最小值,标记这个最小值,然后第二位从这个最小值的孩子节点开始,重复这个步骤

求最小值也一样

code:

#include<bits/stdc++.h>
using namespace std;
using lon = unsigned long long;

const int N = 2e7;
int t[N][10], pos;
lon ans_min = numeric_limits<lon>::max();
lon ans_max = numeric_limits<lon>::min();

void insert(string s)
{
	int k = 0, x;
	for(int i = 0; i < s.length(); i++)
	{
		x = s[i] - '0';
		if(t[k][x] == 0)
			t[k][x] = ++pos;
		
		k = t[k][x];
	}
}

void find_max(string s)
{
	int k = 0, x = 0, bit = 0, go = 0; lon res = 0;

	for(int i = 0; i < s.length(); i++)//每一位 
	{
		x = s[i] - '0', bit = 0;
		
		for(int j = 0; j < 10; j++)
		if(t[k][j] && (j+x)%10 >= bit)//如果这里有值 && 和比bit小一些 
		{
			bit = (j + x) % 10;
			go = j;
		}
		
		res *= 10, res += bit;
		k = t[k][go];//去最小的这一位的下一层 
	}
	
	ans_max = max(ans_max, res);
}

void find_min(string s)
{
	int k = 0, x = 0, bit = 0, go = 0; lon res = 0;
	
	for(int i = 0; i < s.length(); i++)
	{
		x = s[i] - '0', bit = 9;
		
		for(int j = 0; j < 10; j++)
		if(t[k][j] && (j+x)%10 <= bit)
		{
			bit = (j + x) % 10;
			go = j;
		}
		
		res *= 10, res += bit;
		k = t[k][go];
	}
	
	ans_min = min(ans_min, res);
}

int main()
{
	int n; string s;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> s;
		while(s.length() < 20) s = '0' + s;
		
		if(i)
		{
			find_max(s);
			find_min(s);
		}
		
		insert(s);
	}
	
	cout << ans_min << ' ' << ans_max << endl;
	
	return 0;
	
}



















 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值