codevs 1230 元素查找 二分
来源:互联网 发布:狼人杀炸房软件 编辑:程序博客网 时间:2024/06/04 18:25
题目:
http://codevs.cn/problem/1230/
为什么做这一道题呢?
因为网上说只有10%的程序员可以写出二分查找
虽然过了,但也不能说我的代码一定正确;
吐槽:这道题用STL比手打要快=_=
第一个:sort+lower_bound;
第二个:手写归并+lower_bound;
第三个:手写归并+手写二分;
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=1000001 + 55;int a[MAXN],b[MAXN];int n,m;void merge_sort(int l,int r){ if(l==r) return; int mid=(l+r)>>1; merge_sort(l,mid),merge_sort(mid+1,r); int i=l,j=mid+1,k=l; while(i<=mid && j<=r) if(a[i]>a[j]) b[k++]=a[j++]; else b[k++]=a[i++]; while(i<=mid) b[k++]=a[i++]; while(j<=r) b[k++]=a[j++]; for(int i=l;i<=r;i++) a[i]=b[i]; return; }void solve(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); merge_sort(1,n); int x; for(int i=1;i<=m;i++) { scanf("%d",&x); int l=1,r=n+1; while(r - l > 1) { int mid=(l+r)>>1; if(a[mid]>x) r=mid; else l=mid; } if(x==a[l]) printf("YES\n"); else printf("NO\n"); } return;}int main(){ solve(); return 0;}
阅读全文
1 0
- codevs 1230 元素查找 二分
- CODEVS 1230 元素查找
- CODEVS 1230元素查找
- CODEVS 1230 元素查找
- 【codevs 1230】元素查找
- CODEVS 1230 元素查找
- CODEVS 1230 元素查找
- 【CODEVS 1230】元素查找 哈希表
- codevs 1230 元素查找(hash)
- [Codevs 1230]元素查找(手写哈希表)
- 【codevs 1230】元素查找 splay……
- codevs 1230 元素查找(hash)
- codevs 1230_元素查找_hash
- 【CODE[VS]】1230 元素查找 二分
- [二分]1159: 查找元素
- 二分查找目标元素索引
- 顺序元素的二分查找
- 关于排序之二分查找特定元素
- WebService(1)——使用JDK开发WebService
- MySQL的数据类型
- android databinding 常用语法
- Android下拉选择控件
- mysql共享iscsi
- codevs 1230 元素查找 二分
- LightOJ
- linux获取线程ID
- npm 命令
- java如何实现线程间通信
- linux 命令 jar 命令
- 改进排序算法:快速排序(对冒泡排序的改进)
- PHP环境搭建:
- Effective C++ 第二章 理解&总结