首页 > 代码库 > hdu 4907 Task schedule(二分)

hdu 4907 Task schedule(二分)

# include <stdio.h>
# include <string.h>
# include <algorithm>
using namespace std;
int vis[200010];
int b[200100];
# define Max 200010
int main()
{
	int t,T,n,m,a,i;
	while(~scanf("%d",&T))
	{
		while(T--)
		{
			scanf("%d%d",&n,&m);
			memset(vis,0,sizeof(vis));
			for(i=0;i<n;i++)
			{
				scanf("%d",&t);
			    vis[t]=1;
			}
			int len=0;
			memset(b,0,sizeof(b));
		    for(i=1;i<=Max;i++)//空余时间
			{
				if(vis[i]==0)
					b[len++]=i;
			}
			while(m--)
			{
				scanf("%d",&a);
				if(!vis[a])
					printf("%d\n",a);
				else
				{
					int l=0;
					int r=len;
					while(l<=r)
					{
						int mid=(l+r)/2;
						if(b[mid]>=a)//空余时间大于任务时间,向下找
							r=mid-1;
						else
							l=mid+1;
					}
					printf("%d\n",b[l]);
				}
			}
		}
	}
	return 0;
}