Chào mọi người, chả là mình đang học giải thuật trên coursera có điều Anh Văn mình gà quá, gặp cái đề này, dù đã cố gắng google dịch nhưng vẫn không hiểu hết ý nghĩa của nó, post lên đây ae giải thích hộ mình cái đề nó muốn mình làm gì với, cám ơn mọi người.
Bài 1:
- Problem Introduction:
In this problem you will simulate a program that processes a list of jobs in parallel. Operating systems such as Linux, MacOS or Windows all have special programs in them called schedulers which do exactly this with the programs on your computer. - Problem Description:
Task. You have a program which is parallelized and uses 𝑛 independent threads to process the given list
of 𝑚 jobs. Threads take jobs in the order they are given in the input. If there is a free thread,
it immediately takes the next job from the list. If a thread has started processing a job, it doesn’t
interrupt or stop until it finishes processing the job. If several threads try to take jobs from the list
simultaneously, the thread with smaller index takes the job. For each job you know exactly how long
will it take any thread to process this job, and this time is the same for all the threads. You need to
determine for each job which thread will process it and when will it start processing.
Input Format. The first line of the input contains integers 𝑛 and 𝑚.
The second line contains 𝑚 integers 𝑡𝑖 — the times in seconds it takes any thread to process 𝑖-th job.
The times are given in the same order as they are in the list from which threads take jobs.
Threads are indexed starting from 0.
Constraints. 1 ≤ 𝑛 ≤ 105; 1 ≤ 𝑚 ≤ 105; 0 ≤ 𝑡𝑖 ≤ 109.
Output Format. Output exactly 𝑚 lines. 𝑖-th line (0-based index is used) should contain two spaceseparated integers — the 0-based index of the thread which will process the 𝑖-th job and the time in
seconds when it will start processing that job.
Time Limits. C: 1 sec, C++: 1 sec, Java: 4 sec, Python: 6 sec. C#: 1.5 sec, Haskell: 2 sec, JavaScript:
6 sec, Ruby: 6 sec, Scala: 6 sec.
Memory Limit. 512Mb.
Sample 1.
Input:
2 5
1 2 3 4 5
Output:
0 0
1 0
0 1
1 2
0 4
Explanation:
- The two threads try to simultaneously take jobs from the list, so thread with index 0 actually
takes the first job and starts working on it at the moment 0. - The thread with index 1 takes the second job and starts working on it also at the moment 0.
- After 1 second, thread 0 is done with the first job and takes the third job from the list, and starts
processing it immediately at time 1. - One second later, thread 1 is done with the second job and takes the fourth job from the list, and
starts processing it immediately at time 2.
5 - Finally, after 2 more seconds, thread 0 is done with the third job and takes the fifth job from the
list, and starts processing it immediately at time 4.
Sample 2.
Input:
4 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Output:
0 0
1 0
2 0
3 0
0 1
1 1
2 1
3 1
0 2
1 2
2 2
3 2
0 3
1 3
2 3
3 3
0 4
1 4
2 4
3 4
Explanation:
Jobs are taken by 4 threads in packs of 4, processed in 1 second, and then the next pack comes. This
happens 5 times starting at moments 0, 1, 2, 3 and 4. After that all the 5×4 = 20 jobs are processed.
Bài 2:
Problem Introduction
In this problem, your goal is to simulate a sequence of merge operations with tables in a database.
Problem Description
Task. There are 𝑛 tables stored in some database. The tables are numbered from 1 to 𝑛. All tables share
the same set of columns. Each table contains either several rows with real data or a symbolic link to
another table. Initially, all tables contain data, and 𝑖-th table has 𝑟𝑖 rows. You need to perform 𝑚 of
the following operations:
- Consider table number 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖. Traverse the path of symbolic links to get to the data. That
is, while 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖 contains a symbolic link instead of real data do 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖 ← symlink(𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖) - Consider the table number 𝑠𝑜𝑢𝑟𝑐𝑒𝑖 and traverse the path of symbolic links from it in the same
manner as for 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖. - Now, 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖 and 𝑠𝑜𝑢𝑟𝑐𝑒𝑖 are the numbers of two tables with real data. If 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖 ̸=
𝑠𝑜𝑢𝑟𝑐𝑒𝑖, copy all the rows from table 𝑠𝑜𝑢𝑟𝑐𝑒𝑖 to table 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖, then clear the table 𝑠𝑜𝑢𝑟𝑐𝑒𝑖
and instead of real data put a symbolic link to 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖 into it. - Print the maximum size among all 𝑛 tables (recall that size is the number of rows in the table).
If the table contains only a symbolic link, its size is considered to be 0.
See examples and explanations for further clarifications.
Input Format. The first line of the input contains two integers 𝑛 and 𝑚 — the number of tables in the
database and the number of merge queries to perform, respectively.
The second line of the input contains 𝑛 integers 𝑟𝑖 — the number of rows in the 𝑖-th table.
Then follow 𝑚 lines describing merge queries. Each of them contains two integers 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖 and
𝑠𝑜𝑢𝑟𝑐𝑒𝑖 — the numbers of the tables to merge.
Constraints. 1 ≤ 𝑛,𝑚 ≤ 100 000; 0 ≤ 𝑟𝑖 ≤ 10 000; 1 ≤ 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖, 𝑠𝑜𝑢𝑟𝑐𝑒𝑖 ≤ 𝑛.
Output Format. For each query print a line containing a single integer — the maximum of the sizes of all
tables (in terms of the number of rows) after the corresponding operation.
Time Limits. C: 2 sec, C++: 2 sec, Java: 14 sec, Python: 6 sec. C#: 3 sec, Haskell: 4 sec, JavaScript: 6
sec, Ruby: 6 sec, Scala: 14 sec.
Memory Limit. 512Mb.
7
Sample 1.
Input:
5 5
1 1 1 1 1
3 5
2 4
1 4
5 4
5 3
Output:
2
2
3
5
5
Explanation:
In this sample, all the tables initially have exactly 1 row of data. Consider the merging operations: - All the data from the table 5 is copied to table number 3. Table 5 now contains only a symbolic
link to table 3, while table 3 has 2 rows. 2 becomes the new maximum size. - 2 and 4 are merged in the same way as 3 and 5.
- We are trying to merge 1 and 4, but 4 has a symbolic link pointing to 2, so we actually copy
all the data from the table number 2 to the table number 1, clear the table number 2 and put a
symbolic link to the table number 1 in it. Table 1 now has 3 rows of data, and 3 becomes the new
maximum size. - Traversing the path of symbolic links from 4 we have 4 → 2 → 1, and the path from 5 is 5 → 3.
So we are actually merging tables 3 and 1. We copy all the rows from the table number 1 into
the table number 3, and now the table number 3 has 5 rows of data, which is the new maximum. - All tables now directly or indirectly point to table 3, so all other merges won’t change anything.
Sample 2.
Input:
6 4
10 0 5 0 3 3
6 6
6 5
5 4
4 3
Output:
10
10
10
11
Explanation:
In this example tables have different sizes. Let us consider the operations: - Merging the table number 6 with itself doesn’t change anything, and the maximum size is 10
(table number 1).
8 - After merging the table number 5 into the table number 6, the table number 5 is cleared and has
size 0, while the table number 6 has size 6. Still, the maximum size is 10. - By merging the table number 4 into the table number 5, we actually merge the table number 4
into the table number 6 (table 5 now contains just a symbolic link to table 6), so the table number
4 is cleared and has size 0, while the table number 6 has size 6. Still, the maximum size is 10. - By merging the table number 3 into the table number 4, we actually merge the table number 3
into the table number 6 (table 4 now contains just a symbolic link to table 6), so the table number
3 is cleared and has size 0, while the table number 6 has size 11, which is the new maximum size.