HDU 4907 task schedule (hash Application)

Source: Internet
Author: User

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;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.