TLE求助贴
  • 板块学术版
  • 楼主gao_shou
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/4/13 09:11
  • 上次更新2024/4/13 11:08:54
查看原帖
TLE求助贴
1034531
gao_shou楼主2024/4/13 09:11

题目: 作业:数据查找

题目描述 给定包含 n 个数的数列, q 次询问,每次查询一个正整数 x ,统计数列中小于等于 x 的数字的个数。(如果没有小于等于x的数,则返回-1)

输入格式 输入有 3 行

第一行两个正整数 n 和 q。(n,q≤100000)

第二行 n 个正整数。( int 范围)

第三行 q 个正整数 x 。( int 范围)

输出格式 输出 q 行,每行一个整数。

样例 样例输入

10 3

1 2 3 4 5 6 7 8 9 10

3 11 4

样例输出

3

10

4

代码

#include <bits/stdc++.h>
using namespace std;
int a[100005],maxx;
int bins(int l, int r, int x) {
	int mid;
	while(l<=r) 
	{
		mid=(l+r)/2;
		if(a[mid]==x)
		{
			return mid;
		}
		else if(a[mid]<x) 
		{
			l=mid+1;
		}
		else 
		{
			r=mid-1;
		}
	}
	
}

int main() 
{
	int n,t,x,maxx=0;
	cin>>n>>t;
	for(int i=1;i<=n;i++) 
	{
		cin>>a[i];
		maxx=max(maxx,a[i]);
	}
	sort(a+1,a+n+1);
	for(int i=0;i<t;i++) 
	{
		
		cin>>x;
		int ans=bins(1,n,x);
		if(x>=maxx)
		{
			cout<<maxx<<endl;
		}
		else if(ans!=0)
		{
			cout<<ans<<endl;
		}
		else 
		{
			cout<<-1<<endl;
		}
	}
	return 0;
}

10个TLE真的服了

2024/4/13 09:11
加载中...