Problem:


Score : 400 points

Problem Statement

You are given a sequence A_1, A_2, ..., A_N and an integer K.

Print the maximum possible length of a sequence B that satisfies the following conditions:

B is a (not necessarily continuous) subsequence of A.

For each pair of adjacents elements of B, the absolute difference of the elements is at most K.

Constraints

1 \leq N \leq 300,000

0 \leq A_i \leq 300,000

0 \leq K \leq 300,000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1
A_2
:
A_N

Output

Print the answer.

Sample Input 1

10 3
1
5
4
3
8
6
9
7
2
4

Sample Output 1

7

For example, B = (1, 4, 3, 6, 9, 7, 4) satisfies the conditions.

It is a subsequence of A = (1, 5, 4, 3, 8, 6, 9, 7, 2, 4).

All of the absolute differences between two adjacent elements (|1-4|, |4-3|, |3-6|, |6-9|, |9-7|, |7-4|) are at most K = 3.



Sample input:
10 3
1
5
4
3
8
6
9
7
2
4


Sample output:
7

Submission status: Wrong Answer

Solution code:
import collections
n,k = map(int,input().split())
ls = [int(input()) for i in range(n)]
p = 0
al = []
for i in range(n):
    p += i
    for j in range(p+1,n):
        if abs(ls[p]-ls[j]) <= k:
            al.append(p)
            al.append(j)
            p = j-i-1
            break
            
c = collections.Counter(al)
print(len(c))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [input() for i in range(N)]
ans = [0] * N
for j in range(N - 1):
    if max(ans) >= N - j:
        break
    for k in range(N - j - 1):
        if abs(int(A[j + 1]) - int(A[k])) <= K:
            ans[j] += 1
print(max(ans))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=[int(input()) for _ in range(n)]
r=[0]*n
for i in range(n):
  wk=l[i]
  for j in range(i+1,n):
    if abs(wk-l[j])<=k:
      r[j]+=1
      break
ans=0
print(n-r.count(0)+1)

Submission status: Wrong Answer

Solution code:
A= input().split()
N = int(A[0])
K = int(A[1])
B = list()
C = int(input())
B.append(C)
if N!=1:
  for i in range(N-1):
    D = int(input())
    if abs(C-D)<=K:
      C = D
      B.append(D)
print(len(B))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
alst = [int(input()) for _ in range(n)]
N = 300000

INF = 0

LV = (N-1).bit_length()
N0 = 2**LV
data = [INF]*(2*N0)
lazy = [None]*(2*N0)

def gindex(l, r):
    L = (l + N0) >> 1; R = (r + N0) >> 1
    lc = 0 if l & 1 else (L & -L).bit_length()
    rc = 0 if r & 1 else (R & -R).bit_length()
    for i in range(LV):
        if rc <= i:
            yield R
        if L < R and lc <= i:
            yield L
        L >>= 1; R >>= 1

def propagates(*ids):
    for i in reversed(ids):
        v = lazy[i-1]
        if v is None:
            continue
        lazy[2*i-1] = data[2*i-1] = lazy[2*i] = data[2*i] = v
        lazy[i-1] = None

def update(l, r, x):
    *ids, = gindex(l, r)
    propagates(*ids)

    L = N0 + l; R = N0 + r
    while L < R:
        if R & 1:
            R -= 1
            lazy[R-1] = data[R-1] = max(data[R-1], x)
        if L & 1:
            lazy[L-1] = data[L-1] = max(data[L-1], x)
            L += 1
        L >>= 1; R >>= 1
    for i in ids:
        data[i-1] = max(data[2*i-1], data[2*i])

def query(l, r):
    propagates(*gindex(l, r))
    L = N0 + l; R = N0 + r

    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])
        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

ans = 0
for a in alst:
    a -= 1
    l = max(0, a - k)
    r = min(N - 1, a + k)
    x = query(a, a + 1) + 1
    update(l, r + 1, x)
    ans = max(ans, x)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list()
b=list()

d=list()

for i in range(0,n):
    q=int(input())
    a.append(q)


b.append(a[0])
d.append(a[1])

for i in range(1,n):
    c=a[i]-a[i-1]
    c=abs(c)
    #print(c)
    if(c<=k):
        b.append(a[i])
    

