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 ...
随机推荐
- Luogu3953 NOIP2017逛公园(最短路+拓扑排序+动态规划)
跑一遍dij根据最短路DAG进行拓扑排序,按拓扑序dp即可.wa了三发感觉非常凉. #include<iostream> #include<cstdio> #include&l ...
- pmap用法小计
By francis_hao Aug 4,2017 pmap-报告进程的内存映射. 概要 pmap [options] pid [...] 描述 pmap命令用来报告一个或多个进程的 ...
- Codeforces 931.D Peculiar apple-tree
D. Peculiar apple-tree time limit per test 1 second memory limit per test 256 megabytes input standa ...
- [codeforces/gym/100431/E]KMP关于border的理解
题目链接:http://codeforces.com/gym/100431/ 考虑到对于一个串β,能cover它的最短的α必然是它的border的某个前缀,或者是这个β本身. 所谓border,就是n ...
- css3 新旧伸缩盒的异同
由于不需要理会IE浏览器,伸缩盒(flexible box)移动端开发中非常好用! 工作中使用APICLOUD开发手机App,老板要求兼容到安卓2.3(新版的需要安卓4.4以上),所以一直使用的是旧版 ...
- 'express' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
新安装了express,但是当查看版本号输入: express -v 时出现如下错误: 网上查找了相关资料才发现express查看版本 的命令是 express -V (即V大写) 再次尝试: 发现同 ...
- 【Sqlite3】SQLITE3使用总结(转)
原文转自 https://www.cnblogs.com/wenxp2006/archive/2012/06/04/2535169.html SQL语句操作 介绍如何用sqlite 执行标准 sql ...
- Python学习笔记 - day2 - PyCharm的基本使用
什么是IDE 开始学习的小白同学,一看到这三个字母应该是懵逼的,那么我们一点一点来说. 既然学习Python语言我们就需要写代码,那么代码写在哪里呢? 在记事本里写 在word文档里写 在sublim ...
- POJ1019 Number Sequence
Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36256 Accepted: 10461 ...
- CTP多点触摸协议【转】
转自:http://blog.chinaunix.net/uid-26403844-id-5063920.html linux kernel 2.6.30开始对多点触摸支持,最近高通要求所有CTP器件 ...