請問為何我用binary search 會TLE?
以下是我的程式碼
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
while (scanf("%d%d",&a,&b)==2)
{
for (int g = 0; g < a; g++)
{
int n[b];
int m[b],num=0;
for (int i = 0; i < b; i++) scanf("%d",&n[i]);
cout<<endl;
for (int i = 0; i < b; i++) scanf("%d",&m[i]);
int first = 0;
for (int i = 0; i < b; i++)
{
int end = b-1,j;
if(n[i]<m[first]) continue;
else if(n[i]==m[first] ) {num++;first++;continue;
}
if(n[i]>m[end])break;
while (end>=first)
{
j = (first + end) / 2;
if(n[i]>m[j]&&n[i]<m[j+1]||(n[i]<m[j]&&n[i]>m[j-1])) {first=j;break;}
if (n[i] < m[j])end--;
else if (n[i] > m[j]) first++;
else { num++; first = j; break; }
}
}
printf("%d\n",num);
}
}
return 0;
}