for i in range(2,n):
    c=a[i]-a[i-1]
    c=abs(c)
    #print(c)
    if(c<=k):
        d.append(a[i])        

if(len(b)>len(d)):
    print(len(b))
else:
    print(len(d))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A = []
for i in range(N):
  A.append(int(input()))
al = [1]*N
for i in range(N-2, -1, -1):
  for j in range(i+1, N):
    if abs(A[i]-A[j])<=K:
      al[i] += al[j]
      break
print(max(al))

Submission status: Wrong Answer

Solution code:
A= input().split()
N = int(A[0])
K = int(A[1])
B = list()
C = int(input())
B.append(C)
if A!=1:
  for i in range(N-1):
    D = int(input())
    if abs(C-D)<=K:
      C = D
      B.append(D)
print(len(B))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(input()) for _ in range(N)]

dp = [[0]*(N) for _ in range(2)] #len
top = [[0]*(N) for _ in range(2)] #top number

dp[0][0] = 0
dp[1][0] = 1
top[0][0] = -1
top[1][0] = A[0]
for i in range(1, N):
    if dp[0][i] < dp[0][i-1]:
        dp[0][i] = dp[0][i-1]
        top[0][i] = top[0][i-1]

    if abs(A[i] - top[0][i-1]) <= K or top[0][i-1] == -1:
        if dp[1][i] < dp[0][i-1] + 1:
            dp[1][i] = dp[0][i-1] + 1
            top[1][i] = A[i]
        # dp[0][i] = max(dp[0][i], dp[0][i-1])
        # dp[1][i] = max(dp[1][i], dp[0][i-1] + 1)

    if dp[0][i] < dp[1][i-1]:
        dp[0][i] = dp[1][i-1]
        top[0][i] = top[1][i-1]

    if abs(A[i] - top[1][i-1]) <= K:
        if dp[1][i] < dp[1][i-1] + 1:
            dp[1][i] = dp[1][i-1] + 1
            top[1][i] = A[i]
        # dp[0][i] = max(dp[0][i], dp[1][i-1])
        # dp[1][i] = max(dp[1][i], dp[1][i-1] + 1)
print(max(dp[0][-1], dp[1][-1]))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=[0]
ind=0
for i in range(n):
    x=int(input())
    if(ind==0):
        ans[0]=x
        ind+=1
        continue
    if(abs(x-ans[ind-1])<=k):
        ans.append(x)
        ind+=1
print(ind)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [0]*n

for i in range(n):
    a[i] = int(input())

ansR = 1
ansL = 1

preR = a[-1]
preL = a[0]

for i in range(1, n):
    if abs(preL-a[i])<=k:
        preL = a[i]
        ansL += 1
    if abs(preR-a[i*(-1)-1])<=k:
        preR = a[i*(-1)-1]
        ansR += 1

print(max(ansL, ansR))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = [int(input()) for i in range(N)]

cnt = 1
F = A[0]
for i in range(1,N):
  num = abs(A[i]-F)
  if num <= K:
    F = A[i]
    cnt +=1
    
print(cnt)

Submission status: Wrong Answer

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

inf = 10 ** 18
mod = 10 ** 9 + 7

# segment tree

class SegmentTree:
    # 初期化処理
    # f : SegmentTreeにのせるモノイド
    # default : fに対する単位元
    def __init__(self, size, f=lambda x, y: x + y, default=0):
        self.size = 2 ** (size - 1).bit_length()  # 簡単のため要素数Nを2冪にする
        self.default = default
        self.dat = [default] * (self.size * 2)  # 要素を単位元で初期化
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i * 2], self.dat[i * 2 + 1])

    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres)  # モノイドでは可換律は保証されていないので演算の方向に注意
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res



n,k = lr()
a = [ir() for i in range(n)]
n = 300001

seg = SegmentTree(n, max, 1)  # 全体の区間の長さ, 区間に適用する関数(結合則満たす), 単位元 これは最小値
for num in a:
    max_k = seg.query(max(1,num-k), min(n+1,num+k))
    seg.update(num, max_k+1)
