Gaby And Addition Gym - 101466A (初学字典树)

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 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

                                       

 

 这题也是被安排的明明白白  组队训练的时候这题不会做

后面说是字典树 学了2个小时字典树还是没写出来

心态蹦了

现学字典树

 

 1 #include <bits/stdc++.h>
 2 #define  pi acos(-1.0)
 3 #define  eps 1e-6
 4 #define  fi first
 5 #define  se second
 6 #define  lson l,m,rt<<1
 7 #define  rson m+1,r,rt<<1|1
 8 #define  bug         printf("******\n")
 9 #define  mem(a,b)    memset(a,b,sizeof(a))
10 #define  fuck(x)     cout<<"["<<x<<"]"<<endl
11 #define  f(a)        a*a
12 #define  sf(n)       scanf("%d", &n)
13 #define  sff(a,b)    scanf("%d %d", &a, &b)
14 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
15 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
16 #define  pf          printf
17 #define  FIN         freopen("DATA.txt","r",stdin)
18 #define  gcd(a,b)    __gcd(a,b)
19 #define  lowbit(x)   x&-x
20 #pragma  comment (linker,"/STACK:102400000,102400000")
21 using namespace std;
22 typedef long long LL;
23 typedef unsigned long long ULL;
24 const int INF = 0x7fffffff;
25 const LL LLINF = 0x3f3f3f3f3f3f3f3fll;
26 const int maxn = 1e6 + 10;
27 const int mod = 1e9 + 7;
28 LL t[20], a[maxn];
29 struct trie {
30     int cnt[maxn * 15], tree[maxn * 15][10], arr[20], root, rear;
31     int newnode() {
32         cnt[++rear] = 0;
33         mem(tree[rear], 0);
34         return rear;
35     }
36     void init() {
37         rear = 0;
38         root = newnode();
39     }
40     void add(LL x) {
41         int now = root, temp;
42         for (int i = 0 ; i < 19 ; i++) arr[i] = x % 10, x /= 10;
43         for (int i = 18 ; i >= 0 ; i--) {
44             temp = arr[i];
45             if (!tree[now][temp]) tree[now][temp] = newnode();
46             now = tree[now][temp];
47             cnt[now]++;
48         }
49     }
50     LL query1(LL x) {
51         int now = root, maxx, idx;
52         LL ret = 0;
53         for (int i = 0 ; i < 19 ; i++) arr[i] = x % 10, x /= 10;
54         for (int i = 18 ; i >= 0 ; i--) {
55             maxx = -1, idx = -1;
56             for (int j = 0 ; j < 10 ; j++)
57                 if (tree[now][j] && (arr[i] + j) % 10 > maxx) maxx = (arr[i] + j) % 10, idx = j;
58             ret += t[i] * maxx;
59             now = tree[now][idx];
60         }
61         return ret;
62     }
63     LL query2(LL x) {
64         int now = root, maxx, idx;
65         LL ret = 0;
66         for (int i = 0 ; i < 19 ; i++) arr[i] = x % 10, x /= 10;
67         for (int i = 18 ; i >= 0 ; i--) {
68             maxx = 10, idx = -1;
69             for (int j = 0 ; j < 10 ; j++)
70                 if (tree[now][j] && (arr[i] + j) % 10 < maxx  )  maxx = (arr[i] + j) % 10, idx = j;
71             ret += t[i] * maxx;
72             now = tree[now][idx];
73         }
74         return ret;
75     }
76 } tr;
77 int main() {
78     t[0] = 1;
79     for (int i = 1 ; i < 19 ; i++) t[i] = t[i - 1] * 10;
80     int n;
81     sf(n);
82     LL ans1 = (1LL) << 62, ans2 = 0;
83     tr.init();
84     for (int i = 0 ; i < n ; i++) {
85         scanf("%lld", &a[i]);
86         if (i) {
87             ans1 = min(ans1, tr.query2(a[i]));
88             ans2 = max(ans2, tr.query1(a[i]));
89         }
90         tr.add(a[i]);
91     }
92     printf("%lld %lld\n", ans1, ans2);
93     return 0;
94 }

 

 

 

 

转载于:https://www.cnblogs.com/qldabiaoge/p/9514777.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值