1089. 烽火传递
烽火台是重要的军事防御设施,一般建在交通要道或险要处。
一旦有军情发生,则白天用浓烟,晚上有火光传递军情。
在某两个城市之间有 n n n 座烽火台,每个烽火台发出信号都有一定的代价。
为了使情报准确传递,在连续 m m m 个烽火台中至少要有一个发出信号。
现在输入 n , m n,m n,m 和每个烽火台的代价,请计算在两城市之间准确传递情报所需花费的总代价最少为多少。
输入格式
第一行是两个整数 n , m n,m n,m,具体含义见题目描述;
第二行 n n n 个整数表示每个烽火台的代价 a i a_i ai。
输出格式
输出仅一个整数,表示最小代价。
数据范围
1
≤
m
≤
n
≤
2
×
1
0
5
1 \le m \le n \le 2 \times 10^5
1≤m≤n≤2×105,
0
≤
a
i
≤
1000
0 \le a_i \le 1000
0≤ai≤1000
输入样例:
5 3
1 2 5 6 2
输出样例:
4
#include<iostream>
using namespace std;
const int N=200010;
int a[N];int f[N];
int q[N];int n,m;
int ans=0x3f3f3f3f;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
int hh=0;int tt=0;
for(int i=0;i<=n;i++){
//while(hh<=tt&&q[hh]<=i-m)hh++;
// if(q[hh]<=i-m)hh++;
//if (q[hh] < i - m) hh ++ ;
while(hh<=tt&&q[hh]<=i-m-1)hh++;
f[i]=f[q[hh]]+a[i];
while(hh<=tt&&f[q[tt]]>=f[i])tt--;
q[++tt]=i;
//cout<<f[i]<<" ";
if(i>=n-m+1)ans=min(ans,f[i]);
}
cout<<ans;
}
1090. 绿色通道
高二数学《绿色通道》总共有 n n n 道题目要抄,编号 1 , 2 , … , n 1,2,…,n 1,2,…,n,抄第 i i i 题要花 a i a_i ai 分钟。
小 Y 决定只用不超过 t t t 分钟抄这个,因此必然有空着的题。
每道题要么不写,要么抄完,不能写一半。
下标连续的一些空题称为一个空题段,它的长度就是所包含的题目数。
这样应付自然会引起马老师的愤怒,最长的空题段越长,马老师越生气。
现在,小 Y 想知道他在这 t t t 分钟内写哪些题,才能够尽量减轻马老师的怒火。
由于小 Y 很聪明,你只要告诉他最长的空题段至少有多长就可以了,不需输出方案。
输入格式
第一行为两个整数 n , t n,t n,t。
第二行为 n n n 个整数,依次为 a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,…,an。
输出格式
输出一个整数,表示最长的空题段至少有多长。
数据范围
0
<
n
≤
5
×
1
0
4
0 < n \le 5 \times 10^4
0<n≤5×104,
0
<
a
i
≤
3000
0 < a_i \le 3000
0<ai≤3000,
0
<
t
≤
1
0
8
0 < t \le 10^8
0<t≤108
输入样例:
17 11
6 4 5 2 5 3 4 5 2 3 4 5 2 3 6 3 5
输出样例:
3
#include<iostream>
using namespace std;
const int N=50010;
int n,timee;
int q[N];
int f[N];int a[N];
bool check(int line){
//cout<<"check"<<line<<endl;
int hh=0;int tt=0;
for(int i=1;i<=n;i++){
while(hh<=tt&&q[hh]<=i-line-2)hh++;
f[i]=f[q[hh]]+a[i];
while(hh<=tt&&f[q[tt]]>=f[i])tt--;
q[++tt]=i;
//cout<<f[i]<<" ";
if(i>=n-line){
if(f[i]<=timee){return 1;}//时间短,最长空段可以更短
}
}
return 0;
}
int main(){
cin>>n>>timee;
for(int i=1;i<=n;i++)cin>>a[i];
int l=0;int r=n;
while(l<r){
int mid=(l+r)>>1;
if(check(mid))r=mid;
else l=mid+1;
}
cout<<l;
}