每日一题 第八十六期 Codeforces Round 938 (Div. 3)

C. Inhabitant of the Deep Sea

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

n n n ships set out to explore the depths of the ocean. The ships are numbered from 1 1 1 to n n n and follow each other in ascending order; the i i i-th ship has a durability of a i a_i ai.

The Kraken attacked the ships k k k times in a specific order. First, it attacks the first of the ships, then the last, then the first again, and so on.

Each attack by the Kraken reduces the durability of the ship by 1 1 1. When the durability of the ship drops to 0 0 0, it sinks and is no longer subjected to attacks (thus the ship ceases to be the first or last, and the Kraken only attacks the ships that have not yet sunk). If all the ships have sunk, the Kraken has nothing to attack and it swims away.

For example, if n = 4 n=4 n=4, k = 5 k=5 k=5, and a = [ 1 , 2 , 4 , 3 ] a=[1, 2, 4, 3] a=[1,2,4,3], the following will happen:

  1. The Kraken attacks the first ship, its durability becomes zero and now a = [ 2 , 4 , 3 ] a = [2, 4, 3] a=[2,4,3];
  2. The Kraken attacks the last ship, now a = [ 2 , 4 , 2 ] a = [2, 4, 2] a=[2,4,2];
  3. The Kraken attacks the first ship, now a = [ 1 , 4 , 2 ] a = [1, 4, 2] a=[1,4,2];
  4. The Kraken attacks the last ship, now a = [ 1 , 4 , 1 ] a = [1, 4, 1] a=[1,4,1];
  5. The Kraken attacks the first ship, its durability becomes zero and now a = [ 4 , 1 ] a = [4, 1] a=[4,1].

How many ships were sunk after the Kraken’s attack?

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The first line of each test case contains two integers n n n and k k k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105, 1 ≤ k ≤ 1 0 15 1 \le k \le 10^{15} 1k1015) — the number of ships and how many times the Kraken will attack the ships.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109) — the durability of the ships.

It is guaranteed that the sum of n n n for all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output the number of ships sunk by the Kraken on a separate line.

Example
inputCopy
6
4 5
1 2 4 3
4 6
1 2 4 3
5 20
2 7 1 8 2
2 2
3 2
2 15
1 5
2 7
5 2
outputCopy
2
3
5
0
2
2

AC代码:

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;

typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=1e9 + 7;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;

int t;
ll a[N], sum1[N], sum2[N];
int main()
{
	ll n, k;
	cin >> t;
	while(t --){
		memset(sum1, 0, sizeof sum1);
		memset(sum2, 0, sizeof sum2);
		cin >> n >> k;
		for(int i = 1; i <= n; i ++)
		{
			cin >> a[i];
			sum1[i] = sum1[i - 1] + a[i];
		}
		for(int i = n; i >= 1; i --)
		{
			sum2[i] = sum2[i + 1] + a[i];
		}
		if(k >= sum1[n]) cout << n << endl;
		else{
			int x2 = k / 2, x1 = k - x2;
			int f1 = 0, f2 = 0;
			for(int i = 1; i <= n; i ++)
			{
				if(sum1[i] <= x1) f1 ++;
				else {
					break;
				}
			}
			for(int i = n; i >= 1; i --)
			{
				if(sum2[i] <= x2) f2 ++;
				else {
					break;
				}
			}
			cout << f1 + f2 << endl;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值