codeforces Round #440 A Search for Pretty Integers【hash/排序】
1 second
256 megabytes
standard input
standard output
You are given two lists of non-zero digits.
Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer?
The first line contains two integers n and m (1 ≤ n, m ≤ 9) — the lengths of the first and the second lists, respectively.
The second line contains n distinct digits a1, a2, ..., an (1 ≤ ai ≤ 9) — the elements of the first list.
The third line contains m distinct digits b1, b2, ..., bm (1 ≤ bi ≤ 9) — the elements of the second list.
Print the smallest pretty integer.
2 3
4 2
5 7 6
25
8 8
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1
In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list.
In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
【题意】:给定2个数组,求一个最小数,满足里面含有2个数组中至少一个数。注意特判俩个数组中含有相等的数的求看!
【分析】:方法一:分别hash一下每个数组的数,出现的标记为1,如果某个数在两个数组同时标记为1,说明在两个数组内都出现过,直接输出该数(如果两个数组出现同一个数字,纵使不是里面最小的,也要单独输出)。还要把两个数组内最小的mina和minb用递归找出来,再分别比较两者,最小放前,稍大置后。
方法二:排序。不用标记,直接升序排序。先判断两数组是否出现同一个数(二层循环枚举一下),否则分别将两数组第一个(最小)数记录,比较一下大小,小的在前大的在后。
【代码】:
#include <bits/stdc++.h> using namespace std;
int main()
{
int n,m,ma=,mb=;
int x,y,a[],b[];//或者不用int,用bool
memset(a,,sizeof(a));
memset(b,,sizeof(b));//hash数组注意清0,否则结果错误
cin>>n>>m;
for(int i=;i<=n;i++)
{
cin>>x;
a[x]=;
ma=min(x,ma);
}
for(int i=;i<=m;i++)
{
cin>>y;
b[y]=;
mb=min(y,mb);
}
for (int i=;i<=;i++)
{
if (a[i] && b[i])
{
cout<<i<<endl;
return ;//真的好用,不能是break,否则还会顺序执行下去,会是不正确的输出
}
}
cout<<min(ma,mb)<<max(ma,mb)<<endl; return ;
}
hash
#include<bits/stdc++.h> using namespace std; int main()
{
int n,m,a[],b[];
while(cin>>n>>m)
{
for(int i=;i<=n;i++)
cin>>a[i];
sort(a+,a+n+); for(int i=;i<=m;i++)
cin>>b[i];
sort(b+,b+m+); for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(a[i]==b[j])
{
cout<<a[i]<<endl;
return ;
}
}
}
int ma=a[];
int mb=b[];
int minx=min(ma,mb);
int maxx=max(ma,mb);
cout<<minx<<maxx<<endl;
}
}
sort
codeforces Round #440 A Search for Pretty Integers【hash/排序】的更多相关文章
- Codeforces Round #440 (Div. 2)【A、B、C、E】
Codeforces Round #440 (Div. 2) codeforces 870 A. Search for Pretty Integers(水题) 题意:给两个数组,求一个最小的数包含两个 ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...
- Codeforces Round #440 (Div. 2) A,B,C
A. Search for Pretty Integers time limit per test 1 second memory limit per test 256 megabytes input ...
- ACM-ICPC (10/15) Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
A. Search for Pretty Integers You are given two lists of non-zero digits. Let's call an integer pret ...
- 【Codeforces Round #440 (Div. 2) A】 Search for Pretty Integers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先枚举一个数字的情况. 再枚举两个数的情况就好. [代码] #include <bits/stdc++.h> #defi ...
- Codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers 高精度比大小,模拟
A. Comparing Two Long Integers 题目连接: http://www.codeforces.com/contest/616/problem/A Description You ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) D. Something with XOR Queries
地址:http://codeforces.com/contest/872/problem/D 题目: D. Something with XOR Queries time limit per test ...
- codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers
题目链接:http://codeforces.com/problemset/problem/616/A 题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小 模拟即可.提供两个版本,数组 ...
- Educational Codeforces Round 5 A. Comparing Two Long Integers
A. Comparing Two Long Integers time limit per test 2 seconds memory limit per test 256 megabytes inp ...
随机推荐
- AJAX基本演示使用
Servlet配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="h ...
- Firefox浏览器浏览自己做的网站需要输入用户名和密码解决
我用最新的Firefox 35.0浏览我制作的网站,就会弹出这个对话框.这是什么原因?
- Visual Source Safe的使用方法
VSS 的全称为 Visual Source Safe .作为 Microsoft Visual Studio 的一名成员,它主要任务就是负责项目文件的管理,几乎可以适用任何软件项目.管理软件开发中各 ...
- BZOJ4592 SHOI2015脑洞治疗仪(线段树)
考虑需要资瓷哪些操作:区间赋值为0:统计区间1的个数:将区间前k个0变为1:询问区间最长全0子串.于是线段树维护区间1的个数.0的个数.最长前缀后缀全0子串即可.稍微困难的是用一个log实现将区间前k ...
- bzoj 1878
莫队乱搞的第一题,(感觉这个算法初学的时候就能想到啊) 总之就是离线,然后扫一遍然后回答,用数组记录状态 但还是有一个地方不太明白 为什么要除siz?这样为什么会优化复杂度呢?? bool cmp(c ...
- 如何使用Photoshop批量扫描保存文档
以笔主手头上的Canon LIDE 100为例 先安装好扫描仪驱动程序,可使用自带驱动光盘或驱动精灵等程序完成. 打开Photoshop程序,以CS5为例,找到扫描仪入口: 点开高级模式进行配置,笔主 ...
- JavaScript的lazyload延迟加载是如何实现的
懒加载技术(简称lazyload)并不是新技术, 它是js程序员对网页性能优化的一种方案.lazyload的核心是按需加载.在大型网站中都有lazyload的身影,例如谷歌的图片搜索页,迅雷首页,淘宝 ...
- MySQL使用笔记(七)排序和限制数据记录查询
By francis_hao Dec 17,2016 排序数据记录查询 排序是指将筛选出符合条件的数据进行有序排放,有升序(ASC(默认))方式和降序(DESC)方式. mysql> se ...
- JavaScript 被忽视的细节
语句/表达式 换个角度理解语句(statemaents)和表达式(expressions):表达式不会改变程序的运行状态,而语句会.还有一种叫做表达式语句,可以理解为表达式和语句的交集,如({a:1} ...
- Patch Windows with SSM on AWS
ec2ssmupdate https://docs.amazonaws.cn/systems-manager/latest/userguide/systems-manager-patch.htmlht ...