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终极篇 ---django 模板系统
模板系统 . MV ...
- hdu刷题2
hdu1021 给n,看费波纳列数能否被3整除 算是找规律吧,以后碰到这种题就打打表找找规律吧 #include <stdio.h> int main(void) { int n; whi ...
- Firefox-css-hack
先记下:之后研究.试了一下,新版本FF-32.0效果不错,低版本还没测试. @-moz-document url-prefix() { .container { ... }}
- Python基础 之 数据类型
数据类型 一.运算符 算数运算a = 10 * 10赋值运算a = a + 1 a+=1 布尔值:True 真 False 假 if True: pass while True: pass v = n ...
- 五:Edits Viewer离线日志查看器
离线日志查看器可以将二进制日志翻译成可读的文件(如XML),只有当hadoop集群停止时才能使用.输入文件支持的类型:XML和二进制.输出文件支持类型:XML 二进制 Stats(标准输出?) ...
- SGU 520 Fire in the Country(博弈+搜索)
Description This summer's heat wave and drought unleashed devastating wildfires all across the Earth ...
- 第一章 Windows编程基础(1~4课)
第一课:从main到WinMain 第二课:窗口和消息 第三课:MFC编程 第四课:MFC应用程序框架 概括: Win32的两种编程框架:SDK方式.MFC方式 1. SDK方式:使用WinMain入 ...
- “Hello World!团队”Alpha发布—视频链接+文案+美工
视频链接:http://v.youku.com/v_show/id_XMzEyNjc2MTAyOA==.html?sharefrom=iphone&sharekey=5378037f8b710 ...
- Java中I/O流之轮换流
Java 中的轮换流: 非常有用,可以把一个字节流转换成字符流. inputStreamReader, outputStreamReader Demo_1: import java.io.*; cla ...
- Spring Boot(二)配置分析
回顾一下采用SSM开发项目时,项目中会存在多个配置文件,比如web.xml,配置Spring相关的applicationContext-springmvc.xml, applicationContex ...