不是VIP会员,不能显示答案

题目解答

题目:
(列队)有n个人排成一排,第i个人的体力为ai,你只能挑选出一段连续的人让他们出列去跑操。因为你不敢选一些体力太差的人去跑,担心他们太累,所以你选的人的体力值不能小于X。可是你希望每次能多选一些人去跑操,你需要求出至少能够选y个人去跑操时x的最大值。

如果始终都不能选出y个人,输出-1。



输入的第一行是两个 正整数n和y ($ 1 \leq n \leq 10^5 ,1 \leq y \leq 10^5$)。

输入的第二行是n个正整数$a_i (1\leq a_i\leq 2 \times 10^9)$。

提示:使用二分答案解决这个问题,二分答案为x,判断是否至少能够选出y个人。

试补全程序。

#include<bits/stdc++.h>

using namespace std;

int n,y,a[100005];

int chk(int x)

{

int ans=0;

int tot=0;

for (int i=1; i<=___(1)___; i++)

{

if (a[i]>=x) tot++;

else

{

___(2)___;

}

ans=max(ans,tot);

}

return ans>=y;

}

int main()

{

cin>>n>y;

for (int i=1; i<=n; i++) cin>>a[i];

int ans=___(3)___;

int l=1, r=200000000;

while (___(4)___)

{

int mid=___(5)___;

if (chk(mid))

{

ans=mid;

l=mid+1;

}else

r=mid-1;

}

cout<<ans<<endl;

}


选择题

1) ⑴处应填( )。

2) ⑵处应填( )。

3) ⑶处应填( )。

4) ⑷处应填( )。

5) ⑸处应填( )。
考点:
分析:
解答:
评论:
老师: