【贪心】【字典树】Gym - 101466A - Gaby And Addition
题意:定义一种无进位加法运算,给你n个正整数,问你取出两个数,使得他们加起来和最大/最小是多少。
无进位加法运算,其实是一种位运算,跟最大xor那个套路类似,很容易写出对于每个数字,其对应的最优数字是谁,就对于十叉的字典树,贪心地尽量往使结果更优越的方向走即可。
#include<cstdio>
#include<algorithm>
using namespace std;
int ch[1000010*20][10],sz;
typedef long long ll;
ll pw[20];
void Insert(ll x)
{
int U=0;
for(int i=18;i>=0;--i){
if(!ch[U][x/pw[i]%10ll]){
ch[U][x/pw[i]%10ll]=++sz;
}
U=ch[U][x/pw[i]%10ll];
}
}
ll qmax(ll x){
int U=0;
ll res=0;
for(int i=18;i>=0;--i){
int wei=x/pw[i]%10ll;
int k=9;
for(int j=9-wei;j>=0;--j,--k){
if(ch[U][j]){
res+=(ll)k*pw[i];
wei=j;
goto OUT;
}
}
for(int j=9;j>9-wei;--j,--k){
if(ch[U][j]){
res+=(ll)k*pw[i];
wei=j;
goto OUT;
}
}
OUT:
U=ch[U][wei];
}
return res;
}
ll qmin(ll x){
int U=0;
ll res=0;
for(int i=18;i>=0;--i){
int wei=x/pw[i]%10ll;
int k=0;
for(int j=9-wei+1;j<=9;++j,++k){
if(ch[U][j]){
res+=(ll)k*pw[i];
wei=j;
goto OUT2;
}
}
for(int j=0;j<=9-wei;++j,++k){
if(ch[U][j]){
res+=(ll)k*pw[i];
wei=j;
goto OUT2;
}
}
OUT2:
U=ch[U][wei];
}
return res;
}
int n;
ll a[1000005];
int main(){
//freopen("a.in","r",stdin);
ll ans1=0,ans2=9000000000000000000ll;
pw[0]=1;
for(int i=1;i<=18;++i){
pw[i]=pw[i-1]*10ll;
}
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%I64d",&a[i]);
if(i>1){
ans1=max(ans1,qmax(a[i]));
ans2=min(ans2,qmin(a[i]));
}
Insert(a[i]);
}
printf("%I64d %I64d\n",ans2,ans1);
return 0;
}
【贪心】【字典树】Gym - 101466A - Gaby And Addition的更多相关文章
- CodeFoeces GYM 101466A Gaby And Addition (字典树)
gym 101466A Gaby And Addition 题目分析 题意: 给出n个数,找任意两个数 “相加”,求这个结果的最大值和最小值,注意此处的加法为不进位加法. 思路: 由于给出的数最多有 ...
- 字典树变形 A - Gaby And Addition Gym - 101466A
A - Gaby And Addition Gym - 101466A 这个题目是一个字典树的变形,还是很难想到的. 因为这题目每一位都是独立的,不会进位,这个和01字典树求最大的异或和是不是很像. ...
- ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n, ...
- Ancient Printer HDU - 3460 贪心+字典树
The contest is beginning! While preparing the contest, iSea wanted to print the teams' names separat ...
- 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个数,重定义两个数之间的加法不进位,求这些数中两个数相加的最大值和最小值. 思路: ...
- ACM: Gym 100935F A Poet Computer - 字典树
Gym 100935F A Poet Computer Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d &am ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- NEUOJ711 异星工厂 字典树+贪心
题意:你可以收集两个不相交区间的权值,区间权值是区间异或,问这两个权值和最大是多少 分析:很多有关异或求最大的题都是利用01字典树进行贪心,做这个题的时候我都忘了...最后是看别人代码的时候才想起来这 ...
随机推荐
- 【杂谈】需要mark的一些东西
https://riteme.github.io/blog/2017-10-28/oi-coding-guidelines.html https://www.luogu.org/blog/34238/ ...
- JDK1.8源码TreeMap
基于红黑树(Red-Black tree)的 NavigableMap 实现:键的排序由构造方法决定:自然排序,Comparator排序:非线程安全(仅改变与现有键关联的值不是结构上的修改):线程安全 ...
- 一步一步搭建 oracle 11gR2 rac+dg之grid安装(四)【转】
一步一步在RHEL6.5+VMware Workstation 10上搭建 oracle 11gR2 rac + dg 之grid安装 (四) 转自 一步一步搭建 oracle 11gR2 rac+d ...
- ansible的几点记录
user模块的password加密 ansible中user模块的password需要传入加密数据,可以通过这种方式快速加密: To get a sha512 password hash (rando ...
- NLP里面好的学习资料
别人推荐的网址: http://ruder.io/deep-learning-nlp-best-practices/index.html#wordembeddings
- Linux基础 - crontab
列出当前用户设置的定时任务 crontab -l 编辑定时任务 crontab -e 用法 m h dom mon dow * * * * * command 字段详解: *:any m: minut ...
- java 内部类与控制框架
应用程序控制框架(application framework)就是设计解决某类特殊问题的一个类,或一组类,要运用某个应用程序框架,通常是继承一个类或多个类,并覆盖这些方法.在覆盖的方法中编写代码定制应 ...
- Codeforces 801C Voltage Keepsake(二分枚举+浮点(模板))
题目链接:http://codeforces.com/contest/801/problem/C 题目大意:给你一些电器以及他们的功率,还有一个功率一定的充电器可以给这些电器中的任意一个充电,并且不计 ...
- Ansible之迭代、模板
本节内容: 迭代 模板(JInjia2相关) Jinja2相关 一.迭代 当有需要重复性执行的任务时,可以使用迭代机制.其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语 ...
- easyUI小技巧-纯干货
一.显示分页(pagination:true)情况下,隐藏每页显示的记录条数的那个select(即pageList),下图箭头 方法1:onBeforeLoad:function(param){ ...