对于每组測试数据:
第一行是两个数字n, m,表示工作表里面有n个任务, 有m个询问。
第二行是n个不同的数字t1, t2, t3....tn,表示机器在ti时间运行第i个任务。
接下来m行,每一行有一个数字q,表示在q时间有一个工作表之外的任务请求。
特别提醒:m个询问之间是无关的。
[Technical Specification]
1. T <= 50
2. 1 <= n, m <= 10^5
3. 1 <= ti <= 2*10^5, 1 <= i <= n
4. 1 <= q <= 2*10^5
1 5 5 1 2 3 5 6 1 2 3 4 5
4 4 4 4 7
一開始想的是假设当前时间没有被占用,时间输出当前时间,假设被占用,就向后查找。知道找到空暇时间为止,但这是超时的.....每次询问都要向后查找,并且询问的次数又那么多,肯定不行。后来打表预处理一下。把每一个时间的任务的空暇时间预先处理。这样查询的时候就O(1)了。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=1e5+5;
bool hash[maxn*2];
int f[maxn*2];
int T,n,m,ti,q;
void prepare()
{
int cur=maxn*2;//始终是空暇时间
for(int i=maxn*2;i>=1;i--)
{
if(hash[i]==0)
cur=i;
f[i]=cur;
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
memset(hash,0,sizeof(hash));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&ti);
hash[ti]=1;
}
prepare();
for(int i=1;i<=m;i++)
{
scanf("%d",&q);
printf("%d\n",f[q]);
}
}
return 0;
}
[BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)
原文:http://www.cnblogs.com/jhcelue/p/6881885.html