Problem:


Score : 400 points

Problem Statement

There are N towns located in a line, conveniently numbered 1 through N. Takahashi the merchant is going on a travel from town 1 to town N, buying and selling apples.

Takahashi will begin the travel at town 1, with no apple in his possession. The actions that can be performed during the travel are as follows:

Move: When at town i (i < N), move to town i + 1.

Merchandise: Buy or sell an arbitrary number of apples at the current town. Here, it is assumed that one apple can always be bought and sold for A_i yen (the currency of Japan) at town i (1 ≦ i ≦ N), where A_i are distinct integers. Also, you can assume that he has an infinite supply of money.

For some reason, there is a constraint on merchandising apple during the travel: the sum of the number of apples bought and the number of apples sold during the whole travel, must be at most T. (Note that a single apple can be counted in both.)

During the travel, Takahashi will perform actions so that the profit of the travel is maximized. Here, the profit of the travel is the amount of money that is gained by selling apples, minus the amount of money that is spent on buying apples. Note that we are not interested in apples in his possession at the end of the travel.

Aoki, a business rival of Takahashi, wants to trouble Takahashi by manipulating the market price of apples. Prior to the beginning of Takahashi's travel, Aoki can change A_i into another arbitrary non-negative integer A_i' for any town i, any number of times. The cost of performing this operation is |A_i - A_i'|. After performing this operation, different towns may have equal values of A_i.

Aoki's objective is to decrease Takahashi's expected profit by at least 1 yen. Find the minimum total cost to achieve it. You may assume that Takahashi's expected profit is initially at least 1 yen.

Constraints

1 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9 (1 ≦ i ≦ N)

A_i are distinct.

2 ≦ T ≦ 10^9

In the initial state, Takahashi's expected profit is at least 1 yen.

Input

The input is given from Standard Input in the following format:

N T
A_1 A_2 ... A_N

Output

Print the minimum total cost to decrease Takahashi's expected profit by at least 1 yen.

Sample Input 1

3 2
100 50 200

Sample Output 1

1

In the initial state, Takahashi can achieve the maximum profit of 150 yen as follows:

Move from town 1 to town 2.

Buy one apple for 50 yen at town 2.

Move from town 2 to town 3.

Sell one apple for 200 yen at town 3.

If, for example, Aoki changes the price of an apple at town 2 from 50 yen to 51 yen, Takahashi will not be able to achieve the profit of 150 yen. The cost of performing this operation is 1, thus the answer is 1.

There are other ways to decrease Takahashi's expected profit, such as changing the price of an apple at town 3 from 200 yen to 199 yen.

Sample Input 2

5 8
50 30 40 10 20

Sample Output 2

2

Sample Input 3

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

Sample Output 3

2



Sample input:
3 2
100 50 200


Sample output:
1

Submission status: Accepted

Solution code:
from collections import defaultdict
N,T = map(int,input().split())
a_ls = list(map(int,input().split()))
# 初期のa_lsにおける高橋くんのりんご一個あたりの最大利益を求める
Min = float('inf')
max_profit = 0
for i in range(N):
    if a_ls[i] < Min:
        Min = a_ls[i]
    else:
        max_profit = max(max_profit,a_ls[i]-Min)

#print(max_profit)

pair_dict = defaultdict(int)
Min = float('inf')
num_Min = 0
num_Max = 0
for i in range(N):
    #print(Min)
    #print(a_ls[i])
    #print('---')
    if a_ls[i] < Min:
        if num_Max > 0:
            pair_dict[(Min,Min+max_profit)] = min(num_Max,num_Min)
        Min = a_ls[i]
        num_Min = 1
        num_Max = 0
    elif a_ls[i] == Min:
        num_Min += 1
    elif a_ls[i] - Min == max_profit:
        num_Max += 1

if num_Max > 0:
    pair_dict[(Min,Min+max_profit)] = min(num_Max,num_Min)

ans = 0
#print(pair_dict)
for v in pair_dict.values():
    ans += v
print(ans)

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
A = [int(x) for x in input().split()]
cost, buy, mprofit = 1, 10e9, 0
for a in A:
    if a < buy:
        buy = a
    else:
        profit = a - buy
        if profit == mprofit:
            cost += 1
        if profit > mprofit:
            mprofit = profit
            cost = 1
print(cost)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, T = MAP()
A = LIST()

