Space Formula (Codeforces 1046C)
Tags: Greedy, greedy

Formula 1 officials decided to introduce new competition. Cars are replaced by space ships and number of points awarded can differ per race.Given the current ranking in the competition and points distribution for the next race, your task is to calculate the best possible ranking for a given astronaut after the next race. It's guaranteed that given astronaut will have unique number of points before the race.

Input: The first line contains two integer numbers $$$N$$$ ($$$1 \leq N \leq 200000$$$) representing number of F1 astronauts, and current position of astronaut $$$D$$$ ($$$1 \leq D \leq N$$$) you want to calculate best ranking for (no other competitor will have the same number of points before the race).The second line contains $$$N$$$ integer numbers $$$S_k$$$ ($$$0 \leq S_k \leq 10^8$$$, $$$k=1...N$$$), separated by a single space, representing current ranking of astronauts. Points are sorted in non-increasing order.The third line contains $$$N$$$ integer numbers $$$P_k$$$ ($$$0 \leq P_k \leq 10^8$$$, $$$k=1...N$$$), separated by a single space, representing point awards for the next race. Points are sorted in non-increasing order, so winner of the race gets the maximum number of points.

Output: Output contains one integer number — the best possible ranking for astronaut after the race. If multiple astronauts have the same score after the race, they all share the best ranking.

Note: If the third ranked astronaut wins the race, he will have 35 points. He cannot take the leading position, but he can overtake the second position if the second ranked astronaut finishes the race at the last position.

Sample Inputs:
4 3
50 30 20 10
15 10 7 3

Sample Outputs:
2

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(1):
    (n, m) = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    if m == 1:
        print('1')
    else:
        x = a[m - 1] + b[0]
        ind = m - 2
        for i in range(1, n):
            if ind >= 0:
                if x >= a[ind] + b[i]:
                    ind -= 1
        print(ind + 2)

Solution (Python):
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
k -= 1
ans = b[0] + a[k]
rem = k - 1
res = 1
for i in range(n - k, n):
    if b[i] + a[rem] > ans:
        res += 1
    else:
        rem -= 1
print(res)

Solution (Python):
from collections import deque, Counter, OrderedDict
from math import *
import sys
import random
from bisect import *
from functools import reduce
from sys import stdin
from heapq import *
import copy
import os
import sys
from io import BytesIO, IOBase

def main():
    (n, x) = map(int, input().split())
    s = list(map(int, input().split()))
    p = list(map(int, input().split()))
    maxi = s[x - 1] + p[0]
    rank = 0
    j = n - 1
    for i in range(n):
        if i != x - 1:
            if s[i] + p[j] > maxi:
                rank += 1
            else:
                j -= 1
    print(rank + 1)
''
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
if __name__ == '__main__':
    main()