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算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
随机推荐
- IBM存储降级告警等一些服务器问题/dd/ethtool
1.IBM存储降级告警 一般两种情况 a.端口降级 例如模块16G->8G(IBM储存端口自适应) b.系统在作raid后,有硬盘损坏,降级 黄灯告警 2. dimm error dimm内存插 ...
- 设计模式PHP篇(二)————工厂模式
一个很简单的工厂模式.代码如下: <?php interface Person { public function sex(); } class Man implements Person { ...
- Privoxy
Privoxy 前沿: 这个玩意我以前都没听说过,今天在别人的帮助下试了试,只想说:谁还能阻挡我,我就是要USA....ps:在此感谢每一个帮助我的人 介绍: Privoxy是一款带过滤功能的代理服务 ...
- RAD Studio 10.3 Rio (BCB & Dephi) 发布啦
期盼已久的RAD Studio 10.3 Rio 终于发布了: 下载链接:http://altd.embarcadero.com/download/radstudio/10.3/delphicbui ...
- solr4.2增量索引之同步(修改,删除,新增)--转载
原文地址:http://www.jiancool.com/article/12743229775/;jsessionid=14E9B3F1BB33399799884B5C8F15DDE1 solr增 ...
- 【bzoj4897】[Thu Summer Camp2016]成绩单 区间dp
题目描述 给你一个数列,每次你可以选择连续的一段,付出 $a+b\times 极差^2$ 的代价将其删去,剩余部分拼到一起成为新的数列继续进行此操作.求将原序列全部删去需要的最小总代价是多少. 输入 ...
- [二十三]SpringBoot 之 redis
本文章牵涉到的技术点比较多:spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对以上这些技术点有一定的了解或者也可以先看看这篇文章 ...
- Contest 2
A:辣鸡题.搜索怎么这么难啊.不会啊. B:裸的高斯消元,看起来可以优化到n2. #include<iostream> #include<cstdio> #include< ...
- C++11线程使用总结
std::thread 在 <thread> 头文件中声明,因此使用 std::thread 需包含 <thread> 头文件. <thread> 头文件摘要 &l ...
- 【基础】一个简单的MVC实例及故障排除
Controller: public ActionResult Index() { string setting = "ApplicationServices"; var conn ...