洛谷 1571 眼红的Medusa

虽说这道题标签里写明了二分,然而我还是首先想到了map......毕竟map真的是简单好写。

map做法

#include<bits/stdc++.h>
using namespace std;
int n,m;
map<int,bool> v;
int a[],b[];
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) {scanf("%d",&a[i]);}
for(int i=;i<=m;i++) {scanf("%d",&b[i]);v[b[i]]=true;}//建立映射关系
for(int i=;i<=n;i++) if(v[a[i]]) cout<<a[i]<<" ";//如果出现过直接输出
}
return ;

不过言归正传,我还是冲着二分来写这道题的,这个二分思路不难想,就是将第二个数组排序(第一个数组与输出顺序有关,不能排序),然后挨个二分第一个数组里的数是否存在于第二个数组就好了,时间复杂度O(nlogn),n<100000,那就不超时啦。

枚举+二分

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[],b[];
bool binary_search(int x)
{
int l=,r=m;
while (l<=r)
{
int mid=(l+r)>>;
if (b[mid]==a[x])return ;
if (b[mid-]<a[x]&&b[mid+]>a[x])return ;
if (b[mid]>a[x])r=mid;
else l=mid+;
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=m;i++) scanf("%d",&b[i]);
sort(b+,b++m);
for(int i=;i<=n;i++) if(binary_search(i)) printf("%d ",a[i]);
return ;
}
 

最新文章

  1. selenium元素定位篇
  2. 招聘高级.Net工程师
  3. linux 访问tomcat 管理页面时 You are not authorized to view this page 403(真实可用)
  4. Floyd最短路径算法
  5. web-app1--移动端等比例代码
  6. ld: 18 duplicate symbols for architecture i386 .linker command failed with exit code 1 (use -v to see invocation)_
  7. WebSite---前台系统图片验证码心得
  8. POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)
  9. Python_字符串检测与压缩
  10. 关于win7+VS2017环境下的opencv-contirb配置的一个坑
  11. 如何用kaldi做孤立词识别三
  12. Gitlab 备份迁移恢复报错gtar: .: Cannot mkdir: No such file or directory
  13. Mysql乐观锁与悲观锁
  14. mybatis中_parameter使用和常用sql
  15. jQuery检查复选框是否被选
  16. Linux- Showdown 命令详解
  17. laraver框架学习------工厂模型填充测试数据
  18. Codeforces 535C - Tavas and Karafs
  19. gj7 对象引用、可变性和垃圾回收
  20. Java工具类- 跨域工具类

热门文章

  1. STP-8-RSTP中的提议/同意过程
  2. js和jq中常见的各种位置距离之offset()和position()的区别(二)
  3. C# 实现本地化日志管理
  4. nodejs 实践:express 最佳实践(五) connect解析
  5. Unity3d中3D Text对模型的穿透显示
  6. 观察者模式和php实现
  7. The first step in solving any problem is recognizing there is one.
  8. Promise 对象与Generator 函数
  9. 洛谷 P2383 狗哥玩木棒
  10. Intel 快速存储蓝屏