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;
}