Task schedule
<span style="font-size:18px;"><strong>Description</strong></span>
<span style="font-size:14px;">有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务。
有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行。
机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求。</span>
<span style="font-size:18px;"><strong>Input</strong></span>
<span style="font-size:14px;">输入的第一行包含一个整数T, 表示一共有T组测试数据。</span>
<span style="font-size:18px;">对于每组测试数据:
第一行是两个数字n, m,表示工作表里面有n个任务, 有m个询问;
第二行是n个不同的数字t1, t2, t3....tn,表示机器在ti时间执行第i个任务。
接下来m行,每一行有一个数字q,表示在q时间有一个工作表之外的任务请求。
特别提醒:m个询问之间是无关的。</span>
<span style="font-size:18px;">[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</span>
<span style="font-size:18px;"><strong>
Output</strong></span>
<span style="font-size:14px;">对于每一个询问,请计算并输出该任务何时才能被执行,每个询问输出一行。</span>
<span style="font-size:18px;"><strong>Sample Input</strong></span>
<span style="font-size:14px;">1
5 5
1 2 3 5 6
1
2
3
4
5 </span>
<strong><span style="font-size:18px;">Sample Output</span></strong>
<span style="font-size:14px;">4
4
4
4
7
<span style="font-size:18px;"><strong>
解决代码:</strong></span>
</span><pre name="code" class="cpp">#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;
int f[200010];
int find(int x)
{
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
}
int main()
{
int n,m,t,u;
scanf("%d",&u);
while(u--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=200000;i++)
f[i]=i;
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
f[t]=t+1;
}
int h;
for(int i=1;i<=m;i++)
{
scanf("%d",&h);
printf("%d\n",find(h));
}
}
return 0;
}