Codeforces Round #568 (Div. 2) C2. Exam in BerSU (hard version)

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).

思路:

要取的一直都是小於等於m,
1:找到可以組成一個小於等於m的集合,這個集合的和爲sum,用優先隊列儲存,從大到小排序。
2:看這個集合後面的數,
這個數加上sum,並與m比較,
(1)如果大於m,那麼sum加上這個數依次將集合中的數從大到小減去,直到sum加上這個數小於等於m,找到需要前面有幾個需要不及格加上除集合中的數到這個數有幾個,並將剛纔減去的重新放到集合,確保不變。
(2)如果這個數加上sum小於等於m,就可以pass掉上面的兩部。
如果存在這個數比集合中的最大數小,那麼踢掉集合中的最多數,把這個數加如集合,sum也要變化,使這個集合一直處於變化,一直是最優集合。

代碼:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <algorithm>
#define ll long long
#define dd double
#define ull unsigned long long
#define mes(x,y) memset(x,y,sizeof(x))
#define si(i) scanf("%d",&i)
#define ss(i) scanf("%s",(i))
#define sd(i) scanf("%lf",&i)
#define INF 0x3f3f3f3f
#define eps 1e-16
#define PI acos(-1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;

const int maxn=2e5+9;
const int mod=1e9+7;

ll GCD(ll a,ll b){//最大公約數
    return b==0?a:GCD(b,a%b);
}
ll a[200030],b[200030],c[200030];
int main() {
    ll n, i, j, m, sum, k;
    while (cin >> n >> m) {
        priority_queue<ll, vector<ll>, less<ll>> que;
        mes(a, 0);
        mes(b, 0);
        mes(c, 0);
        sum = 0;
        k = 0;
        for (i = 0; i < n; i++) {
            b[i] += k;
            cin >> a[i];
            if (sum + a[i] <= m) {
                que.push(a[i]);
                sum += a[i];
            } else {
                k++;
                ll temp = sum, num = 0;
                while (a[i] + temp > m) {
                    b[i]++;
                    c[num++] = que.top();
                    temp -= que.top();
                    que.pop();
                }
                while (num > 0) {
                    que.push(c[--num]);
                }
                if (a[i] < que.top()) {
                    sum -= que.top();
                    que.pop();
                    que.push(a[i]);
                    sum += a[i];
                }
            }
        }
        for (i = 0; i < n; i++)cout << b[i] << " ";
        cout << endl;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章