A - Gaby And Addition

Gym - 101466A

这个题目是一个字典树的变形,还是很难想到的。

因为这题目每一位都是独立的,不会进位,这个和01字典树求最大的异或和是不是很像。

知道这个了,就还比较好写了,不过要注意数组越界和超时问题。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <stack>
#include <map>
#include <string>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
ll val[maxn * 20];
int trie[maxn * 20][10], tot;
ll f[20];
void init() {
tot = 1;
memset(trie[0], 0, sizeof(trie[0]));
f[0] = 1;
for (int i = 1; i <= 18; i++) f[i] = f[i - 1] * 10;
}
 
void insert(ll x) {
int u = 0;
// printf("x=%lld\n", x);
for (int i = 18; i >= 0; i--) {
ll v = (x / f[i]) % 10;
// printf("i=%d v=%lld\n", i, v);
if (trie[u][v] == 0) {
memset(trie[tot], 0, sizeof(trie[tot]));
val[tot] = 0;
trie[u][v] = tot++;
}
// printf("u=%d v=%lld\n", u, v);
u = trie[u][v];
}
val[u] = x;
}
 
ll query_max(ll x) {
int u = 0;
for (int i = 18; i >= 0; i--) {
ll v = (x / f[i]) % 10;
bool flag = 0;
for (int j = 9 - v; j >= 0; j--) {
if (trie[u][j]) {
flag = 1;
u = trie[u][j];
break;
}
}
if (flag == 0) {
for (int j = 9; j >= 9 - v + 1; j--) {
if (trie[u][j]) {
u = trie[u][j];
break;
}
}
}
}
return val[u];
}
 
ll query_min(ll x) {
// printf("x=%lld\n", x);
int u = 0;
for (int i = 18; i >= 0; i--) {
ll v = (x / f[i]) % 10;
// printf("i=%d v=%lld\n", i, v);
bool flag = 0;
for (int j = 9 - v + 1; j <= 9; j++) {
// printf("www u=%d v=%lld j=%d\n", u, v,j);
if (trie[u][j]) {
u = trie[u][j];
// printf("j=%d\n", j);
flag = 1;
break;
}
}
if (flag == 0) {
for (int j = 0; j <= 9 - v; j++) {
if (trie[u][j]) {
u = trie[u][j];
break;
}
}
}
}
// printf("val=%lld\n", val[u]);
return val[u];
}
 
ll a[maxn];
 
ll check(ll a, ll b) {
ll ans = 0;
// printf("a=%lld b=%lld\n", a, b);
for (int i = 18; i >= 0; i--) {
ans = ans * 10 + (a / f[i] % 10 + b / f[i] % 10) % 10;
}
return ans;
}
 
int main() {
int n;
init();
scanf("%d", &n);
ll maxs = 0, mins = inf64;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if(i!=1)
{
ll res1 = query_max(a[i]);
ll res2 = query_min(a[i]);
maxs = max(maxs, check(a[i], res1));
mins = min(mins, check(a[i], res2));
}
insert(a[i]);
}
printf("%lld %lld\n", mins, maxs);
return 0;
}

  

字典树变形 A - Gaby And Addition Gym - 101466A的更多相关文章

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

    Gaby is a little baby who loves playing with numbers. Recently she has learned how to add 2 numbers ...

  2. POJ 3764 - The xor-longest Path - [DFS+字典树变形]

    题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...

  3. CH 1602 - The XOR Largest Pair - [字典树变形]

    题目链接:传送门 描述在给定的 $N$ 个整数 $A_1, A_2,\cdots,A_N$ 中选出两个进行xor运算,得到的结果最大是多少? 输入格式第一行一个整数 $N$,第二行 $N$ 个整数 $ ...

  4. HDU 1247 Hat’s Words(字典树变形)

    题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...

  5. hdu 2846 字典树变形

    mark: 题目有字串匹配的过程 有两点 1.为了高效的匹配子串 可以把所有的子串都预处理进去 然后字典树计数就放在最后面 2.在同一个母串处理自串的时候 会有重复的时候 比如abab  这里去重用个 ...

  6. CodeFoeces GYM 101466A Gaby And Addition (字典树)

    gym 101466A Gaby And Addition 题目分析 题意: 给出n个数,找任意两个数 “相加”,求这个结果的最大值和最小值,注意此处的加法为不进位加法. 思路: 由于给出的数最多有 ...

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

    题目链接:http://codeforces.com/gym/101466/problem/A 题目: 题意: 给你n个数,重定义两个数之间的加法不进位,求这些数中两个数相加的最大值和最小值. 思路: ...

  8. 【贪心】【字典树】Gym - 101466A - Gaby And Addition

    题意:定义一种无进位加法运算,给你n个正整数,问你取出两个数,使得他们加起来和最大/最小是多少. 无进位加法运算,其实是一种位运算,跟最大xor那个套路类似,很容易写出对于每个数字,其对应的最优数字是 ...

  9. ACM: Gym 100935F A Poet Computer - 字典树

    Gym 100935F A Poet Computer Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d &am ...

随机推荐

  1. unity3d的键盘和鼠标输入

    一.键盘的输入 •GetKey,GetKeyDown,GetKeyUp三个方法分别获取用户键盘按键的输入 1. GetKey:用户长按按键有效: bool down = Input.GetKeyDow ...

  2. BeanShell计算支付价格

    问题:需要获取支付价格,而支付价格是商品价格*折扣 解决:先将商品价格和折扣获取出来,然后使用BeanShell将两个值相乘,获得最后的支付价格 1.使用json提取器提取商品的价格 2.使用正则表达 ...

  3. sqli-labs通关教程----21~30关

    第二十一关 第二十一关我们正常登陆后看到,uname后面变成了一堆字母 这是经过base64编码之后的样子,所以就照葫芦画瓢,将我payload的uname后面的部分转码成base64,这里可以用正常 ...

  4. Personal Photo Management Application

    Customer Problems & Needs People may take a large number of photos and their phone don't have en ...

  5. Candy Distribution

    Kids like candies, so much that they start beating each other if the candies are not fairly distribu ...

  6. 深圳博客第一篇(Json)

    JSON JSON是纯文本 JSON具有自我描述性 JSON具有层级结构 JSON可通过javascript进行解析 JSON数据可使用Ajax进行传输 JSON对象的取值 var myObj = { ...

  7. 基于netty实现rpc框架-spring boot服务端

    demo地址 https://gitee.com/syher/grave-netty RPC介绍 首先了解一下RPC:远程过程调用.简单点说就是本地应用可以调用远程服务器的接口.那么通过什么方式调用远 ...

  8. docker环境常用命令

    Ubuntu 安装docker及docker-compose 安装: apt-get install docker apt-get install docker-compose 启动docker环境: ...

  9. Unity 游戏框架搭建 2019 (三十二、三十三) 类的命名 & 代码文件命名

    昨天我们完成了第八个示例的第二个 MenuItem 菜单顺序的调整. 我们今天再往下接着调整. 我们来看下接下来的 MenuItem 代码如下: [MenuItem("QFramework/ ...

  10. react: typescript integrate withRouter

    define interface: export interface INav { nav: string } export interface IModuleItem { state?: strin ...