A .Gaby And Addition (Gym - 101466A + 字典树)

Posted dillonh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A .Gaby And Addition (Gym - 101466A + 字典树)相关的知识,希望对你有一定的参考价值。

题目链接:http://codeforces.com/gym/101466/problem/A

题目:

技术分享图片

技术分享图片

技术分享图片

题意:

  给你n个数,重定义两个数之间的加法不进位,求这些数中两个数相加的最大值和最小值。

思路:

  字典树。我们首先将前i-1为放入字典树中,然后在查询第i位时,我们去字典树中查询,对每一位进行寻找,找到满足题意的当前位的最大值和最小值,然后继续更新下一位,最后维护总的最大值和最小值即可。

代码实现如下:

 

  1 #include <set>
  2 #include <map>
  3 #include <queue>
  4 #include <stack>
  5 #include <cmath>
  6 #include <bitset>
  7 #include <cstdio>
  8 #include <string>
  9 #include <vector>
 10 #include <cstdlib>
 11 #include <cstring>
 12 #include <iostream>
 13 #include <algorithm>
 14 using namespace std;
 15 
 16 typedef long long ll;
 17 typedef pair<ll, ll> pll;
 18 typedef pair<ll, int> pli;
 19 typedef pair<int, ll> pil;;
 20 typedef pair<int, int> pii;
 21 typedef unsigned long long ull;
 22 
 23 #define lson i<<1
 24 #define rson i<<1|1
 25 #define lowbit(x) x&(-x)
 26 #define bug printf("*********
");
 27 #define debug(x) cout<<"["<<x<<"]" <<endl;
 28 #define FIN freopen("D://code//in.txt", "r", stdin);
 29 #define IO ios::sync_with_stdio(false),cin.tie(0);
 30 
 31 const double eps = 1e-8;
 32 const int mod = 1e9 + 7;
 33 const int maxn = 1e6 + 7;
 34 const double pi = acos(-1);
 35 const int inf = 0x3f3f3f3f;
 36 const ll INF = 0x3f3f3f3f3f3f3f3f;
 37 
 38 int n;
 39 int le,root;
 40 int arr[20];
 41 ll num, pw[20];
 42 
 43 struct node{
 44     int nxt[10];
 45     void init(){
 46         for(int i = 0; i < 10; i++) nxt[i] = -1;
 47     }
 48 }T[13*maxn];
 49 
 50 void insert(ll x){
 51     int now = root;
 52     for(int i = 0; i <= 18; i++) {
 53         arr[i] = x % 10;
 54         x /= 10;
 55     }
 56     for(int i = 18;i >= 0; i--){
 57         int num = arr[i];
 58         if(T[now].nxt[num] == -1){
 59             T[le].init();
 60             T[now].nxt[num] = le++;
 61         }
 62         now = T[now].nxt[num];
 63     }
 64 }
 65 
 66 ll search1(ll x){
 67     int now = root, mx, idx;
 68     ll res = 0;
 69     for(int i = 0; i <= 18; i++) {
 70         arr[i] = x % 10;
 71         x /= 10;
 72     }
 73     for(int i = 18; i >= 0; i--) {
 74         mx = -1, idx = -1;
 75         for(int j = 0; j < 10; j++) {
 76             if(T[now].nxt[j] != -1 && (j + arr[i]) % 10 > mx) {
 77                 mx = (j + arr[i]) % 10;
 78                 idx = j;
 79             }
 80         }
 81         now = T[now].nxt[idx];
 82         res = res + mx * pw[i];
 83     }
 84     return res;
 85 }
 86 
 87 ll search2(ll x){
 88     int now = root, mx, idx;
 89     ll res = 0;
 90     for(int i = 0; i <= 18; i++) {
 91         arr[i] = x % 10;
 92         x /= 10;
 93     }
 94     for(int i = 18; i >= 0; i--) {
 95         mx = 11, idx = -1;
 96         for(int j = 0; j < 10; j++) {
 97             if(T[now].nxt[j] != -1 && (j + arr[i]) % 10 < mx) {
 98                 mx = (j + arr[i]) % 10;
 99                 idx = j;
100             }
101         }
102         now = T[now].nxt[idx];
103         res = res + mx * pw[i];
104     }
105     return res;
106 }
107 
108 int main() {
109     le = 1;
110     pw[0] = 1;
111     for(int i = 1; i <= 18; i++) pw[i] = pw[i-1] * 10;
112     T[0].init();
113     scanf("%d", &n);
114     ll ans1 = INF, ans2 = -1;
115     for(int i = 1; i <= n; i++) {
116         scanf("%lld", &num);
117         if(i > 1) {
118             ans1 = min(search2(num), ans1);
119             ans2 = max(search1(num), ans2);
120         }
121         insert(num);
122     }
123     printf("%lld %lld
", ans1, ans2);
124     return 0;
125 }

 

以上是关于A .Gaby And Addition (Gym - 101466A + 字典树)的主要内容,如果未能解决你的问题,请参考以下文章

Gym - 101480A_ASCII Addition

592. Fraction Addition and Subtraction 分数加法和减法

[leetcode-592-Fraction Addition and Subtraction]

LC 592. Fraction Addition and Subtraction

E - Addition and Multiplication 2(贪心)

E - Addition and Multiplication 2(贪心)