PURE_LOVE @ 2024-03-19 14:17:43
#include<bits/stdc++.h>
using namespace std;
int n = 0, m = 0;
int mark = 0;
int chazhao(int a,vector<int>arr,int left,int right)
{
int mid = (left + right) / 2;
if (left == right)
{
if (arr[left]==a)
return left+1;
else
return -1;
}
else if(arr[mid] >= a)
{
return chazhao(a, arr, left, mid);
}
else
{
return chazhao(a, arr, mid+1, right);
}
}
int main()
{
scanf("%d%d", &n, &m);
vector<int>arr(n);
vector<int>brr(m);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (int i = 0; i < m; i++)
{
scanf("%d", &brr[i]);
int temp=chazhao(brr[i], arr, 0, n - 1);
printf("%d ", temp);
}
}```c
by tanlihan @ 2024-03-19 14:37:51
@PURE_LOVE 你把vector数组定义成全局它就不会超时了。
by PURE_LOVE @ 2024-03-19 14:52:50
@tanlihan 感谢佬,刚学着用vector还不是很明白,谢谢佬
by PURE_LOVE @ 2024-03-19 15:06:33
@tanlihan 佬 我改完之后re了,是为什么啊
#include<bits/stdc++.h>
using namespace std;
int n = 0, m = 0;
vector<int>arr(1000001);
vector<int>brr(10001);
int mark = 0;
int chazhao(int a,int left,int right)
{
int mid = (left + right) / 2;
if (left>= right)
{
if (arr[left]==a)
return left+1;
else
return -1;
}
else if(arr[mid] >= a)
{
return chazhao(a, left, mid);
}
else
{
return chazhao(a, mid+1, right);
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (int i = 0; i < m; i++)
{
scanf("%d", &brr[i]);
int temp=chazhao(brr[i], 0, n - 1);
printf("%d ", temp);
}
}```c
by tanlihan @ 2024-03-19 15:21:49
@PURE_LOVE
by PURE_LOVE @ 2024-03-19 15:55:38
@tanlihan 爱了爱了