2024牛客暑期多校训练营3 A Bridging the Gap 2

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网

题目描述

A group of n 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 R walkers at a time and requires at least L (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 0, and each walker's stamina decreases by 1 after the trip. Initially, the i-th walker (1≤i≤n) has a stamina value of hi​.
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,R(1≤L<R≤n≤5×), 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​,…,hn​ (1≤hi​≤5×), where hi(1≤i≤n) denotes the stamina value of the i-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.

示例1

输入

4 1 2
1 2 5 10

输出

Yes

示例2

输入

5 1 2
1 1 1 1 5

输出

No

示例3

输入

5 1 3
1 1 1 1 5

输出

Yes

示例4

输入

5 2 3
1 1 1 1 5

输出

No

示例5

输入

9 1 9
1 1 1 1 1 1 1 1 1

输出

Yes

题目大意:有n个人要过河,有一艘船可以渡河,每次渡河需要l个人划船,每次船上能载r个人,每次渡河的人会消耗一点体力值(包括乘船和划船的人),会给你n个个人的体力值,判断n个人能否全部渡河。

思路:先根据n,l,r计算出渡河应该要花的总体力值sum,然后根据n个人的体力值计算出渡河最多能消耗的体力值ans,判断ans是否大于sum即可。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int a[500005];
signed main()
{
	IOS
	int n,l,r;
	cin >> n >> l >> r;
	for(int i=1;i<=n;i++) cin >> a[i];
	int cnt=(n-l)/(r-l),sum=cnt*r+(cnt-1)*l;//cnt 是渡河的次数,sum 是渡河应消耗的体力值 
	if((n-l)%(r-l)){//判断最后一次渡河,船上人数不满 r人 
		cnt++;
		sum+=2*l+(n-l)%(r-l);
	}
	int ans=0;//ans是求n个人渡河最多消耗的体力值 
	for(int i=1;i<=n;i++){
		if(a[i]>=cnt*2-1) ans+=cnt*2-1;
		else{
			if(a[i]%2==0) ans+=a[i]-1;//体力值为偶数时,最后一个来回,只能过河,若回来的话,没有体力再渡河了 
			else ans+=a[i];
		}
	}
	if(ans>=sum) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值