codeforces上的代码是开放的,常常就能看到本渣与大神们的差距
比如二分查找。。。
1.在数组中,找出第一个4所在位置
输入:
14 4
1 2 2 3 4 4 4 4 5 6 7 9 9 10
这是本鶸代码。。。。。。。
#include <stdio.h>
int a[1010],n;
int main(){
int i,l,r,mid,b;
scanf("%d%d",&n,&b);
for(i=1;i<=n;i++)scanf("%d",&a[i]);
l=0,r=n;
while(l+1<r){
mid=(l+r)/2;
if(a[mid]<b)l=mid;
else r=mid;//a[mid]>=b
}
if(a[r]==b)printf("%d\n",r);//找数字b出现在最左侧
else printf("NO\n");
return 0;
}
输出:
5
然而大神是这样写的:
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1010],n;
int main(){
int i,b,pos;
scanf("%d%d",&n,&b);
for(i=1;i<=n;i++)scanf("%d",&a[i]);
pos=lower_bound(a+1,a+1+n,b)-a;//找数字b出现在最左侧
if(pos==n+1||a[pos]!=b)printf("NO\n");
else printf("%d\n",pos);//a[pos]==b;
return 0;
}
2.在数组中,找出最后一个4所在位置
输入:
14 4
1 2 2 3 4 4 4 4 5 6 7 9 9 10
这是本鶸代码。。。。。。。
#include <stdio.h>
int a[1010],n;
int main(){
int i,l,r,mid,b;
scanf("%d%d",&n,&b);
for(i=1;i<=n;i++)scanf("%d",&a[i]);
l=1,r=n+1;
while(l+1<r){
mid=(l+r)/2;
if(a[mid]<=b)l=mid;
else r=mid;
}
if(a[l]==b)printf("%d\n",l);//找数字b出现在最右侧
else printf("NO\n");
return 0;
}
然而大神是这样写的:
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1010],n;
int main(){
int i,b,pos;
scanf("%d%d",&n,&b);
for(i=1;i<=n;i++)scanf("%d",&a[i]);
pos=upper_bound(a+1,a+1+n,b)-a;//找数字b出现在最右侧
if(pos==n+1||a[pos-1]!=b)printf("NO\n");
else printf("%d\n",pos-1);//a[pos-1]==b;
return 0;
}
3.用法
头文件:#include <algorithm>
时间复杂度:一次查询O(log n),n为数组长度。
图示:
lower_bound:
功能:查找非递减序列[first,last) 内第一个大于或等于某个元素的位置。
返回值:如果找到,则返回找到元素的地址;否则返回last的地址。(这样不注意的话会越界,小心)
用法:int t=lower_bound(a+l,a+r,key)-a;(a是数组)。
upper_bound:
功能:查找非递减序列[first,last) 内第一个大于某个元素的位置。
返回值:如果找到,则返回找到元素的地址;否则返回last的地址。(同样这样不注意的话会越界,小心)
用法:int t=upper_bound(a+l,a+r,key)-a;(a是数组)。
摘自https://blog.csdn.net/lwgkzl/article/details/82851346
结构体中查找:
把我们需要查找的数封装成一个结构体。然后才可以在结构体重进行查找。即使我们只需要针对某一维进行查找,也需要把整个结构体构造出来。
代码如下:
struct MY{
int a,b;
MY(){}
MY(int a,int b):a(a),b(b){}
bool operator<(const MY m)const{ //定义比较方式,这一步很重要
return a<m.a;
}
};
int main(){
MY m[10];
for(int i=0;i<10;i++){
m[i] = MY(i+1,2*i);
cout<<m[i].a<<" "<<m[i].b<<endl;
}
cout<<"请输入你需要查找的数字a:"<<endl;
int num;
cin>>num;
sort(m,m+10);//进行二分之前需要排序
int aa = lower_bound(m,m+10,MY(num,0)) - m; //需要把我们查找的数封装成一个结构体才能进行查找。
cout<<"查到位置为:"<<aa<<endl;
return 0;
}
这里我只需要查找第一维,并且我对第一维进行了排序,只有有序数列才可以进行二分,然后在查找的时候,把其他维置零即可。但是必须要封装成一个结构体
vector中也是同理:
代码:
struct MY{
int a,b;
MY(){}
MY(int a,int b):a(a),b(b){}
bool operator<(const MY m)const{ //定义比较方式,这一步很重要
return a<m.a;
}
};
int main(){
vector<MY>ve;
for(int i=0;i<10;i++){
ve.push_back(MY(i+1,2*i));
cout<<ve[i].a<<" "<<ve[i].b<<endl;
}
cout<<"请输入你需要查找的数字a:"<<endl;
int num;
cin>>num;
sort(ve.begin(),ve.end());//进行二分之前需要排序
int aa = lower_bound(ve.begin(),ve.end(),MY(num,0)) - ve.begin(); //需要把我们查找的数封装成一个结构体才能进行查找。
cout<<"查到位置为:"<<aa<<endl;
return 0;
}
ve.begin()指向vector的开始,ve.end()指向vector的结尾。
结果如下:
摘自https://blog.csdn.net/codehappy123/article/details/97814162
输入N个字符串后,排个序,同时记录他在未排序之前的编号。所以这需要结构体。然后就是对结构体排序。之后需要在最快时间内找到前缀为s的第一个字符串,需要使用二分中 的lower_bound() 所以我们需要运用重载运算符,重新定义查找规则。
值得注意的是lower_bound()中第三个参数也必须是一个结构体,所以你需要将前缀字符串s封装成结构体才可以查询。
#include <bits/stdc++.h>
using namespace std;
struct node
{
int i;
string s;
bool operator<(const node m)const{
return s<m.s;
}
}hah[1000006];
char ch[1000006];
bool cmp(struct node x,struct node y)
{
return x.s<y.s;
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%s",ch);
hah[i].s=ch;
hah[i].i=i;
}
sort(hah,hah+n,cmp);
while(m--)
{
string c;
int k;
struct node kk;
scanf("%d %s",&k,ch);
c=ch;
kk.s=c;
int tt=lower_bound(hah,hah+n,kk)-hah;
if(hah[tt+k-1].s.find(c)!=0)printf("-1\n");
else printf("%d\n",hah[tt+k-1].i+1);
}
return 0;
}