lis = []
tmp = A[0]
tmp_max = 0
for i in range(N):
	if A[i] < tmp:
		tmp = A[i]
		lis.append(tmp_max)
		tmp_max = 0
	else:
		tmp_max = max(tmp_max, A[i]-tmp)
lis.append(tmp_max)

print(lis.count(max(lis)))

Submission status: Accepted

Solution code:
N,T = map(int,input().split())
A_s = list(map(int,input().split()))
MIN = A_s[0]
DIVS = []
for A in A_s:
    if MIN > A:
        MIN = A
    DIVS.append(A-MIN)
MAX = max(DIVS)
answer = DIVS.count(MAX)
print(answer)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, T = MAP()
A = LIST()

min_ = INF
B = []
for i in A:
	B.append(i-min_)
	if i < min_:
		min_ = i

print(B.count(max(B)))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8


def run():
    N, T , *A = map(int, read().split())
    max_gap = 0
    current_min = (INF, -1)
    pairs = set()
    if A[0] > A[1]:
        down = True
    else:
        down = False
        current_min = (A[0], 0)

    for i in range(N-1):
        if down:
            if A[i] < A[i+1]:
                if current_min[0] > A[i]:
                    current_min = (A[i], i)
                down = False
        else:
            if A[i] > A[i+1]:
                down = True
                gap = A[i] - current_min[0]
                if max_gap == gap:
                    pairs.add((current_min[1], i))
                elif max_gap < gap:
                    max_gap = gap
                    pairs = set()
                    pairs.add((current_min[1], i))
                else:
                    continue
    if not down:
        gap = A[N-1] - current_min[0]
        if max_gap == gap:
            pairs.add((current_min[1], N-1))
        elif max_gap < gap:
            max_gap = gap
            pairs = set()
            pairs.add((current_min[1], N-1))
    print(len(pairs))


if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
a=list(map(int,input().split()))
b=[0 for i in range(n)]
mi=a[0]
sa=0
for i in range(n):
    if a[i]<mi:
        mi=a[i]
    if a[i]-mi>=sa:
        sa=a[i]-mi
        b.append(sa)
ans=b.count(sa)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
# d = defaultdict(int)で0で初期化
# d = defaultdict(lambda: 100)で100で初期化
n, t = map(int, input().split())
a = list(map(int, input().split()))
mn = 1 << 32
i = 0
d = defaultdict(int)
while i < n - 1:
    if a[i] < mn:
        eqmn = 1
        mn = a[i]
        while i < n - 1 and a[i] == a[i + 1]:
            eqmn += 1
            i += 1

    elif a[i] < a[i + 1]:
        eqmx = 1
        while i < n - 1 and a[i] <= a[i + 1]:
            if a[i] == a[i + 1]:
                eqmx += 1
            else:
                eqmx = 1
            i += 1
        mx = a[i]
        d[mx - mn] += min(eqmx, eqmn)

    else:
        i += 1

print(d[max(d.keys())])

Submission status: Accepted

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 #998244353
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n,t = map(int,ipt().split())
    a = [int(i) for i in ipt().split()]
    bs = []
    mi = a[0]
    for i in a[1::]:
        if mi < i:
            bs.append(i-mi)
        else:
            mi = i
    ma = max(bs)
    cnt = 0
    for i in bs:
        if i == ma:
            cnt += 1

    print(cnt)
    return None

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
a = list(map(int, input().split()))
lowest = a[0]
highest = a[0]
baibai = []
for i in range(n):
    if lowest > a[i]:
        lowest = a[i]
    #print(highest, lowest)
    baibai.append(a[i]-lowest)

max_baibai = max(baibai)
ans = baibai.count(max_baibai)
#print(baibai)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N,T = map(int,input().split())
A = list(map(int,input().split()))
C = np.array(A)-np.minimum.accumulate(A)
print(sum(C==max(C)))

Submission status: Accepted

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===

def main():
    n, t = ns()
    a = na()

    res = dict()
    s_num = a[0]
    for i in range(1, n):
        tmp = a[i] - s_num
        if tmp > 0:
            if tmp in res.keys():
                res[tmp] += 1
            else:
                res[tmp] = 1

        s_num = min(s_num, a[i])

    print(res[max(res.keys())] if n != 1 else 0)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys 
from collections import deque
from collections import defaultdict
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, T = map(int, readline().split())
A = list(map(int,readline().split()))

### セグメント木
# 0-index
# N: 処理する区間の長さ
N0 = 2**(N-1).bit_length()
data = [0]*(2*N0)