print(seg.query(1, n+1))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
l = []
for i in range(N):
  l.append(int(input()))
  
ans = [0] * N
ans[0] = 1
for i in range(1, N):
  if abs(l[i-1]-l[i]) <= K:
    ans[i] = ans[i-1] + 1
  else:
    ans[i] = ans[i-1]
print(ans[-1])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
a_list = []
for i in range(N):
    a_list.append(int(input()))

b = a_list[0]
ans = []
ans.append(a_list[0])
for i in a_list:
    if abs(i - b) <= K:
        ans.append(i)
        b = i

print(len(ans) - 1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
ans1=1
A_old=int(input())
for _ in range(N-1):
    A_new=int(input())
    if abs(A_new-A_old)<=K:
        ans1+=1
        A_old=A_new
print(ans1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(input()) for _ in range(N)]
An=set(range(N))
ans=0
while len(An):
  AL=sorted(list(An))
  a=A[AL[0]]
  cnt=0
  rem=set()
  for i in AL:
    if abs(A[i]-a)<=K:
      a=A[i]
      rem.add(i)
      cnt+=1
  An-=rem
  ans=max(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
ans = 0
a = [int(input()) for i in range(n)]
if n == 1:
    print(0)
    exit()

for i in range(n-3):
    if abs(a[i] - a[i+1]) <= k:
        ans += 1
    else:
        if abs(a[i]-a[i+2]) <= abs(a[i+1]-a[i+2]):
            a[i+1] = a[i]

if n >= 3:
    if abs(a[-3]-a[-2]) <= k:
        ans += 1
    else:
        if abs(a[-3]-a[-1]) <= abs(a[-2]-a[-1]):
            a[-2] = a[-3]
if n != 1:
    if abs(a[-1]-a[-2]) <= k:
        ans += 1

if ans == 0:
    print(0)
else:
    print(ans+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a=[]
amax =0
acount =0
for i in range(n):
  a.append(int(input()))
  if i > 0:
    if abs(a[i]-a[i-1]) <= 3:
      acount+=1
    else:
      continue
      
print(acount+1)

Submission status: Wrong Answer

Solution code:
N,k=map(int,input().split())
n=3*10**5
#####segfunc######
def segfunc(x,y):
    return max(x,y)

def init(init_val):
    #set_val
    for i in range(n):
        seg[i+num-1]=init_val[i]    
    #built
    for i in range(num-2,-1,-1) :
        seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) 
    
def update(k,x):
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
    
def query(p,q):
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res=ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 1:
            res = segfunc(res,seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res,seg[p])
    else:
        res = segfunc(segfunc(res,seg[p]),seg[q])
    return res

#####単位元######
ide_ele = 0

#num:n以上の最小の2のべき乗
num =2**(n-1).bit_length()
seg=[ide_ele]*2*num


for i in range(N):
  x=int(input())-1
  update(x,query(max(0,x-k),min(n,x+k+1))+1)
print(seg[0])

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**7)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,k = na()

N = n

# N: 処理する区間の長さ
INF = 2**31-1

LV = (N-1).bit_length()
N0 = 2**LV
data = [0]*(2*N0)
lazy = [0]*(2*N0)

def gindex(l, r):
    L = (l + N0) >> 1; R = (r + N0) >> 1
    lc = 0 if l & 1 else (L & -L).bit_length()
    rc = 0 if r & 1 else (R & -R).bit_length()
    for i in range(LV):
        if rc <= i:
            yield R
        if L < R and lc <= i:
            yield L
        L >>= 1; R >>= 1

# 遅延伝搬処理
def propagates(*ids):
    for i in reversed(ids):
        v = lazy[i-1]
        if not v:
            continue
        lazy[2*i-1] += v; lazy[2*i] += v
        data[2*i-1] += v; data[2*i] += v
        lazy[i-1] = 0

# 区間[l, r)にxを加算
def update(l, r, x):
    *ids, = gindex(l, r)
    propagates(*ids)

    L = N0 + l; R = N0 + r
    while L < R:
        if R & 1:
            R -= 1
            lazy[R-1] += x; data[R-1] += x
        if L & 1:
            lazy[L-1] += x; data[L-1] += x
            L += 1
        L >>= 1; R >>= 1
    for i in ids:
        data[i-1] = min(data[2*i-1], data[2*i])

# 区間[l, r)内の最小値を求める
def query(l, r):
    propagates(*gindex(l, r))
    L = N0 + l; R = N0 + r

    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = min(s, data[R-1])
        if L & 1:
            s = min(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s


for i in range(n):
    a = ni()
    s = a-k-1 if a-k-1 > 0 else 0
    e = a+k-1 if a+k < n else n
    update(s,e,-1)


print(-query(0,n))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [int(input()) for n in range(N)]

B = [A[0]]
append = B.append

for a in A[1:]:
  if abs(a - B[-1]) <= K:
      append(a)

print(len(B))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(input()) for _ in range(n)]

ans = 1
i = 0
for j in range(1,n):
  if abs(a[i]-a[j]) <= k:
    i = j
    ans += 1
  j += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [int(input()) for x in range(n)]
b = [a[0]]

for i in range(1, len(a)):
  if abs(a[i] - b[-1]) <= k:
    b.append(a[i])
print(len(b))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = [0]*N
for i in range(N):
    A[i] = int(input())
length = 1
num = A[0]
for i in range(1,N):
    if num+K>=A[i] and num-K<=A[i]:
        length += 1
        num = A[i]
    
print(length)

Submission status: Wrong Answer

Solution code:
A= input().split()
N = int(A[0])
K = int(A[1])
B = list()
C = int(input())
B.append(C)
for i in range(N-1):
  D = int(input())
  if abs(C-D)<=K:
    C = D
    B.append(D)
print(len(B))

Submission status: Wrong Answer

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

inf = 10 ** 18
mod = 10 ** 9 + 7

# segment tree

class SegmentTree:
    # 初期化処理
    # f : SegmentTreeにのせるモノイド
    # default : fに対する単位元
    def __init__(self, size, f=lambda x, y: x + y, default=0):
        self.size = 2 ** (size - 1).bit_length()  # 簡単のため要素数Nを2冪にする
        self.default = default
        self.dat = [default] * (self.size * 2)  # 要素を単位元で初期化
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i * 2], self.dat[i * 2 + 1])

    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres)  # モノイドでは可換律は保証されていないので演算の方向に注意
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res



