A .Gaby And Addition (Gym - 101466A + 字典树)
题目链接:http://codeforces.com/gym/101466/problem/A
题目:



题意:
给你n个数,重定义两个数之间的加法不进位,求这些数中两个数相加的最大值和最小值。
思路:
字典树。我们首先将前i-1为放入字典树中,然后在查询第i位时,我们去字典树中查询,对每一位进行寻找,找到满足题意的当前位的最大值和最小值,然后继续更新下一位,最后维护总的最大值和最小值即可。
代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define lowbit(x) x&(-x)
#define bug printf("*********\n");
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define FIN freopen("D://code//in.txt", "r", stdin);
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; int n;
int le,root;
int arr[];
ll num, pw[]; struct node{
int nxt[];
void init(){
for(int i = ; i < ; i++) nxt[i] = -;
}
}T[*maxn]; void insert(ll x){
int now = root;
for(int i = ; i <= ; i++) {
arr[i] = x % ;
x /= ;
}
for(int i = ;i >= ; i--){
int num = arr[i];
if(T[now].nxt[num] == -){
T[le].init();
T[now].nxt[num] = le++;
}
now = T[now].nxt[num];
}
} ll search1(ll x){
int now = root, mx, idx;
ll res = ;
for(int i = ; i <= ; i++) {
arr[i] = x % ;
x /= ;
}
for(int i = ; i >= ; i--) {
mx = -, idx = -;
for(int j = ; j < ; j++) {
if(T[now].nxt[j] != - && (j + arr[i]) % > mx) {
mx = (j + arr[i]) % ;
idx = j;
}
}
now = T[now].nxt[idx];
res = res + mx * pw[i];
}
return res;
} ll search2(ll x){
int now = root, mx, idx;
ll res = ;
for(int i = ; i <= ; i++) {
arr[i] = x % ;
x /= ;
}
for(int i = ; i >= ; i--) {
mx = , idx = -;
for(int j = ; j < ; j++) {
if(T[now].nxt[j] != - && (j + arr[i]) % < mx) {
mx = (j + arr[i]) % ;
idx = j;
}
}
now = T[now].nxt[idx];
res = res + mx * pw[i];
}
return res;
} int main() {
le = ;
pw[] = ;
for(int i = ; i <= ; i++) pw[i] = pw[i-] * ;
T[].init();
scanf("%d", &n);
ll ans1 = INF, ans2 = -;
for(int i = ; i <= n; i++) {
scanf("%lld", &num);
if(i > ) {
ans1 = min(search2(num), ans1);
ans2 = max(search1(num), ans2);
}
insert(num);
}
printf("%lld %lld\n", ans1, ans2);
return ;
}
A .Gaby And Addition (Gym - 101466A + 字典树)的更多相关文章
- 字典树变形 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 ...
- CodeFoeces GYM 101466A Gaby And Addition (字典树)
gym 101466A Gaby And Addition 题目分析 题意: 给出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算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
随机推荐
- CodeForces Round #527 (Div3) D1. Great Vova Wall (Version 1)
http://codeforces.com/contest/1092/problem/D1 Vova's family is building the Great Vova Wall (named b ...
- webpack打包多html开发案例新
闲来无事在原来简单打包案例的基础上,参考vue-cli的打包代码,改为多文件打包. 区别于上篇文章<webpack打包多html开发案例>,此次打包根据开发的不同环节进行打包,也就是有开发 ...
- CentOS7 修改yum源为阿里云
1,登陆root帐号 2,cd /etc/yum.repo.d 3,mv CentOS-Base.repo CentOS-Base.repo.bak4,wget http://mirrors.aliy ...
- MachineLearning ---- lesson 2 Linear Regression with One Variable
Linear Regression with One Variable model Representation 以上篇博文中的房价预测为例,从图中依次来看,m表示训练集的大小,此处即房价样本数量:x ...
- solrCloud源码分析之CloudSolrClient
CloudSolrClient是solrj提供的客户端与solrCloud交互的类.该类的实例与zookeeper进行通信来确定solrCloud collections中的solr endpoint ...
- 「美团 CodeM 资格赛」跳格子
题目描述 nnn 个格子排成一列,一开始,你在第一个格子,目标为跳到第 n 个格子.在每个格子 i 里面你可以做出两个选择: 选择「a」:向前跳 ai 步. 选择「b」:向前跳 bi 步. 把每步 ...
- What Is The Promiscuous Mode
What Is The Promiscuous Mode? Some Network Interface Cards (NICs) may not allow network traffic afte ...
- TCP的拥塞控制 (一)
拥塞控制不同于流量控制,拥塞控制是在拥塞发生时,发送方根据一定的反馈,主动调节自己的发送速率,以防止拥塞恶化的行为. 1. 网络拥塞 路由器是网络中的关键组件,其内部有一定量的缓冲区,用于缓存来不 ...
- Codeforces711
A ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on th ...
- Python new() 方法
什么是new方法 __new__方法接受的参数虽然也是和__init__一样,但__init__是在类实例创建之后调用,而 __new__方法正是创建这个类实例的方法. __new__()是在新式类中 ...