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 ...
随机推荐
- GET传值
发送页: <form id="form1" runat="server"> <div> <asp:TextBox ID=</ ...
- P2574 XOR的艺术
题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的01串. 2. 给定一个范围[l,r ...
- [洛谷P1278]单词游戏
题目大意:给一个有$n(n\leqslant16)$个单词的字典,求单词接龙的最大长度 题解:发现$n$很小,可以状压,令$f_{i,j}$表示选的数的状态为$i$,最后一个字母是$j$的最大长度. ...
- BZOJ 1040: [ZJOI2008]骑士 | 在基环外向树上DP
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1040 题解: 我AC了 是自己写的 超开心 的 考虑断一条边 这样如果根节点不选答案一定正确 ...
- BZOJ1095 [ZJOI2007]Hide 捉迷藏 【动态点分治 + 堆】
题目链接 BZOJ1095 题解 传说中的动态点分治,一直不敢碰 今日一会,感觉其实并不艰涩难懂 考虑没有修改,如果不用树形dp的话,就得点分治 对于每个重心,我们会考虑其分治的子树内所有点到它的距离 ...
- How to disable index in innodb
Q: I read from many places that disabling index before loading a data table can significantly speed ...
- wyh的天鹅~vector的使用
链接:https://www.nowcoder.com/acm/contest/93/L来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 262144K,其他语言52428 ...
- Java中中英文对齐输出问题,以及Java中的格式化输出
一 中英文对齐输出问题 问题,要求控制台输出如下: abcefg def 森林 阿狗 其实就是要求对齐输出,各种查找java的格式化输出,然后发现只要一个简单的“\t”就可以实现. 代码如下: Sy ...
- BZOJ 4206: 最大团
4206: 最大团 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 91 Solved: 36[Submit][Status][Discuss] De ...
- 【BZOJ】5010: [Fjoi2017]矩阵填数
[算法]离散化+容斥原理 [题意]给定大矩阵,可以每格都可以任意填1~m,给定n个子矩阵,要求满足子矩阵内的最大值为vi,求方案数. n<=10,h,w<=1w. [题解] 此题重点之一在 ...