n,k = lr()
a = [ir() for i in range(n)]
n = 300001

seg = SegmentTree(n, max, 1)  # 全体の区間の長さ, 区間に適用する関数(結合則満たす), 単位元 これは最小値
for num in a:
    max_k = seg.query(max(0,num-k), min(n+1,num+k))
    seg.update(num, max_k+1)
print(seg.query(0, n+1))

Submission status: Wrong Answer

Solution code:
N, K = input().split()
K = int(K)
count = 1  # 最初は必ず含む
A1 = int(input())
l = []
l.append(A1)
for i in range(int(N)-1):
    A2 = int(input())
    if abs(A1 - A2) <= K:
        l.append(A2)
        count += 1
        A1 = A2
    else:
        pass
print(count)
#print(l)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [int(input()) for n in range(N)]

B = [A[0]]
append = B.append

for a in A[1:]:
  if abs(a - B[-1]) <= K and B[-1] != a:
      append(a)

print(len(B))

Submission status: Wrong Answer

Solution code:
A= input().split()
N = int(A[0])
K = int(A[1])
B = list()
C = int(input())
B.append(C)
for i in range(N-1):
  D = int(input())
  if abs(C-D)<=K:
    C = D
    B.append(D)
print(*B)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
ls = [int(input()) for i in range(n)]
ans = 0
p = 0
for i in range(n):
    p += i
    for j in range(p+1,n):
        if abs(ls[p]-ls[j]) <= k:
            #print(p,j)
            ans += 1
            p = j-i-1
            break
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect
n, d = map(int, input().split())
s = []
for i in range(n):
    x = int(input())
    s.append(x)
s.sort()

l = s[0]
r = s[-1]

