洛谷 1571 眼红的Medusa
2024-08-29 17:44:39
洛谷 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 ;
}
最新文章
- selenium元素定位篇
- 招聘高级.Net工程师
- linux 访问tomcat 管理页面时 You are not authorized to view this page 403(真实可用)
- Floyd最短路径算法
- web-app1--移动端等比例代码
- ld: 18 duplicate symbols for architecture i386 .linker command failed with exit code 1 (use -v to see invocation)_
- WebSite---前台系统图片验证码心得
- POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)
- Python_字符串检测与压缩
- 关于win7+VS2017环境下的opencv-contirb配置的一个坑
- 如何用kaldi做孤立词识别三
- Gitlab 备份迁移恢复报错gtar: .: Cannot mkdir: No such file or directory
- Mysql乐观锁与悲观锁
- mybatis中_parameter使用和常用sql
- jQuery检查复选框是否被选
- Linux- Showdown 命令详解
- laraver框架学习------工厂模型填充测试数据
- Codeforces 535C - Tavas and Karafs
- gj7 对象引用、可变性和垃圾回收
- Java工具类- 跨域工具类
热门文章
- STP-8-RSTP中的提议/同意过程
- js和jq中常见的各种位置距离之offset()和position()的区别(二)
- C# 实现本地化日志管理
- nodejs 实践:express 最佳实践(五) connect解析
- Unity3d中3D Text对模型的穿透显示
- 观察者模式和php实现
- The first step in solving any problem is recognizing there is one.
- Promise 对象与Generator 函数
- 洛谷 P2383 狗哥玩木棒
- Intel 快速存储蓝屏