#PS0000179. Chọn chỉ số theo trọng số — phiếu cho trước (Random Pick with Weight — Recorded Draws)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chọn chỉ số theo trọng số — phiếu cho trước (Random Pick with Weight — Recorded Draws)
Random Pick with Weight — Recorded Draws
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given positive integer weights , let . Number tickets from 1 through , assigning the first to index 0, the next to index 1, and so on. For each supplied drawn ticket , identify its owner index. Draws are independent, with replacement. This deterministic adaptation of weighted random sampling supplies the drawn integers in the input; submissions must not generate random numbers.
Input
The first line contains . The second contains weights . The third contains tickets .
Output
Print lines, each giving the corresponding owner index (0 through ).
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Example 1
Input
2 4
1 3
1 2 4 1
Output
0
1
1
0
Explanation
Ticket 1 belongs to index 0; tickets 2,3,4 belong to index 1. Drawing ticket 1 again still gives zero.
Example 2
Input
3 5
2 1 3
1 2 3 4 6
Output
0
0
1
2
2
Explanation
The three ticket intervals are [1,2], [3,3], [4,6].