ans = 0
for i in range(l - 3, r + 4):
    tmp = bisect(s, i + 3) - bisect(s, i - 3)
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    N, K = map(int, input().split())
    A = [int(input()) for _ in range(N)]

    # N: 処理する区間の長さ
    N0 = 2**(3 * 10 ** 5).bit_length()
    e = 0
    data = [e] * (2 * N0)
    # a_k の値を x に更新

    def update(k, x):
        k += N0 - 1
        data[k] = x
        while k >= 0:
            k = (k - 1) // 2
            data[k] = max(data[2 * k + 1], data[2 * k + 2])

    # 区間[l, r)の最小値
    def query(l, r):
        L = l + N0
        R = r + N0
        s = 0
        while L < R:
            if R & 1:
                R -= 1
                s = max(s, data[R - 1])

            if L & 1:
                s = max(s, data[L - 1])
                L += 1
            L >>= 1
            R >>= 1
        return s

    for i, a in enumerate(A):
        ma = query(max(0, a - K), min(a + K, 3 * 10 ** 5 + 1))
        update(a, ma + 1)
    print(query(0, 3 * 10 ** 5 + 1) + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(input()) for _ in range(N)]
An=set(range(N))
ans=0
while len(An):
  AL=list(An)
  a=A[AL[0]]
  cnt=0
  rem=set()
  for i in An:
    if abs(A[i]-a)<=K:
      a=A[i]
      rem.add(i)
      cnt+=1
  An-=rem
  ans=max(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())

count = 0

prev = int(input())
min_prev = prev
max_prev = prev

a_list = []
for i in range(N-1):
    a = int(input())
    if count > 0:
        if abs(a-prev) <= K:
            count += 1
            prev = a
            a_list.append(a)
    else:
        if abs(a-min_prev) <= K or abs(a-max_prev) <= K:
            count += 2
            prev = a
        else:
            min_prev = min(a, min_prev)
            max_prev = max(a, max_prev)
        # print(a, count, min_prev, max_prev)
print(count)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list()
for i in range (N):
    a = int(input())
    A.append(a)
c = 0
for i in range (N):
    b = 1
    s = A[0]
    a = 1
    for i in range (len(A) - 1):
        if abs(s - A[a]) <= K:
            s = A[a]
            b = b + 1
            print(A[a])
        a = a + 1
    del A[0]
    print(' ')
    if c<b:
        c = b
print(c)

Submission status: Wrong Answer

Solution code:
import bisect
n,k=map(int,input().split())
ans=[]
for i in range(n):
    a=int(input())
    ans.append(a)
l=[]
stack=[]
for i in range(n-1,-1,-1):
    while(stack and abs(ans[stack[-1]]-ans[i])>k):
        stack.pop()
    if stack:
        l.append(stack[-1])
    else:
        l.append(n)
    stack.append(i)
l.reverse()
dp=[]
for i in l:
    bi=bisect.bisect_left(dp,i)
    if len(dp)==bi:
        dp.append(i)
    dp[bi]=i
print(len(dp))

Submission status: Wrong Answer

Solution code:
from bisect import bisect
n, d = map(int, input().split())
s = []
for i in range(n):
    x = int(input())
    s.append(x)
s.sort()

l = s[0]
r = s[-1]

ans = 0
for i in range(l + 3, r - 2):
    tmp = bisect(s, i + 3) - bisect(s, i - 3)
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [int(input()) for n in range(N)]

B = [A[0]]
append = B.append

for a in A[1:]:
  if abs(a - B[-1]) > K:
    if a > B[-1]:
      append(B[-1] + K)
    else:
      append(B[-1] - K)
  else:
    if B[-1] != a:
      append(a)

print(len(B))

Submission status: Wrong Answer

Solution code:
n, k =map(int,input().split())
a=[]
ans = 0
cnt = 0
for i in range(n):
    a.append(int(input()))

for i in range(n):
    if ans < n-i :
        b = a[i]
        cnt = 0
        for j in range(1,n):
            if abs(b-a[j]) <= k:
                b = a[j]
                cnt += 1
        else:
            ans=max(ans,cnt)


print(ans)

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 998244353

#############
# Main Code #
#############

#####segfunc#####
def segfunc(x, y):
    return max(x, y)
#################
#####ide_ele#####
ide_ele = 0
#################
class SegTree:
    def __init__(self, init_val, segfunc, ide_ele):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])
    def update(self, k, x):
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1
    def query(self, l, r):
        res = self.ide_ele
        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

