#BS0000174. Chọn chỉ số theo trọng số bằng vé (Random Pick with Weight)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chọn chỉ số theo trọng số bằng vé (Random Pick with Weight)
Random Pick with Weight — Ticket Mapping
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given positive weights corresponding to zero-based indices through . There are tickets numbered to . Index owns the first tickets, index owns the next tickets, and so forth; each index owns a consecutive ticket interval whose length equals its weight.
For each of the supplied ticket numbers , output the index owning that ticket.
PHOJ Extended adaptation: The original LeetCode 528 API independently generates random samples with probability proportional to weights. This version supplies the tickets and checks the exact ticket-to-index mapping. It does not test a contestant's random-number generator. If tickets are uniformly chosen from to , the probability of obtaining an index is exactly proportional to its weight.
Input
The first line contains integers . The second line contains positive weights . The third line contains ticket numbers .
Output
Print lines. Line contains the zero-based index that owns ticket .
Subtasks
- Subtask 1 (20 points): ; ; for every ; for every .
- Subtask 2 (30 points): ; ; for every ; for every .
- Subtask 3 (50 points): ; ; for every ; for every .
Examples
Input
2 5
1 3
1 4 2 3 1
Output
0
1
1
1
0
Explanation
There are tickets. Index owns ticket , while index owns tickets . Therefore the five given tickets map to indices in that order.