Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4907
Task scheduletime limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 554 accepted submission (s): 275
Problem description has a machine and provides you with a worksheet for this machine. There are n tasks on the worksheet. The machine executes the I-th task at TI time and can complete one task in one second.
There are m inquiries, and each query has a number Q, indicating that if there is a task request beyond the worksheet in the Q time, calculate when the task can be executed.
The machine always runs according to the worksheet. when the machine is idle, it immediately executes the task requests outside the worksheet. The first line of input contains an integer T, indicating a total of T groups of test data.
For each group of test data:
The first row contains two numbers, N and M, indicating that there are n tasks and M inquiries in the worksheet;
The second row contains n different numbers T1, T2, T3.... TN, indicating that the machine executes the I-th task at the Ti time.
In the next m row, each row has a number Q, indicating that there is a task request out of the worksheet at Q time.
Note: m queries are irrelevant.
[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: For each query, calculate and output the time when the task can be executed. Each query outputs a row.
Sample Input
15 51 2 3 5 612345
Sample output
44447
Sourcebestcoder round #3 recommendwe have carefully selected several similar problems for you: 4910 4909 4908 4906 4904
Please press http://blog.163.com/?email protected]/blog/static/111352259201171111938208/
Hash usage; direct search will time out!
The Code is as follows:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define MAXN 200017int hash[MAXN], te[MAXN];int main(){int t;int n, m;int ti, tt;int i;scanf("%d",&t);while(t--){memset(hash,0,sizeof(hash));scanf("%d%d",&n,&m);for(i = 0; i < n; i++){scanf("%d",&tt);hash[tt]++;}int x = MAXN+1;for(i = MAXN; i > 0; i--){if(hash[i]){te[i] = x;}else{te[i] = i;x = i;}}for(i = 0; i < m; i++){scanf("%d",&ti);printf("%d\n",te[ti]);}}return 0;}