CodeFoeces GYM 101466A Gaby And Addition (字典树)
题目分析
题意:
给出n个数,找任意两个数 “相加”,求这个结果的最大值和最小值,注意此处的加法为不进位加法。
思路:
由于给出的数最多有 1e6 个,且每个数的值最大为 1e18 ,又因为特殊的加法运算,我们自然无法用常规的方法解决
注意到这个加法运算可以分配到每一位上进行运算,而且最大为1e18,十九位数,那么我们就可以用字典树来存储每个数,并进行计算,为了将字典树每个结点的深度和数的位数对应起来,我们可以将每个数都处理为19位数,从高位依次存入字典树,这样一来,就方便我们求最值了。
然后就是求最值的问题,我最初的想法是先将所有数存入字典树,随后枚举字典树的每条链(从根结点到叶子节点的路径),以求出最值,不过TLE了,想来这样写确实时间比较长,最坏的情况下查询用时:5 ^ 18 次,大约3.8e13次运算, 妥妥地TLE。不过我们可以在插入某个数之前,求出这个数和字典树中存在的数“相加”的得到的最值,随后将这个数存入字典树,最后所有数存入字典树后,就可以得到最值了。
总结:
这个题目写了我近半天,确定是字典树后,由于求最值这里对原方法用时估计错误,导致自己一直TLE在第四组样例,后来请教同学才发现问题。
还有就是这个地方求最值的时候不要用递归,在这里递归的效率很低。
代码区
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
#include<fstream>
#include<vector>
#include<stack>
#include <map>
#include <iomanip>
#define bug cout << "**********" << endl
#define show(x,y) cout<<"["<<x<<","<<y<<"] "
//#define LOCAL = 1;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
unsigned const long long inf = (ull);
const int mod = 1e9 + ;
const int Max = 2e7 + ; struct Node
{
int maps[];
}node[Max]; int n,id;
ull num[];
ull max_val, min_val;
char str[], str2[];
char lead[][]{ "","","","","","" ,"","","","",
"","","","","","","",
"",""}; //用于补充前导零,使得数凑成19位数 void insert() //字典树常规插入操作
{
int root = ;
for (int i = ;i < ;i++)
{
if (node[root].maps[str[i] - ''] == ) node[root].maps[str[i] - ''] = ++id;
root = node[root].maps[str[i] - ''];
}
} ull dfs(int mode) //mode == 1代表求最大值
{
ull val = ;
int root = ;
for (int i = ; i <= ;i++) //最大19位数
{
int id = -;
ull t = ; //记录这个位置上的最大(小)值
if (mode == ) t = ; for (int j = ;j <= ;j++)
{
if (mode && node[root].maps[j] && (j + str[i] - '') % >= t)
{
id = j;t = (j + str[i] - '') % ;
}
else if (!mode && node[root].maps[j] && (j + str[i] - '') % <= t)
{
id = j;t = (j + str[i] - '') % ;
}
}
val += num[i] * t;
root = node[root].maps[id];
}
return val;
} int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
num[] = ;
for (int i = ;i >= ;i--)
{
num[i] = num[i + ] * ; //预处理出1 ~ 1e18
} id = ; //控制字典树结点数s
min_val = inf;
max_val = ; scanf("%d", &n);
for (int i = ;i <= n;i++)
{
scanf("%s", str2);
int len = strlen(str2);
strcpy(str, lead[ - len]);
strcat(str, str2); //将数补充为19位数 if (i != ) //找最值的前提:至少有两个以上的数
{
max_val = max(max_val, dfs());
min_val = min(min_val, dfs());
}
insert();
}
if (min_val == inf) min_val = ;
printf("%llu %llu\n", min_val, max_val);
return ;
} /*
附上一组样例,如果 wrong on test 3 的话,可以用一下,这个是数极限大的情况
input
7
1000000000000000000
1000000000000000000
999999999999999999
1000000000000000000
1000000000000000000
1000000000000000000
1234567890987654321 output
1123456789876543210 2234567890987654321
*/
gym 101466A Gaby And Addition (字典树)
CodeFoeces GYM 101466A Gaby And Addition (字典树)的更多相关文章
- 【贪心】【字典树】Gym - 101466A - Gaby And Addition
题意:定义一种无进位加法运算,给你n个正整数,问你取出两个数,使得他们加起来和最大/最小是多少. 无进位加法运算,其实是一种位运算,跟最大xor那个套路类似,很容易写出对于每个数字,其对应的最优数字是 ...
- ACM: Gym 100935F A Poet Computer - 字典树
Gym 100935F A Poet Computer Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d &am ...
- 字典树变形 A - Gaby And Addition Gym - 101466A
A - Gaby And Addition Gym - 101466A 这个题目是一个字典树的变形,还是很难想到的. 因为这题目每一位都是独立的,不会进位,这个和01字典树求最大的异或和是不是很像. ...
- Gaby And Addition Gym - 101466A (初学字典树)
Gaby is a little baby who loves playing with numbers. Recently she has learned how to add 2 numbers ...
- A .Gaby And Addition (Gym - 101466A + 字典树)
题目链接:http://codeforces.com/gym/101466/problem/A 题目: 题意: 给你n个数,重定义两个数之间的加法不进位,求这些数中两个数相加的最大值和最小值. 思路: ...
- codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)
题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...
- stl应用(map)或字典树(有点东西)
M - Violet Snow Gym - 101350M Every year, an elephant qualifies to the Arab Collegiate Programming C ...
- Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点
题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
随机推荐
- A. Sea Battle
A. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- JavaWeb-SpringBoot(抖音)_二、服务器间通讯
JavaWeb-SpringBoot(抖音)_一.抖音项目制作 传送门 JavaWeb-SpringBoot(抖音)_二.服务器间通讯 传送门 JavaWeb-SpringBoot(抖音)_三.抖音项 ...
- (十三)C语言之break、continue
- VS Code 调试 Golang 出现 Failed to continue: Check the debug console for details
VS Code断点调试Golang时候,弹出提示:Failed to continue: Check the debug console for details 点击Open launch.json, ...
- CentOS6.5 64位下装Docker
试装,仅仅是做个记录. [root@localhost ~]# sudo tee /etc/yum.repos.d/docker.repo <<-'EOF' > ; > [do ...
- 溢出overflow: hidden
如果要防止内容把div容器或者表格撑大,可以在CSS中设置一.overflow: hidden; 表示如果内容超出容器大小,就把超出部分隐藏(相当于切掉)二.overflow: scroll; 这个表 ...
- MyBatis Mapper Demo
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- 五十四:WTForms表单验证之自定义表单验证器
如果想要对表单中的某个字段进行自定义验证,则需要对这个字段进行单独的验证1.定义一个方法,命名规则为:validate_字段名(self, filed)2.在方法中,使用filed.data获取字段的 ...
- AES256位加密
目录 1. 算法简介 2. 算法流程 2.1 扩展密钥 2.2 轮密钥加 2.3 字节代替 2.4 行位移 2.5 列混淆 3. 总结 附录A 运算示例 1.算法简介高级加密标准(英 ...
- jdk1.8-LinkedList源码分析
一:类的继承关系 我们看下类的继承关系 ) ) )) { ; i > index; i--) //当前记录结点等于上一节点 x = x.prev; //返回索引为index的点! return ...