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:
- Line up the numbers vertically matching digits places.
- Add together the numbers that share the same place value from right to left.
- 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
6
17 5 11 0 42 99
0 99
7
506823119072235413 991096248449924896 204242310783332529 778958050378192979 384042493592684633 942496553147499866 410043616343857825
52990443860776502 972190360051424498
Note
In the first sample input this is how you get the minimum and the maximum value
这题也是被安排的明明白白 组队训练的时候这题不会做
后面说是字典树 学了2个小时字典树还是没写出来
心态蹦了
现学字典树
#include <bits/stdc++.h>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const LL LLINF = 0x3f3f3f3f3f3f3f3fll;
const int maxn = 1e6 + ;
const int mod = 1e9 + ;
LL t[], a[maxn];
struct trie {
int cnt[maxn * ], tree[maxn * ][], arr[], root, rear;
int newnode() {
cnt[++rear] = ;
mem(tree[rear], );
return rear;
}
void init() {
rear = ;
root = newnode();
}
void add(LL x) {
int now = root, temp;
for (int i = ; i < ; i++) arr[i] = x % , x /= ;
for (int i = ; i >= ; i--) {
temp = arr[i];
if (!tree[now][temp]) tree[now][temp] = newnode();
now = tree[now][temp];
cnt[now]++;
}
}
LL query1(LL x) {
int now = root, maxx, idx;
LL ret = ;
for (int i = ; i < ; i++) arr[i] = x % , x /= ;
for (int i = ; i >= ; i--) {
maxx = -, idx = -;
for (int j = ; j < ; j++)
if (tree[now][j] && (arr[i] + j) % > maxx) maxx = (arr[i] + j) % , idx = j;
ret += t[i] * maxx;
now = tree[now][idx];
}
return ret;
}
LL query2(LL x) {
int now = root, maxx, idx;
LL ret = ;
for (int i = ; i < ; i++) arr[i] = x % , x /= ;
for (int i = ; i >= ; i--) {
maxx = , idx = -;
for (int j = ; j < ; j++)
if (tree[now][j] && (arr[i] + j) % < maxx ) maxx = (arr[i] + j) % , idx = j;
ret += t[i] * maxx;
now = tree[now][idx];
}
return ret;
}
} tr;
int main() {
t[] = ;
for (int i = ; i < ; i++) t[i] = t[i - ] * ;
int n;
sf(n);
LL ans1 = (1LL) << , ans2 = ;
tr.init();
for (int i = ; i < n ; i++) {
scanf("%lld", &a[i]);
if (i) {
ans1 = min(ans1, tr.query2(a[i]));
ans2 = max(ans2, tr.query1(a[i]));
}
tr.add(a[i]);
}
printf("%lld %lld\n", ans1, ans2);
return ;
}
Gaby And Addition Gym - 101466A (初学字典树)的更多相关文章
- 字典树变形 A - Gaby And Addition Gym - 101466A
A - Gaby And Addition Gym - 101466A 这个题目是一个字典树的变形,还是很难想到的. 因为这题目每一位都是独立的,不会进位,这个和01字典树求最大的异或和是不是很像. ...
- CodeFoeces GYM 101466A Gaby And Addition (字典树)
gym 101466A Gaby And Addition 题目分析 题意: 给出n个数,找任意两个数 “相加”,求这个结果的最大值和最小值,注意此处的加法为不进位加法. 思路: 由于给出的数最多有 ...
- A .Gaby And Addition (Gym - 101466A + 字典树)
题目链接:http://codeforces.com/gym/101466/problem/A 题目: 题意: 给你n个数,重定义两个数之间的加法不进位,求这些数中两个数相加的最大值和最小值. 思路: ...
- 【贪心】【字典树】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 ...
- 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算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
随机推荐
- 孤荷凌寒自学python第七十七天开始写Python的第一个爬虫7
孤荷凌寒自学python第七十七天开始写Python的第一个爬虫7 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 今天的学习仍然是在纯粹对docx模 ...
- 对Java对象的认识与理解
今天是我学习编程以来第一次写博客,记下平日学习所得,本来这几日都在学习web框架 但觉得梳理一下之前所学很有必要.毕竟之前学习Java感觉很粗略只是以考试为目的.所以就以<Thinking in ...
- spark操作数据库的几种方法
一.使用jdbcRDD的接口: SparkConf conf = new SparkConf(); conf.setAppName("Simple Application").se ...
- Scala学习笔记之Actor多线程与线程通信的简单例子
题目:通过子线程读取每个文件,并统计单词数,将单词数返回给主线程相加得出总单词数 package review import scala.actors.{Actor, Future} import s ...
- ElasticSearch 2.0以后的改动导致旧的资料和书籍需要订正的部分
id原先是可以通过path指定字段的 "thread": { "_id" : { "path" : "thread_id" ...
- php 连接redis查询数据
class Layoutdemo{ function index(){ $db = new Db(); $id=390; $layout_json = array(); if($info = $db- ...
- wpa_supplicant下行接口浅析
wpa_supplicant通过socket通信机制实现下行接口,与内核进行通信,获取信息或下发命令. 以下摘自http://blog.csdn.net/fxfzz/article/details/6 ...
- Thunder团队第三周 - Scrum会议5
Scrum会议5 小组名称:Thunder 项目名称:i阅app Scrum Master:苗威 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...
- 软件工程课堂作业(十一)——NABC分析
一.团队开发项目:基于Android的重力感应的解锁APP 二.项目特点:区别于一般解锁软件用开机按钮开锁解锁,我们的重力解锁软件根据动作实现解锁,减少了开机按钮的使用频率,提高寿命. 三.NABC分 ...
- WPF+数据库+三层
1.计算类 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...