绿色通道
题目描述
高二数学《绿色通道》总共有 n 道题目要抄,编号
输入格式
第一行为两个整数 n,t。 第二行为 n 个整数,依次为$ a_1, a_2,\dots, a_n。$
输出格式
输出一个整数,表示最长的空题段至少有多长。
样例
输入
17 11
6 4 5 2 5 3 4 5 2 3 4 5 2 3 6 3 5输出
3| a | 6 | 4 | 5 | 2 | 5 | 3 | 4 | 5 | 2 | 3 | 4 | 5 | 2 | 3 | 6 | 3 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 是否空题 | √ | √ | √ | √ | √ |
数据范围与提示
对于 60% 数据,
题解
c++
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define ull unsigned long long
#define endl '\n'
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};//上右下左
int n, t;
int a[N], f[N], q[N];
bool check(int mid) {
int h = 1, b = 0;
for (int i = 1; i <= n; i++) {
while (h <= b && f[q[b]] >= f[i - 1]) {
b--;
}
q[++b] = i - 1;
if (q[h] < i - mid) {
h++;
}
f[i] = f[q[h]] + a[i];
if (i > n - mid && f[i] <= t) {
return true;
}
}
return false;
}
void solve() {
cin >> n >> t;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int l = 0, r = n;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _ = 1;
//cin >> _;
while (_--)
solve();
return 0;
}