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;
    }
}

发布了148 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/92866843