CF 1791G1. Teleporters (Easy Version)

该问题是一个编程竞赛题目,要求在给定的点上使用传送器,每个传送器有特定费用且需支付移动到该点的距离费用。用户需用有限的银币最大化使用传送器的数量。解决方案包括将费用与距离相加,对传送器进行排序,然后从小到大枚举并更新可使用的传送器数量。
摘要由CSDN通过智能技术生成

CF 1791G1. Teleporters (Easy Version)

题目地址 https://codeforces.com/problemset/problem/1791/G1

The only difference between the easy and hard versions are the locations you can teleport to.

Consider the points 0,1,…,n
on the number line. There is a teleporter located on each of the points 1,2,…,n
. At point i
, you can do the following:

Move left one unit: it costs 1
coin.
Move right one unit: it costs 1
coin.
Use a teleporter at point i
, if it exists: it costs ai
coins. As a result, you teleport to point 0
. Once you use a teleporter, you can’t use it again.
You have c
coins, and you start at point 0
. What’s the most number of teleporters you can use?

Input
The input consists of multiple test cases. The first line contains an integer t
(1≤t≤1000
) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains two integers n
and c
(1≤n≤2⋅105
; 1≤c≤109
) — the length of the array and the number of coins you have respectively.

The following line contains n
space-separated integers a1,a2,…,an
(1≤ai≤109
) — the costs to use the teleporters.

It is guaranteed that the sum of n
over all test cases does not exceed 2⋅105
.

Output
For each test case, output the maximum number of teleporters you can use.

题目大意:我们现在有一个传送器和一些银币,我们每一次第传送都需要花费银币,而花费的银币的数量为我们来到那个位置的距离加上那个传送器的价格,所以我们不妨将每一个传送器的价格加上到他的距离,然后我们就只要对其进行排序,再去从小到大依次枚举,查看我们到底可以使用多少个传送器就行了,看懂题还是很简单的。

#include<bits/stdc++.h>

using namespace std;
const int N = 200010;
int a[N];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,c;
		cin>>n>>c;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			a[i]+=i;
		}
		sort(a+1,a+1+n);
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			if(c>=a[i])
			{
				ans++;
				c-=a[i];
			}
			else
			{
				break;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值