標籤:des style blog color strong io 資料 for
Task schedule
Description
有一台機器,並且給你這台機器的工作表,工作表上有n個任務,機器在ti時間執行第i個任務,1秒即可完成1個任務。
有m個詢問,每個詢問有一個數字q,表示如果在q時間有一個工作表之外的工作要求,請計算何時這個任務才能被執行。
機器總是按照工作表執行,當機器空閑時立即執行工作表之外的工作要求。
Input
輸入的第一行包含一個整數T, 表示一共有T組測試資料。
對於每組測試資料:
第一行是兩個數字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
Output
對於每一個詢問,請計算並輸出該任務何時才能被執行,每個詢問輸出一行。
Sample Input
1
5 5
1 2 3 5 6
1
2
3
4
5
Sample Output
4
4
4
4
7
題目大意:
中文。
解題思路:
設一個詢問為q1,且[q1,qt]之間都有工作,則輸出qt+1。
需要打表,否則TLE。
Code:
1 #include<stdio.h> 2 #include<cstring> 3 #define MAXN 200010 4 using namespace std; 5 int N,M; 6 bool a[MAXN+10]; 7 int b[MAXN+10]; 8 void init() 9 {10 int j,i;11 for (i=1;i<=MAXN;)12 {13 int x=i;14 while (a[x])15 x++;16 for (j=i;j<=x;j++)17 b[j]=x;18 i=j;19 }20 }21 int main()22 {23 int T,tmp;24 scanf("%d",&T);25 while (T--)26 {27 memset(a,0,sizeof(a));28 scanf("%d %d",&N,&M);29 for (int i=1; i<=N; i++)30 {31 scanf("%d",&tmp);32 a[tmp]=1;33 }34 init();35 while (M--)36 {37 scanf("%d",&tmp);38 printf("%d\n",b[tmp]);39 }40 }41 return 0;42 }