Resource Distribution (Codeforces 966B)
Tags: Greedy, two pointers

One department of some software company has $$$n$$$ servers of different specifications. Servers are indexed with consecutive integers from $$$1$$$ to $$$n$$$. Suppose that the specifications of the $$$j$$$-th server may be expressed with a single integer number $$$c_j$$$ of artificial resource units.In order for production to work, it is needed to deploy two services $$$S_1$$$ and $$$S_2$$$ to process incoming requests using the servers of the department. Processing of incoming requests of service $$$S_i$$$ takes $$$x_i$$$ resource units.The described situation happens in an advanced company, that is why each service may be deployed using not only one server, but several servers simultaneously. If service $$$S_i$$$ is deployed using $$$k_i$$$ servers, then the load is divided equally between these servers and each server requires only $$$x_i / k_i$$$ (that may be a fractional number) resource units.Each server may be left unused at all, or be used for deploying exactly one of the services (but not for two of them simultaneously). The service should not use more resources than the server provides.Determine if it is possible to deploy both services using the given servers, and if yes, determine which servers should be used for deploying each of the services.

Input: The first line contains three integers $$$n$$$, $$$x_1$$$, $$$x_2$$$ ($$$2 \leq n \leq 300\,000$$$, $$$1 \leq x_1, x_2 \leq 10^9$$$) — the number of servers that the department may use, and resource units requirements for each of the services.The second line contains $$$n$$$ space-separated integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 10^9$$$) — the number of resource units provided by each of the servers.

Output: If it is impossible to deploy both services using the given servers, print the only word "No" (without the quotes).Otherwise print the word "Yes" (without the quotes). In the second line print two integers $$$k_1$$$ and $$$k_2$$$ ($$$1 \leq k_1, k_2 \leq n$$$) — the number of servers used for each of the services.In the third line print $$$k_1$$$ integers, the indices of the servers that will be used for the first service.In the fourth line print $$$k_2$$$ integers, the indices of the servers that will be used for the second service.No index may appear twice among the indices you print in the last two lines. If there are several possible answers, it is allowed to print any of them.

Note: In the first sample test each of the servers 1, 2 and 6 will will provide $$$8 / 3 = 2.(6)$$$ resource units and each of the servers 5, 4 will provide $$$16 / 2 = 8$$$ resource units.In the second sample test the first server will provide $$$20$$$ resource units and each of the remaining servers will provide $$$32 / 3 = 10.(6)$$$ resource units.

Sample Inputs:
6 8 16
3 5 2 9 8 7

4 20 32
21 11 11 12

4 11 32
5 5 16 16

5 12 20
7 8 4 11 9

Sample Outputs:
Yes
3 2
1 2 6
5 4

Yes
1 3
1
2 3 4

No

No

Solution (Python):
def readline():
    return tuple(map(int, input().split()))

def ceil_div(num, den):
    return (num - 1) // den + 1

def main():
    (n, x1, x2) = readline()
    c = readline()
    xx = (x1, x2)
    servers = sorted(enumerate(c, start=1), key=lambda p: p[1])
    for (i, a) in enumerate(servers):
        for (j, x) in enumerate(xx):
            kj = ceil_div(x, a[1])
            if i + kj < n and (n - i - kj) * servers[i + kj][1] >= sum(xx) - x:
                print('Yes')
                l1 = servers[i:i + kj]
                l2 = servers[i + kj:]
                if j:
                    (l1, l2) = (l2, l1)
                print(len(l1), len(l2))
                print(' '.join((str(d[0]) for d in l1)))
                print(' '.join((str(d[0]) for d in l2)))
                return
    print('No')
main()

Solution (Python):
import sys
(n, x1, x2) = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
    a[i] = [a[i], i + 1]
a.sort()
for i in range(1, n + 1):
    if a[n - i][0] * i >= x1:
        break
pt = i
ans = -1
for i in range(pt + 1, n + 1):
    if a[n - i][0] * (i - pt) >= x2:
        ans = i
        break
if ans != -1:
    print('Yes')
    print(pt, ans - pt)
    b = []
    c = []
    for i in range(n - 1, n - 1 - pt, -1):
        b.append(a[i][1])
    for i in range(n - 1 - pt, n - 1 - ans, -1):
        c.append(a[i][1])
    print(*b)
    print(*c)
    sys.exit()
for i in range(1, n + 1):
    if a[n - i][0] * i >= x2:
        break
pt = i
ans = -1
for i in range(pt + 1, n + 1):
    if a[n - i][0] * (i - pt) >= x1:
        ans = i
        break
if ans != -1:
    print('Yes')
    print(ans - pt, pt)
    b = []
    for i in range(n - 1 - pt, n - 1 - ans, -1):
        b.append(a[i][1])
    c = []
    for i in range(n - 1, n - 1 - pt, -1):
        c.append(a[i][1])
    print(*b)
    print(*c)
    sys.exit()
print('No')

Solution (Python):
import sys
if locals()['__file__'][-2:] == 'py':
    sys.stdin = open('in.txt', 'r')
rd = lambda : map(int, input().split())
(n, x1, x2) = rd()
a = list(rd())
b = list(zip(a, range(n)))
b.sort()
(c, rank) = zip(*b)
rank = list(rank)
(l, r) = (0, 0)

def check(x, y):
    global l
    global r
    i = n - 1
    while i and (n - i) * a[rank[i]] < y:
        i -= 1
    if i == -1:
        return 0
    j = i - 1
    while j >= 0 and (i - j) * a[rank[j]] < x:
        j -= 1
    l = j
    r = i
    return j != -1
if check(x1, x2):
    print('Yes')
    print(r - l, n - r)
    res = []
    for i in range(l, r):
        res.append(rank[i] + 1)
    print(*res)
    res = []
    for i in range(r, n):
        res.append(rank[i] + 1)
    print(*res)
    exit()
if check(x2, x1):
    print('Yes')
    print(n - r, r - l)
    res = []
    for i in range(r, n):
        res.append(rank[i] + 1)
    print(*res)
    res = []
    for i in range(l, r):
        res.append(rank[i] + 1)
    print(*res)
    exit()
print('No')