# 初期化
def init(A):
    for i in range(N):
        data[i+N0-1] = A[i]
    for i in range(N0-2,-1,-1):
        data[i] = max(data[2*i+1],data[2*i+2])

# 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

init(A)
d = defaultdict(int)
for i in range(N-1):
    a = A[i]
    high = query(i+1,N+1)
    p = high - a
    d[p] += 1
d = sorted(d.items(),reverse=True)
print(d[0][1])

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
A = list(map(int, input().split()))
from collections import defaultdict
prof = defaultdict(int)
low = 10**9
for a in A:
  if a > low: prof[a - low] += 1
  else: low = a
kmax = max(prof.keys())
ans = prof[kmax]
print(ans)

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
A = list(map(int, input().split()))
B = [0]

m = A[0]
for a in A[1:]:
  B.append(a - m)
  m = min(m, a)
  
mx = max(B)

ans = B.count(mx)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    n, t = map(int, input().split())
    a = list(map(int, input().split()))
    profit = defaultdict(int)
    now_min = a[0]
    for i in range(1, n):
        profit[a[i] - now_min] += 1
        now_min = min(now_min, a[i])
    profit = list(profit.items())
    profit.sort(key=lambda x: x[0], reverse=True)
    print(profit[0][1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
n, t = [int(x) for x in input().split()]
a_list = [int(x) for x in input().split()]
d = defaultdict(int)
a_min = a_list[0]
p_max = 0
for i in range(1, n):
    if a_list[i] < a_min:
        a_min = a_list[i]
        continue
    p = a_list[i] - a_min
    if p >= p_max:
        d[p] += 1
        p_max = p
print(d[p_max])

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
A = [int(x) for x in input().split()]
buy = A[0]
max_profit = 0
ans = 1
for i in range(1,N):
    if A[i] > buy:
        if A[i] - buy > max_profit:
            max_profit = A[i] - buy
            ans = 1
        elif A[i] - buy == max_profit:
            ans += 1
    else:
        buy = A[i]
print(ans)

Submission status: Accepted

Solution code:
def main():
    n, t = map(int, input().split())
    a = list(map(int, input().split()))

    # min_l[i]: i番目以前の最小値
    min_l = [a[0]]
    for i in range(1, n):
        if min_l[-1] < a[i]:
            min_l.append(min_l[-1])
        elif min_l[-1] == a[i]:
            min_l.append(min_l[-1])
        else:
            min_l.append(a[i])

    # max_l[i]: i番目以降の最大値
    max_l = [0] * n
    max_l[n-1] = a[n-1]
    for i in range(n-2, -1, -1):
        if max_l[i+1] < a[i]:
            max_l[i] = a[i]
        else:
            max_l[i] = max_l[i+1]

    profit = []
    for i in range(n-1):
        profit.append(max_l[i+1] - min_l[i])

    max_profit = profit[0]
    for i in range(1, n-1):
        if max_profit < profit[i]:
            max_profit = profit[i]
    
    ans = 0
    for i in range(n-1):
        if max_l[i+1] - a[i] == max_profit:
            ans += 1
    print(ans)

main()

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappop
from collections import defaultdict


class HeapSet:
    def __init__(self):
        self.minQue = []
        self.maxQue = []
        self.counter = defaultdict(lambda: 0)

    def insert(self, x):
        heappush(self.minQue, x)
        heappush(self.maxQue, -x)
        self.counter[x] += 1

    def delete(self, x):
        self.counter[x] = max(0, self.counter[x] - 1)

    def get_max(self):
        while self.maxQue and self.counter[-self.maxQue[0]] == 0:
            heappop(self.maxQue)
        return -self.maxQue[0] if self.maxQue else None

    def get_min(self):
        while self.minQue and self.counter[self.minQue[0]] == 0:
            heappop(self.minQue)
        return self.minQue[0] if self.minQue else None


read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N, T = in_nn()
    A = in_nl()

    la = HeapSet()
    la.insert(A[0])

    min_sect = []
    ind_l = 0
    ind_r = 1
    pre_min = A[0]
    for i in range(N):
        min_a = la.get_min()
        if pre_min == min_a:
            ind_r = i + 1
        else:
            min_sect.append((pre_min, ind_l, ind_r))
            ind_l = i
            ind_r = i + 1

        pre_min = min_a
        if i + 1 < N:
            la.insert(A[i + 1])
    else:
        min_sect.append((min_a, ind_l, ind_r))

    max_benefit = -1
    ans = 0
    for min_a, l, r in min_sect:
        min_a = min(A[l:r])
        max_a = max(A[l:r])
        min_cnt = A[l:r].count(min_a)
        max_cnt = A[l:r].count(max_a)
        if max_a - min_a > max_benefit:
            max_benefit = max_a - min_a
            ans = min(max_cnt, min_cnt)
        elif max_a - min_a == max_benefit:
            ans += min(max_cnt, min_cnt)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
a=list(map(int,input().split()))
m=10**18
d={}
for i in a:
  if i<m:m=i
  if d.get(i-m):d[i-m]+=1
  else:d[i-m]=1
print(d[max(d)])

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
aas = list(map(int, input().split()))
maxs = [0]*n
max_a = aas[-1]
for i in reversed(range(n)):
    maxs[i] = max_a
    if aas[i] > max_a:
        max_a = aas[i]
difs = [0]*n
for i in range(n):
    difs[i] = maxs[i] - aas[i]
print(difs.count(max(difs)))

Submission status: Accepted

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

c = 0
MAX = 0
a1 = A[0]
for i in range(N-1):
    if A[i] <= a1:
        a1 = A[i]
    a2 = A[i+1]
    if MAX == a2 - a1:
        c += 1
    elif MAX < a2 - a1:
        MAX = a2 - a1
        c = 1
print(c)

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
A = list(map(int, input().split()))

lo = 10**18
hi = 0
d = 0
res = 0


for i in range(N):
    if lo > A[i]:
        lo = A[i]
        hi = 0
    if hi < A[i]:
        hi = A[i]
        if d == hi - lo:
            res += 1
        elif d < hi - lo:
            d = hi - lo
            res = 1

print(res)

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
a=list(map(int,input().split()))
min=[float("inf")]*n
min[0]=a[0]
m=0
for i in range(1,n):
    if min[i-1]>a[i]:
        min[i]=a[i]
    else:
        min[i]=min[i-1]
for i in range(n):
    r=a[i]-min[i]
    m=max(r,m)
ans=0
for i in range(n):
    r=a[i]-min[i]
    if r==m:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N, T = NMI()
    A = NLI()

    if N == 1:
        print(1)
        exit()

    A_min = []
    A_min.append(A[0])
    A_min.append(min(A[0], A[1]))
    for i, a in enumerate(A):
        if i < 2:
            continue
        A_min.append(min(A_min[-1], a))

    diff = [0] * N
    for i, am in enumerate(A_min):
        if i == 0:
            diff[0] = 0
            continue
        diff[i] = max(A[i] - A_min[i-1], 0)
    diff_max = max(diff)
    print(diff.count(diff_max))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, T, *A = map(int, read().split())

    B = [0] * N
    B[0] = INF
    for i in range(N - 1):
        if B[i] > A[i]:
            B[i + 1] = A[i]
        else:
            B[i + 1] = B[i]

    p = 0
    C = [0] * N
    for i in range(N):
        C[i] = A[i] - B[i]
        if p < C[i]:
            p = C[i]

    ans = C.count(p)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,T=map(int,input().split())
A=list(map(int,input().split()))

d = 0
cnt = 0
min_a = A[0]
for i in range(1, N):
  a = A[i]
  if min_a > a:
    min_a = a
    continue
 
  if d < a - min_a:
    d = a - min_a
    cnt = 1
  elif d == a - min_a:
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
N,T=map(int,input().split())
A=list(map(int,input().split()))
ans=0
max_=0
B=[0]*N
for i in reversed(range(N)):
    B[i]=max(B[i],max_-A[i])
    max_=max(max_,A[i])
m=max(B)
print(B.count(m))

Submission status: Accepted

Solution code:
class SEGTree:
    def __init__(self,n):
        self.Unit=0
        i=1
        while(i<n):
            i*=2
        self.SEG=[self.Unit]*(2*i-1)
        self.d=i
    def update(self,i,x):
        i+=self.d-1
        self.SEG[i]=x
        while i>0:
            i=(i-1)//2
            self.SEG[i]=max(self.SEG[i*2+1],self.SEG[i*2+2])
    def find(self,a,b,k,l,r):
        if r<=a or b<=l:
            return self.Unit
        if a<=l and r<=b:
            return self.SEG[k]
        else:
            c1=self.find(a,b,2*k+1,l,(l+r)//2)
            c2=self.find(a,b,2*k+2,(l+r)//2,r)
            return max(c1,c2)
    def get(self,a,b):
        return self.find(a,b,0,0,self.d)

N,T=map(int,input().split())
A=list(map(int,input().split()))
SEG=SEGTree(N)
for i in range(N):
    SEG.update(i,A[i])
li=[0]*N
for i in range(N):
    li[i]=max(li[i],SEG.get(i+1,N)-A[i])
m=max(li)
ans=0
for i in range(N):
    if li[i]==m:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり

N,T = LI()
A = LI()

m = A[-1]
ans = []
for i in range(N-2,-1,-1):
    ans.append(m-A[i])
    m = max(m,A[i])

print(ans.count(max(ans)))

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
a=list(map(int,input().split()))
accum=[0]*n
accum[n-1]=a[n-1]
for i in reversed(range(1,n)):
    accum[i-1]=max(accum[i],a[i-1])
maxi=0
for i in range(n-1):
    maxi=max(maxi,accum[i+1]-a[i])
ans=0
d={}
for i in range(n):
    if a[i]-maxi>0:
        if a[i]-maxi in d:
            if d[a[i]-maxi]>0:
                d[a[i]-maxi]-=1
                ans+=1
    if a[i] not in d:
        d[a[i]]=1
    else:
        d[a[i]]+=1
print(ans)

Submission status: Accepted

Solution code:
INF = 10**10
n,t = map(int,input().split())
A = list(map(int,input().split()))

ans = 0
cost = 0
l = 0
r = 1
while r<n:    
  dif = A[r] - A[l]
  if dif == cost:
    ans += 1
  elif dif > cost :
    cost = dif
    ans = 1 
  else:
    pass
  if dif <= 0:
    l = r
  r += 1
  
print(ans)

Submission status: Accepted

Solution code:
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
n,k=MI()
lis=LI()
mx=[0 for i in range(n)]
mx[-1]=lis[-1]
for i in range(n-1):
    j=n-i-1
    mx[j-1]=max(mx[j],lis[j-1])
#print(mx)
difmx=0

for i in range(n):
    if mx[i]-lis[i]>difmx:
        difmx=mx[i]-lis[i]
#print(lis)
#print(difmx)
new_mx=[[0,0] for i in range(n)]
new_mx[-1]=[lis[-1],1]
count=0
for i in range(n-1):
    j=n-i-1
    #print(new_mx)
    #print(count)
    new_mx[j-1]=new_mx[j]
    if new_mx[j][0]-lis[j-1]==difmx:
        count+=new_mx[j][1]
    if new_mx[j][0]==lis[j-1]:
        new_mx[j][1]+=1
    if new_mx[j][0]<lis[j-1]:
        new_mx[j-1][0]=lis[j-1]
        new_mx[j-1][1]=1
print(count)

Submission status: Accepted

Solution code:
n , t = map(int,input().split())
a = list(map(int,input().split()))
k = [0 for i in range(n)]
nowmin = a[0]
for i in range(1,n):
    k[i] = a[i] - nowmin
    nowmin = min(nowmin,a[i])

maxri = max(k)
ans = 0
for i in range(n):
    if k[i] == maxri:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys

def main(apples,ln):
    minp=apples[0]
    profit=[0,0]
    for x in range(1,n):
        minp=min(apples[x],minp)
        if profit[1]<apples[x]-minp:
            profit[0]=1
            profit[1]=apples[x]-minp
        elif profit[1]==apples[x]-minp:
            profit[0]+=1
    return profit[0]

n,t=map(int, sys.stdin.readline().strip().split())
apples=list(map(int,sys.stdin.readline().strip().split()))

print(main(apples,n))

Submission status: Accepted

Solution code:
n,T=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*n
mn=10**18
for i in range(1,n):
  mn=min(mn,a[i-1])
  b[i]=a[i]-mn
# print(b)
from collections import Counter
c=Counter(b)
ans=0
for k,v in sorted(c.items(),reverse=1):
  ans+=v
  break
print(ans)

Submission status: Accepted

Solution code:
N, T = map(int,input().split())
AN = list(map(int,input().split()))

maximum = 0
ans = 0
left = AN[0]

for n in range(1,N):
  if AN[n]-left == maximum:
    ans += 1
  elif AN[n]-left > maximum:
    maximum = AN[n]-left
    ans = 1

  left = min(left,AN[n])

print(ans)

Submission status: Accepted

Solution code:
n,T = map(int,input().split())

a = list(map(int,input().split()))
maxDiff = 0
curMin = a[0]
for i in range(n):
  if curMin > a[i]:
    curMin = a[i]
  if maxDiff < a[i] - curMin:
    maxDiff = a[i] - curMin

isCovered = False
count = 0

curMin = a[0]

for i in range(n):
  if curMin > a[i]:
    curMin = a[i]
    isCovered = False
  if maxDiff == a[i] - curMin:
    if not isCovered:
      count += 1
      isCovered = True

print(count)

Submission status: Accepted

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

max1 = [0] * n
max1[-1] = a[-1]

for i in range(1, n):
    max1[-i-1] = max(max1[-i], a[-i-1])

ans = 0
tmp = -1

for i in range(n):
    if max1[i] - a[i] > tmp:
        tmp = max1[i] - a[i]
        ans = 1
    elif max1[i] - a[i] == tmp:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    n,t=map(int,input().split())
    A=list(map(int,input().split()))
    mlist=[0]*n
    mlist[0]=A[0]
    for i in range(1,n):
        if A[i]<mlist[i-1]:
            mlist[i]=A[i]
        else: mlist[i]=mlist[i-1]
    val=[0]*n
    for i in range(n):
        val[i]=A[i]-mlist[i]

    print(val.count(max(val)))

    
resolve()

Submission status: Accepted

Solution code:
N,T = map(int,input().split())
A = list(map(int,input().split()))
B = [0 for _ in range(N)]
amax = A[-1]
for i in range(N-2,-1,-1):
    a = A[i]
    if a>amax:
        B[i] = 0
        amax = a
    else:
        B[i] = amax-a
bmax = 0
amax = 0
for i in range(N):
    b = B[i]
    if b>bmax:
        amax = A[i]+b
        bmax = b
Ind = []
for i in range(N):
    if B[i]==bmax:
        Ind.append(i)
cnt = 0
cur = N-1
for i in range(len(Ind)-1,-1,-1):
    j = Ind[i]
    amax = A[j]+B[j]
    for k in range(cur,j,-1):
        if A[k]==amax:
            cnt += 1
    cur = j
print(cnt)

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
A = list(map(int, input().split()))
#全て同じではない

#buy = int(T / 2)
#一番安い町で買って、高い町で売る
now = 10 ** 10
max_sub = 0
count = 1
for i in range(N):
  bb = A[i] - now
  if bb > max_sub:
    max_sub = bb
    count = 1
  elif bb == max_sub:
    count += 1

  now = min(now, A[i])
  #print(bb, max_sub, now, count)
  
#print(max_sub, count)
print(count)

Submission status: Accepted

Solution code:
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 unite(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 isSame(self, x, y):
        return self.find(x) == self.find(y)

def solve():
    N, T = map(int,input().split())
    A = list(map(int,input().split()))

    uf = UnionFind(N)

    mn_idx_list = []
    mn = float('inf')
    max_diff = 0
    for i in range(N):
        mn = min(mn, A[i])
        if mn == A[i]:
            mn_idx_list.append(i)
        else:
            mn_idx_list.append(mn_idx_list[-1])

        max_diff = max(max_diff, A[i] - mn)
    
    for i in range(N):
        if A[i] - A[mn_idx_list[i]] == max_diff:
            uf.unite(i, mn_idx_list[i])
    
    parents_set = set()
    for i in range(N):
        if uf.size(i) == 1:
            continue
        parents_set.add(uf.find(i))
    
    print(len(parents_set))

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# D - 高橋君と見えざる手
# https://atcoder.jp/contests/abc047/tasks/arc063_b

from collections import Counter
N,T=map(int,input().split())
A=list(map(int,input().split()))

mini=10**9
maxi=0
m = [0]*N
M = [0]*N
z = [0]*N
for i in range(N):
    mini=min(mini,A[i])
    maxi=max(maxi,A[i])
    m[i]= mini
    M[i]= maxi
    z[i]= A[i]-mini
k=Counter(z)
l=sorted(k,reverse=True)
#print(l)
print(k[l[0]])

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, t = map(int, readline().split())
    a = list(map(int, readline().split()))

    cum_max = [0] * n
    cum_max[-1] = a[-1]

    for i in range(n - 2, -1, -1):
        cum_max[i] = max(cum_max[i + 1], a[i])

    profit = [0] * n
    for i in range(n-1):
        profit[i] = cum_max[i+1] - a[i]

    print(profit.count(max(profit)))


if __name__ == '__main__':
    main()