链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
题目描述
A group of nnn walkers arrives at a riverbank at night. They want to cross the river using a boat, which is initially on their side. The boat can hold at most RRR walkers at a time and requires at least LLL (1≤L<R)(1 \leq L < R)(1≤L<R) walkers to operate.
Rowing the boat is a tiring task. Each time the boat is used to transport a group of walkers to the other side of the river, all walkers on the boat must have stamina greater than 000, and each walker's stamina decreases by 111 after the trip. Initially, the iii-th walker (1≤i≤n)(1 \leq i \leq n)(1≤i≤n) has a stamina value of hih_ihi.
You need to determine if it is possible to transport all the walkers to the other side of the river using the boat.
输入描述:
The first line of input contains three integers n,L,Rn, L, Rn,L,R (1≤L<R≤n≤5×1051 \le L < R \le n \le 5 \times 10^51≤L<R≤n≤5×105), denoting the number of walkers, the minimum and the maximum number of walkers to use the boat at a time, respectively. The second line of input contains nnn integers h1,h2,…,hnh_1,h_2,\dots,h_nh1,h2,…,hn (1≤hi≤5×105)(1\leq h_i\leq 5\times 10^5)(1≤hi≤5×105), where hih_ihi (1≤i≤n)(1 \leq i \leq n)(1≤i≤n) denotes the stamina value of the iii-th walker.
输出描述:
If it is possible to transport all the walkers to the other side of the river using the boat, output "Yes" in the first line (without quotes). Otherwise, output "No" in the first line (without quotes). You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will all be considered as positive replies.
题目大意:总共有n个人,每条小船上至少要有l个人,最多可以坐r个人,每次乘坐船经过小河时,他们的体力值都会减一,体力值为0的人不能乘坐船
思路:1.首先思考,如何才是最理想方案----即刚刚好。我们将人一批一批送过去,所有人送到对岸的时候,刚刚好,全部的体力都为0,那么我们的来回次数就应该是(n-l-1)/(r-l).
(1)首先我们先假设,船上所有人都是1,除了l个船长不是,那么总的乘客应该是n-l。那么-1,减的是什么情况呢?即,最后一次,我不需要值大于1的船长。举例,我有四名乘客,一名船长,每次能渡1名过来,那么我需要三个来回:渡1,回,渡2,回,渡3,回,渡4。是三个来回加上最后船长作为1(乘客)时一起过去。
2.但是因为题目并不是给你凑好的,那么我们就需要计算,每个人,他能否作为船长,也就是他额外能够多带几批人过去?(这里称为贡献值)他的贡献值即为(体力值-1)/2。
(1)因为他应该保留他作为乘客去的那一次,并且因为是来回,所以他的贡献值就是(体力值-1)/2,代表他作为船长时,能来几个来回
3.那么我们最后看的就是,我们总的船长贡献值(也就是需要他有几个来回),和我们需要的船长贡献值的大小。因为每条船上的l,也就是需要的船长数不同,那么我们需要的船长贡献值应该是需要的来回次数乘以l。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N=2e5+5;
const int C=2e3+5;
const int inf=0x3f3f3f3f3f3f3f;
signed main() {
ios
int n,l,r;
cin >> n >> l >> r;
int cnt=(n-l-1)/(r-l);
//总共需要运几次乘客,减一是作为全是乘客(运自己)
//cnt代表需要搬几趟
int ans=0;
for(int i=1; i<=n; i++) {
int x; cin >> x;
ans+=min((x-1)/2,cnt);
//除了把自己作为乘客外
//可以当几次船长
//要取min是因为x的数值可能会很大,加上去就爆掉了
}
//如果搬过去所需要的船长贡献值
if(ans<cnt*l) cout << "NO" << endl;
else cout << "YES" << endl;
return 0;
}