C2. Exam in BerSU (hard version)
The only difference between easy and hard versions is constraints.
If you write a solution in Python, then prefer to send it in PyPy to speed up execution time.
A session has begun at Beland State University. Many students are taking exams.
Polygraph Poligrafovich is going to examine a group of n students. Students will take the exam one-by-one in order from 1-th to n-th. Rules of the exam are following:
The i-th student randomly chooses a ticket.
if this ticket is too hard to the student, he doesn’t answer and goes home immediately (this process is so fast that it’s considered no time elapses). This student fails the exam.
if the student finds the ticket easy, he spends exactly ti minutes to pass the exam. After it, he immediately gets a mark and goes home.
Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student.
The duration of the whole exam for all students is M minutes (maxti≤M), so students at the end of the list have a greater possibility to run out of time to pass the exam.
For each student i, you should count the minimum possible number of students who need to fail the exam so the i-th student has enough time to pass the exam.
For each student i, find the answer independently. That is, if when finding the answer for the student i1 some student j should leave, then while finding the answer for i2 (i2>i1) the student j student does not have to go home.
Input
The first line of the input contains two integers n and M (1≤n≤2⋅105, 1≤M≤2⋅107) — the number of students and the total duration of the exam in minutes, respectively.
The second line of the input contains n integers ti (1≤ti≤100) — time in minutes that i-th student spends to answer to a ticket.
It’s guaranteed that all values of ti are not greater than M.
Output
Print n numbers: the i-th number must be equal to the minimum number of students who have to leave the exam in order to i-th student has enough time to pass the exam.
Examples
input
7 15
1 2 3 4 5 6 7
output
0 0 0 0 0 2 3
input
5 100
80 40 40 40 60
output
0 1 1 2 3
Note
The explanation for the example 1.
Please note that the sum of the first five exam times does not exceed M=15 (the sum is 1+2+3+4+5=15). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are 0.
In order for the 6-th student to pass the exam, it is necessary that at least 2 students must fail it before (for example, the 3-rd and 4-th, then the 6-th will finish its exam in 1+2+5+6=14 minutes, which does not exceed M).
In order for the 7-th student to pass the exam, it is necessary that at least 3 students must fail it before (for example, the 2-nd, 5-th and 6-th, then the 7-th will finish its exam in 1+3+4+7=15 minutes, which does not exceed M).
Solution
有n个人要考试,每个人通过考试需要花费a[i]分钟,如果某个不考试(很明显会挂科)则不需要时间,如果排在后面的人,前面所有人加上他自己的考试总时间超过m则直接挂科。问第i个人考试的时候,为了确保他能够通过考试,前面至少要有多少人挂科(腾出时间给他)。先算出到目前为止所有人考试所需时间,如果比m小则不需要有人挂科,否则从时间需要大的往小遍历,因为时间只有1~100,所以复杂度并不高。用桶记录之前所有人中需要时间为a[i]的考生为多少个。
Code
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 2e5+5;
typedef long long ll;
int n,m,tong[1000];
ll a[maxn],ans,sum[maxn],res;
int main() {
cin >> n >> m;
for(int i=1;i<=n;i++) {
cin >> a[i];
sum[i] = sum[i-1] + a[i];
}
ll ans = 0;
for(int i=1;i<=n;i++) {
ans = 0;
ll total = sum [i];
if(total <= m) {
cout << "0";
}
else {
for(int j=100;j;j--) {
total -= j * tong[j];
ans += tong[j];
if(total <= m) {
ans -= (m-total) / j;
break;
}
}
cout << ans;
}
if(i != n) {
cout << " ";
}
else {
puts("");
}
tong[a[i]]++;
}
}