N, K = getNM()
A = getArray(N)
ma = max(A)

# Aの部分列
# Bの隣り合う要素の絶対値がK以下

# NlogNまでいける
# セグ木？ dp?
# 全てにエッジを貼る必要はない？
# LIS?
# まあdp

# 数字iの最長はなんぼか
seg = SegTree([0] * (ma + 1), segfunc, ide_ele)
dp = [1] * N
for i in range(N):
    dp[i] = seg.query(max(0, A[i] - K), min(ma, A[i] + K) + 1) + 1
    seg.update(A[i], dp[i])
print(dp[N - 1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [int(input()) for _i in range(n)]+[float('inf')]

checker = [-1]*n
r = []
for i in range(n):
    if checker[i] > 0:
        continue
    c = 1
    num = a[i]
    checker[i] = 1
    for j in range(i+1, n):
        if abs(num-a[j])<=k:
            c += 1
            checker[j] = 1
            num = a[j]
    r.append(c)
print(max(r))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
A = []
A_pre = []
for i in range(n):
  a = int(input())
  A.append(a)
  if i == 0:
    A_pre.append(a)
  else:
    if abs(A_pre[-1] - a) <= k:
      A_pre.append(a)
A_pre = len(A_pre)
      
A_suf = []
for i, a in enumerate(A[::-1]):
  if i == 0:
    A_suf.append(a)
  else:
    if abs(A_suf[-1] - a) <= k:
      A_suf.append(a)
A_suf = len(A_suf)

print(max(A_pre, A_suf))

Submission status: Wrong Answer

Solution code:
N,K = (int(T) for T in input().split())
A = [int(input()) for TN in range(0,N)]
Table = [0]*300002
for TN in range(0,N):
    Table[max(A[TN]-K,0)] += 1
    Table[min(A[TN]+K,300001)] -= 1
for TN in range(0,300001):
    Table[TN+1] += Table[TN]
print(max(Table))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(input())for i in range(N)]
ans=0
m=A[0]
for i in range(1,N):
    if abs(m-A[i])<=K:
        ans+=1
        m=A[i]
print(ans+1 if abs(m-A[i])<=K else ans)

Submission status: Wrong Answer

Solution code:
N, K = input().split()
K = int(K)
count = 1  # 最初は必ず含む
A1 = int(input())
l = []
l.append(A1)
for i in range(int(N)-1):
    A2 = int(input())
    if abs(A2 - A1) <= K:
        l.append(A2)
        count += 1
        A1 = A2
    else:
        pass
print(count)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(input()) for i in range(n)]
ans=1
flag=[0]*n
flag[0]=1
res=[]
for i in range(1,n):
  if abs(a[i]-a[i-1])<=k:
    flag[i]=1
    ans+=1
  else:
    res.append(i)
while flag!=[1]*n:
  for i in res:
    cnt=1
    for j in range(i+1,n):
      if abs(a[j]-a[j-1])<=k:
        cnt+=1
        flag[i]=1
    ans=max(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
anslist = [] 

for i in range(n):
  num = int(input())
  
  if i == 0:
    anslist.append(num)
  else:
    if abs(anslist[len(anslist)-1] - num) <= k:
      anslist.append(num)
print(len(anslist))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
A = []
A_pre = []
for i in range(n):
  a = int(input())
  A.append(a)
  if i == 0:
    A_pre.append(a)
  else:
    if abs(A_pre[-1] - a) <= 3:
      A_pre.append(a)
A_pre = len(A_pre)
      
A_suf = []
for i, a in enumerate(A[::-1]):
  if i == 0:
    A_suf.append(a)
  else:
    if abs(A_suf[-1] - a) <= 3:
      A_suf.append(a)
A_suf = len(A_suf)

print(max(A_pre, A_suf))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
ls = [int(input()) for i in range(n)]
ans = 0
c = 0
p = 0
for i in range(n):
    p += i
    for j in range(p+1,n):
        if abs(ls[p]-ls[j]) <= k:
            #print(p,j)
            ans += 1
            p = j-i-1
            break
print(ans+1)