【贪心】【字典树】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字典树进行贪心,做这个题的时候我都忘了...最后是看别人代码的时候才想起来这 ...
随机推荐
- solr简介——(九)
下载: http://archive.apache.org/dist/lucene/solr/ 1.什么是solr Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lu ...
- raw数据类型
Oracle中用于保存位串的数据类型是RAW,LONG RAW(推荐使用BLOB). RAW,类似于CHAR,声明方式RAW(L),L为长度,以字节为单位,作为数据库列最大2000,作为变量最大327 ...
- FPGA quartus开发中常见的错误处理
1.Warning: An incorrect timescale is selected for the Verilog Output (.VO) file of this PLL design. ...
- ps查看CPU和内存占用前10的进程
内存增序 ps aux --sort rss 内存减序 ps aux --sort -rss cpu增序 ps auxw --sort=%cpu cpu减序 ps auxw --sort=-%cpu ...
- Android性能测试工具之APT
1.APT工具简介: APT是一个eclipse插件,可以实时监控Android手机上多个应用的CPU.内存数据曲线,并保存数据:另外还支持自动获取内存快照.PMAP文件分析等,方便开发人员自测或者测 ...
- Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.
在设置请求头的时候报这个Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPE ...
- 签名DLL
签名DLL 首先需要一个密钥文件,后缀为.snk 密钥文件使用sn.exe 创建: sn.exe /k MySingInKey.snk sn.exe 工具的具体使用,可以通过 sn.exe /h 或 ...
- post提交数据的四种编码方式
这里总结下post提交数据的四种方式. 参考文章: https://www.jianshu.com/p/3c3157669b64
- CI框架整合UEditor编辑器上传功能
最近项目中要使用到富文本编辑器,选用了功能强大的UEditor,接下来就来讲讲UEditor编辑器的上传功能整合. 本文UEditor版本:ueditor1_4_3_utf8_php版本 第一步:部署 ...
- codeforces 603 A
题目大意:给你一个0,1串, 你可以反转一段连续的区间,问你最长的合法子串是多长, 合法字串相邻的两个不能相同. 思路:dp[ i ][ k ][ j ] 表示到第 i 个字符, 处于k这种状态, k ...