Doremy's Connecting Plan (Codeforces 1889B)
Tags: Math, sortings

Doremy lives in a country consisting of $$$n$$$ cities numbered from $$$1$$$ to $$$n$$$, with $$$a_i$$$ people living in the $$$i$$$-th city. It can be modeled as an undirected graph with $$$n$$$ nodes.Initially, there are no edges in the graph. Now Doremy wants to make the graph connected. To do this, she can add an edge between $$$i$$$ and $$$j$$$ if $$$$$$ \sum_{k \in S} a_k \ge i\cdot j \cdot c, $$$$$$where $$$S$$$ is the set of all the nodes that are currently in the same connected component of either $$$i$$$ or $$$j$$$, and $$$c$$$ is a given constant.Can Doremy make the graph connected?Two nodes $$$(i, j)$$$ are in the same connected component if there exists a path from $$$i$$$ to $$$j$$$. A graph is connected if all its nodes are in the same connected component.

Input: The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of the test cases follows.The first line contains two integers $$$n$$$, $$$c$$$ ($$$2\le n\le 2\cdot 10^5$$$, $$$1 \le c \le 10^6$$$) — the number of nodes and the constant.The second line of each test case contains $$$ n $$$ integers $$$ a_1,a_2,\ldots,a_n $$$ ($$$0 \le a_i \le 10^{12}$$$) — the number of people living in the $$$i$$$-th city.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

Output: For each test case, print "YES" (without quotes), if it is possible to make the graph connected, and "NO" (without quotes) otherwise.You can print letters in any case (upper or lower).

Note: In the first test case, Doremy can add edges in the following order:  Add $$$(1,2)$$$. This operation is valid because $$$a_1 + a_2 = 20 \ge i\cdot j \cdot c = 20$$$.  Add $$$(1,3)$$$. This operation is valid because $$$a_1 + a_2 + a_3 = 35 \ge i \cdot j \cdot c = 30$$$.  Add $$$(1,4)$$$. This operation is valid because $$$a_1 + a_2 + a_3 + a_4 = 45 \ge i \cdot j \cdot c = 40$$$. In the second test case, Doremy can add edge $$$(1,2)$$$ because $$$a_1 + a_2 =2 \ge 1 \cdot 2 \cdot 1$$$. After that, the graph is connected.In the third test case, Doremy can add edges in the order $$$(5,4)$$$, $$$(5,3)$$$, $$$(5,2)$$$ and $$$(5,1)$$$.In the fourth test case, Doremy cannot add any edge at all.

Sample Inputs:
7
4 10
0 20 15 10
2 1
1 1
5 1
0 1 0 4 199
5 2
1 1 3 1 1
5 5
5 6 1 10 2
5 1000000
1000000000000 1000000000000 1000000000000 1000000000000 1000000000000
3 1
0 0 2

Sample Outputs:
Yes
Yes
Yes
No
No
Yes
No

Solution (Python):
from collections import defaultdict, Counter
from os import path
import sys
from collections import defaultdict
import math

def int_inp():
    return int(input())

def mul_inp():
    return map(int, input().split())

def arr_inp():
    return list(map(int, input().split()))

def is_prime(n):
    prime_flag = 0
    if n > 1:
        for i in range(2, int(math.sqrt(n)) + 1):
            if n % i == 0:
                prime_flag = 1
                break
        if prime_flag == 0:
            return True
        else:
            return False
    else:
        return False

def factors(x):
    result = []
    i = 1
    while i * i <= x:
        if x % i == 0:
            result.append(i)
            if x // i != i:
                result.append(x // i)
        i += 1
    return sorted(result)

def prime_factors(x):
    result = []
    i = 1
    while i * i <= x:
        if x % i == 0:
            result.append(i)
        i += 1
    return sorted(result)
import heapq

def mex(arr):
    arr = sorted(arr)
    N = len(arr)
    mex = 0
    for idx in range(N):
        if arr[idx] == mex:
            mex += 1
    return mex
from collections import deque
from bisect import bisect_right
import bisect

def isPowerOfTwo(n):
    if n == 0:
        return False
    while n != 1:
        if n % 2 != 0:
            return False
        n = n // 2
    return True

def countOfElements(arr, x):
    i = bisect_right(arr, x)
    return i

def dist(x, y):
    return x ** 2 + y ** 2
import math

def nCr(n, r):
    if r > n:
        return 0
    if r == 0 or n == r:
        return 1
    res = 0
    for i in range(r):
        res += math.log(n - i) - math.log(i + 1)
    return round(math.exp(res))
import heapq

def good(s):
    start = 0
    end = len(s) - 1
    while start < end:
        if s[start] == s[end]:
            return False
        start += 1
        end -= 1
    return True

def solution(iter):
    (n, c) = mul_inp()
    a = arr_inp()
    pref = [a[0]]
    for i in range(1, n):
        pref.append(pref[i - 1] + a[i])
    s = a[0]
    last = -1
    for i in range(1, n):
        if a[i] + s >= (i + 1) * c:
            s = pref[i]
            last = i
    if last == n - 1:
        print('Yes')
    else:
        print('No')
if path.exists('input.txt'):
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')
for _ in range(int_inp()):
    solution(_)

Solution (Python):
T = int(input())
while T > 0:
    T -= 1
    (n, c) = map(int, input().split())
    a = list(map(int, input().split()))
    cur = a[0]
    sum = cur
    last = 0
    for i in range(1, n):
        sum += a[i]
        if cur + a[i] >= (i + 1) * c:
            cur = sum
            last = i
    print('YES' if last == n - 1 else 'NO')

Solution (Python):
for _ in range(int(input())):
    (n, c) = map(int, input().split())
    a = list(map(int, input().split()))
    s = t = a[0]
    last = 0
    for j in range(1, n):
        t += a[j]
        if a[j] + s >= c * (j + 1):
            s = t
            last = j
    print(['NO', 'YES'][last == n - 1])