【贪心】【字典树】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字典树进行贪心,做这个题的时候我都忘了...最后是看别人代码的时候才想起来这 ...
随机推荐
- php实现异步请求
PHP开启异步多线程执行脚本 装载自:http://www.cnblogs.com/clphp/p/4913214.html 场景要求 客户端调用服务器a.php接口,需要执行一个长达5s-20s不 ...
- Computer Vision Resources
Computer Vision Resources Softwares Topic Resources References Feature Extraction SIFT [1] [Demo pro ...
- spring-boot-CommandLineRunner
在项目服务启动完成后就去加载一些数据 @Component public class MyStartupRunner1 implements CommandLineRunner { @Override ...
- Hibernate5笔记8--Hibernate事务相关内容
Hibernate事务相关内容: (1) 事务四大特性(简称ACID): (1)原子性(Atomicity) 事务中的全部操作在数据库中是不可分割的,要么全部完成,要么均不执行. (2)一致性(Con ...
- 对于Linux平台下C语言开发中__sync_函数的认识(转)
reference:http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html#Atomic-Builtins A built-i ...
- 全面了解Nginx主要应用场景【转】
前言 本文只针对 Nginx 在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可能介绍的不完整,毕竟只是我个人使用过和了解到过得.所以还请见谅,同时欢迎留言交流 ...
- AdvStringGrid 固定行、列
- AdvStringGrid 标题头
标题头内容: 字体: 标题头高度: 头的对齐方式:
- Python创建ES索引
# pip install elasticsearch from datetime import datetime from elasticsearch import Elasticsearch es ...
- NOIP2015&2016普及组解题报告
NOIP2015普及组题目下载 NOIP2016普及组题目下载 NOIP2015普及组题目: NOIP2018RP++ NOIP2016普及组题目 NOIP2018RP++ T1 金币\((coin. ...