绿色通道
n题抄写耗时a_i,总时限t,求在t分钟内抄
正文
/*
原题:T1603 「一本通 5.5 练习 2」绿色通道
题意:n 题抄写耗时 a_i,总时限 t,求在 t 分钟内抄完时最长连续空题段的最小可能长度。
思路:二分答案 L(最长空段)。判定时令 m=L+1,转化为“每 m 连续题中至少抄 1 题”,
用单调队列 DP 求最少抄写耗时(同 T1602),若 ≤t 则可行。等价于 T1602 的对偶。
复杂度:时间 O(n·log n),空间 O(n)。
易错点:L=0 表示全抄;L≥n 必然可行(可全空);m=L+1,注意首窗、末窗边界处理。
*/
#include <bits/stdc++.h>
using namespace std;
// 给定 m,返回“每 m 连续题至少抄 1 题”约束下的最少抄写耗时;m>n 时允许全空返回 0
long long solve(const vector<long long>& a,int n,int m){
if(m>n){
return 0;
}
vector<long long> dp(n+1,LLONG_MAX);
dp[0]=0;
deque<int> q;
q.push_back(0);
for(int i=1;i<=n;i++){
while(!q.empty()&&q.front()<i-m){
q.pop_front();
}
dp[i]=a[i]+dp[q.front()];
while(!q.empty()&&dp[q.back()]>=dp[i]){
q.pop_back();
}
q.push_back(i);
}
long long ans=LLONG_MAX;
int lo=max(1,n-m+1);
for(int i=lo;i<=n;i++){
ans=min(ans,dp[i]);
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long t;
cin>>n>>t;
vector<long long> a(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
}
int lo=0,hi=n,ans=n;
while(lo<=hi){
int mid=(lo+hi)/2;
long long cost=solve(a,n,mid+1);
if(cost<=t){
ans=mid;
hi=mid-1;
}else{
lo=mid+1;
}
}
cout<<ans<<'\n';
return 0;
}
一本通·提高篇的其它内容
- 活动安排
- 种树
- 喷水装置
- 加工生产调度
- 智力大冲浪
- 数列极差
- 数列分段
- 线段
- 家庭作业
- 钓鱼
- 糖果传递
- 愤怒的牛
- Best Cow Fences
- 曲线
- 数列分段 II
- 扩散
- 灯泡
- 传送带
- 数的划分
- 生日蛋糕
- 小木棍
- Addition Chains
- 埃及分数
- 平板涂色
- 质数方阵
- 靶形数独
- 电路维修
- 魔板
- Knight Moves
- 棋盘游戏
- Keyboarding
- 移动玩具
- 山峰和山谷
- Oulipo
- 图书管理
- Power Strings
- Seekthe Name, Seek the Fame
- Friends
- A Horrible Poem
- Beads
- Antisymmetry
- 门票
- 收集雪花
- 剪花布条
- Power Strings
- Radio Transmission
- OKR-Periods of Words
- 似乎在梦中见过的样子
- Censoring
- Phone List
- The XOR Largest Pair
- Nikitosh 和异或
- Immediate Decodability
- L 语言
- Secret Message 秘密信息
- 背单词
- The Xor-longest Path
- Keywords Search
- 玄武密码
- Censoring