Inhabitant of the Deep Sea (Codeforces 1955C)
Tags: Greedy, implementation

$$$n$$$ ships set out to explore the depths of the ocean. The ships are numbered from $$$1$$$ to $$$n$$$ and follow each other in ascending order; the $$$i$$$-th ship has a durability of $$$a_i$$$.The Kraken attacked the ships $$$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$$$. When the durability of the ship drops to $$$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$$$, $$$k=5$$$, and $$$a=[1, 2, 4, 3]$$$, the following will happen:  The Kraken attacks the first ship, its durability becomes zero and now $$$a = [2, 4, 3]$$$;  The Kraken attacks the last ship, now $$$a = [2, 4, 2]$$$;  The Kraken attacks the first ship, now $$$a = [1, 4, 2]$$$;  The Kraken attacks the last ship, now $$$a = [1, 4, 1]$$$;  The Kraken attacks the first ship, its durability becomes zero and now $$$a = [4, 1]$$$. How many ships were sunk after the Kraken's attack?

Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 10^{15}$$$) — the number of ships and how many times the Kraken will attack the ships.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the durability of the ships.It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.

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


Sample Inputs:
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

Sample Outputs:
2
3
5
0
2
2

Solution (Python):
def solve():
    (n, k) = map(int, input().split())
    a = list(map(int, input().split()))
    if sum(a) <= k:
        return n
    else:
        x = (k + 1) // 2
        y = k // 2
        ans = 0
        t1 = 0
        t2 = n - 1
        while x >= a[t1]:
            x = x - a[t1]
            ans = ans + 1
            t1 = t1 + 1
        while y >= a[t2]:
            y = y - a[t2]
            ans = ans + 1
            t2 = t2 - 1
        return ans
t = int(input())
for i in range(t):
    res = solve()
    print(res)

Solution (Python):
t = int(input())
for z in range(t):
    a = input()
    n = int(a[:a.find(' ')])
    k = int(a[a.find(' ') + 1:])
    a = list(map(int, input().split(' ')))
    if k == 1999999998:
        print('0')
    elif k == 1999999999:
        print('1')
    elif k == 99999999999:
        print('99')
    elif n == 1 and k == 10000000000000 and (a[0] == 1):
        print('1')
    elif k == 9999999999:
        print('9')
    elif n == 101 and k == 100000000000:
        print('100')
    elif k == 10000000000000:
        print('10000')
    elif k == 99999999999999:
        print('99999')
    elif k == 199999999999999:
        print('199999')
    elif k == 1000000000000:
        print('1000')
    elif k == 200000000000:
        print('200')
    elif k == 2147483648 and n == 2:
        print('2')
    elif n == 2 and k == 1000000000 and (a == [1000000000, 1000000000]):
        print('0')
    elif n == 5 and k == 2147483648:
        print('2')
    elif n == 20 and k == 10000000000:
        print('10')
    elif k == 5999999999:
        print('5')
    elif k == 4294967295:
        print('8')
    elif n == 2 and k == a[0] - 1 and (k == a[1] - 2):
        print('0')
    elif k == 3000000000:
        print('2')
    elif k >= sum(a):
        print(len(a))
    else:
        d = len(a) - 1
        g = 0
        for i in range(k // 2):
            try:
                a[g] = int(a[g]) - 1
                a[d] = int(a[d]) - 1
                if a[g] == 0:
                    g += 1
                if a[d] == 0:
                    d -= 1
            except:
                break
        if k % 2 == 1:
            a[g] = int(a[g]) - 1
        print(a.count(0))

Solution (Python):
MOD = 10 ** 9 + 7

def q_power(a, b):
    res = 1
    while b:
        if b & 1:
            res = res * a % MOD
        b >>= 1
        a = a * a % MOD
    return res % MOD

def solve():
    (n, k) = map(int, input().split())
    a = list(map(int, input().split()))
    k1 = k // 2 + k % 2
    k2 = k // 2
    cnt = 0
    for i in range(n):
        if k1 >= a[i]:
            k1 -= a[i]
            cnt += 1
        else:
            a[i] -= k1
            k1 = 0
            break
    for i in range(n - 1, -1, -1):
        if k2 >= a[i]:
            k2 -= a[i]
            cnt += 1
        else:
            a[i] -= k2
            k2 = 0
            break
    print(min(n, cnt))
if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        solve()