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:


N,T = map(int, input().split())
As = list(map(int, input().split()))
AsR = As[::-1]
mxs = []
mx = 0
ind = 0
for i, a in enumerate(AsR):
    if a > mx:
        mx = a
        ind = N-i-1
    mxs.append((mx,ind))
pairs = []
for a,mx in zip(As,mxs[::-1]):
    # print(mx[0]-a,mx[1])
    pairs.append((mx[0]-a,mx[1]))
pairs.sort(reverse=True)
mx = pairs[0][0]
from collections import defaultdict
ans = set()
for x, i in pairs:
    if x != mx:break
    ans.add(i)
print(len(ans))

Submission status: Accepted

Solution code:
N,T=map(int,input().split())
A=list(map(int,input().split()))
t=0
k=A[0]
c=0
for i in A:
    if i<k:
        k=i
    elif i-k>t:
        t=i-k
        c=1
    elif i-k==t:
        c+=1
print(c)

Submission status: Accepted

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

minA = A[0]
maxG = 0
ans = 0
for i in range(1,N) :
    if A[i] < minA :
        minA = A[i]
    else :
        if A[i] - minA > maxG :
            maxG = A[i] - minA
            ans = 1
        elif A[i] - minA == maxG :
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc047/tasks/arc063_b
#
# 高橋くんが利益を上げる状況は街iで安く仕入れてi+jで高く売ること
# min と maxの差が最大になる点がいくつ存在するかをカウントすれば良い
#


import sys
from collections import Counter
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


N, T = read_ints()
A = read_ints()


# minの点を覚えておく
# 各点に足して差を出す(その点を使うと達成可能な利益)
# minはちゃんと更新
scores = []
mi = 10**9 + 7
for a in A:
    mi = min(mi, a)
    scores.append(a - mi)

ans = Counter(scores)
print(ans[max(scores)])

Submission status: Accepted

Solution code:
import sys
INF = 10**10
MOD = 10**9 + 7
sys.setrecursionlimit(100000000)
from functools import lru_cache
from itertools import permutations

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

    cummin = [0] * n
    cummax = [0] * n
    cummin[0] = a[0]
    cummax[-1] = a[-1]
    for i in range(1,n):
        cummin[i] = min(a[i],cummin[i - 1])
        cummax[- i - 1] = max(a[- i - 1],cummax[-i])
    
    ben = 0   
    for i in range(1,n):
        ben = max(ben,cummax[i] - cummin[i - 1])
    
    cnt = 0
    flag = True
    for i in range(1,n):
        if ben == cummax[i] - cummin[i - 1]:
            if flag:
                cnt += 1
                flag = False
            
        else:
            flag = True
    
    print(cnt)

if __name__=='__main__':
    main()

Submission status: Accepted

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

right = [0] * (N+1)
for i in range(N-1,-1,-1):
    right[i+1] = max(right[i], A[i])
left = [10 ** 9 + 7] * (N+1)
for i in range(N):
    left[i+1] = min(left[i], A[i])

profit = 0
cnt = 0
for i in range(1,N+1):
    if profit == right[i]-left[i]:
        cnt += 1
    elif profit < right[i]-left[i]:
        cnt = 1
        profit = right[i]-left[i]

print(cnt)

Submission status: Accepted

Solution code:
# 高橋くんと見えざる手
n, t = map(int, input().split())
a = list(map(int, input().split()))

max_value = [[0, n] for i in range(n + 1)]

# max_value[i]=後ろからi番目までの値で最も大きい値
for i in range(1, n + 1):
    num = a[-i]
    if max_value[i - 1][0] < num:
        max_value[i][0] = num
        max_value[i][1] = n + 1 - i
    else:
        max_value[i] = max_value[i - 1]


min_value = [10**10 for i in range(n + 1)]
# min_value[i]=初めからi番目までの値で最も小さい値
for _ in range(n):
    x = a[_]
    min_value[_ + 1] = min(min_value[_], x)

max_dif = 0
for i in range(1, n + 1):
    if max_value[i][0] > min_value[n - i]:
        max_dif = max(max_dif, max_value[i][0] - min_value[n - i])


disk = []
for i in range(1, n + 1):
    base = a[i - 1]
    if base + max_dif == max_value[n - i][0]:
        disk.append((i, max_value[n - i][1]))

######


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


uni = UnionFind(n)
for some in disk:
    a, b = some[0], some[1]
    uni.union(a, b)
for i in range(n + 1):
    uni.find(i)

# UnionFind

group = {i: [] for i in range(n + 1)}

for i in range(n + 1):
    group[uni.par[i]].append(i)
ans = 0
for i in range(n + 1):
    L = len(group[i])
    ans += L * (L - 1) // 2
print(ans)

Submission status: Accepted

Solution code:

# 高橋くんと見えざる手
n, t = map(int, input().split())
a = list(map(int, input().split()))

max_value = [[0, n] for i in range(n + 1)]

# max_value[i]=後ろからi番目までの値で最も大きい値
for i in range(1, n + 1):
    num = a[-i]
    if max_value[i - 1][0] < num:
        max_value[i][0] = num
        max_value[i][1] = n + 1 - i
    else:
        max_value[i] = max_value[i - 1]


min_value = [10**10 for i in range(n + 1)]
# min_value[i]=初めからi番目までの値で最も小さい値
for _ in range(n):
    x = a[_]
    min_value[_ + 1] = min(min_value[_], x)

max_dif = 0
for i in range(1, n + 1):
    if max_value[i][0] > min_value[n - i]:
        max_dif = max(max_dif, max_value[i][0] - min_value[n - i])


disk = []
for i in range(1, n + 1):
    base = a[i - 1]
    if base + max_dif == max_value[n - i][0]:
        disk.append((i, max_value[n - i][1]))

######


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


uni = UnionFind(n)
for some in disk:
    a, b = some[0], some[1]
    uni.union(a, b)
for i in range(n + 1):
    uni.find(i)

# UnionFind

group = {i: [] for i in range(n + 1)}

for i in range(n + 1):
    group[uni.par[i]].append(i)
ans = 0
for i in range(n + 1):
    L = len(group[i])
    ans += L * (L - 1) // 2
print(ans)

Submission status: Accepted

Solution code:
n,t = map(int,input().split())
a = list(map(int,input().split()))
md = 0
c = 0
m = a[-1]
for i in range(2,n+1):
  if m-a[-i] > md:
    md = m-a[-i]
    c = 1
  elif m-a[-i] == md:
    c += 1
  m = max(m,a[-i])
print(c)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,T=MI()
    A=LI()
    M=[0]*N#自分以降の最大値
    M[-1]=A[-1]
    for i in range(1,N):
        M[-1-i]=max(M[-i],A[-i])
        
    diff=[0]*N
    for i in range(N):
        diff[i]=M[i]-A[i]
     
    diff.sort(reverse = True)
    ans=diff.count(diff[0])
    print(ans)

main()

Submission status: Accepted

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

cmax = [0] * (n+1)
for i in reversed(range(n)):
    cmax[i] = max(a[i], cmax[i+1])

m = 10 ** 18
d = 0
ans = 0
for i in range(n):
    if d == cmax[i+1] - a[i]:
        ans += 1
    elif d < cmax[i+1] - a[i]:
        d = cmax[i+1] - a[i]
        ans = 1
print(ans)

Submission status: Accepted

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

dict = {"tmpmin":A[0],"maxvalue":0,"pairs":0}
for i in range(1,n):
    if A[i] < dict["tmpmin"]:
        dict["tmpmin"] = A[i]
    else:
        if A[i] - dict["tmpmin"] > dict["maxvalue"]:
            dict["maxvalue"] = A[i] - dict["tmpmin"]
            dict["pairs"] = 1
        elif A[i] - dict["tmpmin"] == dict["maxvalue"]:
            dict["pairs"] += 1
print(dict["pairs"])

Submission status: Accepted

Solution code:
N, T = map(int,input().split())
A = list(map(int,input().split()))
m = [0]*N
M = [0]*N
m[0] = A[0]
for k in range(1,N):
    m[k] = min(A[k],m[k-1])

M[-1] = A[-1]
for k in range(N-2,-1,-1):
    M[k] = max(A[k],M[k+1])

D = [M[k]-m[k] for k in range(N)]
P = max(D)

ans = 0
for k in range(N):
    if A[k]-m[k] == P:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline

n,t = map(int,readline().split())
lst1 = list(map(int,readline().split()))
counter = []
mn = 10**9+1

for i in range(n):
    if lst1[i] < mn:
        mn = lst1[i]
    if mn < lst1[i]:
        counter.append(lst1[i]-mn)

from collections import Counter
c = Counter(counter)

ans = 0
mx = 0
for key,value in c.items():
    if key > mx:
        ans = value
        mx = key

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N, T = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

ans = [-1, 0] # [最大の差, 個数]
max_right = -1

for i in range(N - 2, -1, -1):
    if A[i + 1] > max_right:
        max_right = A[i + 1]
    left = A[i]
    if max_right - left > ans[0]:
        ans = [max_right - left, 1]
    elif max_right - left == ans[0]:
        ans[1] += 1

print(ans[1])

Submission status: Accepted

Solution code:
"""from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd"""

N,T=map(int,input().split())
A=list(map(int,input().split()))
ruimin=[A[0]]
ruimax=[A[-1]]
for i in range(1,N):
    ruimin.append(min(ruimin[-1],A[i]))
for i in range(0,N-1)[::-1]:
    ruimax.append(max(ruimax[-1],A[i]))
ruimax.reverse()

value=1
lst=[]
for i in range(0,N-1):
    if ruimax[i+1]-A[i]==value:
        lst.append(i)
    elif ruimax[i+1]-A[i]>value:
        value=ruimax[i+1]-ruimin[i]
        lst=[i]

print(len(lst))

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
a = list(map(int, input().split()))
INF = 10 ** 9 + 1

profit = [0] * n
buy_mn = INF
for i, e in enumerate(a):
    profit[i] = e - buy_mn
    buy_mn = min(buy_mn, e)

profit_mx = max(profit)
ans = profit.count(profit_mx)
print(ans)

Submission status: Accepted

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

profit = [0] * n
buy_mn = a[0]
for i, e in enumerate(a[1:], 1):
    profit[i] = e - buy_mn
    buy_mn = min(buy_mn, e)

profit_mx = max(profit)
ans = profit.count(profit_mx)
print(ans)

Submission status: Accepted

Solution code:
N, T = map(int,input().split())
price = list(map(int,input().split()))
benefit = [0] * N
ans = 1
pr_low = float("inf")
be_max = 0
for i in range(N):
    if pr_low > price[i]:
        pr_low = price[i]
    else:
        benefit[i] = price[i] - pr_low
        if be_max < benefit[i]:
            be_max = benefit[i]
            ans = 1
        elif be_max == benefit[i]:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall

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()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

lis = []
tmp = A[0]
tmp_max = 0
for i in range(N):
	if tmp > A[i]:
		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 = list(map(int, input().split()))

ans = []
min_a = a[0]
for i in range(1, n):
    min_a = min(min_a, a[i-1])
    ans.append(a[i] - min_a)
r = max(ans)
print(ans.count(r))

Submission status: Accepted

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

m = 10**9+1
s = 0
c = 0
for a in A:
    m = min(m, a)
    if a - m > s:
        s = a - m
        c = 1
    elif a - m == s:
        c += 1

print(c)

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
a = list(map(int, input().split()))
max_value = [0] * n #a[i]以降の最大値
max_value[-1] = a[-1]
for i in range(-2, -n - 1, -1):
    max_value[i] = max(max_value[i + 1], a[i])
max_profit = 0
for i in range(n - 1):
    max_profit = max(max_profit, max_value[i] - a[i])
ans = 0
for i in range(n - 1):
    if max_value[i] - a[i] == max_profit: ans += 1
print(ans)

Submission status: Accepted

Solution code:
N,T=map(int,input().split())
A=tuple(map(int,input().split()))
x=[0]*N
x[0]=A[0]
for i in range(1,N):
  x[i]=min(x[i-1],A[i])
B={}
for i in range(1,N):
  tmp=A[i]-x[i-1]
  if tmp<0:
    continue
  if tmp not in B:
    B[tmp]=1
  else:
    B[tmp]+=1
d=list(B.items())
d.sort(reverse=True)
print(d[0][1])

Submission status: Accepted

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

profit = -1
low = a[0]
for i in range(n):
    if a[i] < low:
        low = a[i]
    x = 0 if low > a[i] else a[i] - low
    if profit < x:
        profit = x

ans = 0
low = a[0]
for i in range(n):
    diff = a[i] - low
    if diff == profit:
        ans += 1
    if a[i] < low:
        low = a[i]

print(ans)

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
a=list(map(int,input().split()))
x=[0]*n
x[0]=a[0]
for i in range(1,n):
    x[i]=min(x[i-1],a[i])
d=dict()
for i in range(1,n):
    y=a[i]-x[i-1]
    if y>0:
        if y in d:
            d[y]+=1
        else:
            d[y]=1
d=list(d.items())
d.sort(reverse=True)
print(d[0][1])

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
A = list(map(int, input().split()))
max_prof = 0
cnt = 0
max_after = [0] * N
max_after[-1] = A[-1]
for i in range(N-1)[::-1]:
    max_after[i] = max(max_after[i+1], A[i])
for i in range(N-1):
    prof = max_after[i+1] - A[i]
    if prof == max_prof:
        cnt += 1
    elif prof > max_prof:
        max_prof = prof
        cnt = 1
print(cnt)

Submission status: Accepted

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LMI(): return list(map(int, input().split()))
MOD = 10 ** 9 + 7

N, T = MI()
A = LMI()

# m[i] := i以降で最大の値
m = [0] * N
for j, a in enumerate(reversed(A)):
    if j == 0:
        m[N - 1] = a
        continue
    i = N - j - 1
    m[i] = max(a, m[i + 1])

M = 0
M_cnt = 0
for i, a in enumerate(A):
    if i == N - 1:
        continue
    diff = m[i + 1] - a
    if diff > M:
        M = diff
        M_cnt = 1
    elif diff == M:
        M_cnt += 1
print(M_cnt)

Submission status: Accepted

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

min_a = A[0]
diff = 0
count = 0

for i in range(1, len(A)):
    if A[i] <= min_a:
        min_a = A[i]
    elif A[i] - min_a == diff:
        count += 1
    elif A[i] - min_a > diff:
        diff = A[i] - min_a
        count = 1

print(count)

Submission status: Accepted

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

max_d = 0
max_count = 0

current_min = 999999999
current_max = 0

for a in A:
    if a < current_min:
        current_min = a
        current_max = 0
        if max_d < (current_max - current_min):
            max_d = current_max - current_min
            max_count = 1
        elif max_d == (current_max - current_min):
            max_count += 1

    if a > current_max:
        current_max = a
        if max_d < (current_max - current_min):
            max_d = current_max - current_min
            max_count = 1
        elif max_d == (current_max - current_min):
            max_count += 1

print(max_count)

Submission status: Accepted

Solution code:
def main():
    import sys 
    input = sys.stdin.readline
    n,t = map(int,input().split())
    l = list(map(int,input().split()))
    ans = []
    check = l[0]
    for i in l:
        if i < check:
            check = i
        else:
            dis = i-check
            ans.append(dis)
    print(ans.count(max(ans)))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,t = map(int,input().split())
l = list(map(int,input().split()))
ans = []

check = l[0]
dis = 0
for i in l:
    if i < check:
        check = i
        dis = 0
    
    else:
        dis = i-check
        ans.append(dis)
print(ans.count(max(ans)))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N,T=map(int,input().split())
A=list(map(int,input().split()))

buy=10**9
buy_loc=[]
benefit=0
for index,item in enumerate(A):
    if buy>item:
        buy=item
        buy_loc.append(index)
    benefit=max(item-buy,benefit)

buy_loc.append(10**6)
buy_loc=buy_loc[::-1]

ans=0
buy_count=0
sell_count=0
for i in range(len(A)):
    if i==buy_loc[-1]:
        buy_loc.pop(-1)
        buy=A[i]
        #print(buy_count,sell_count)
        ans+=min(buy_count,sell_count)
        buy_count=0
        sell_count=0
    if A[i]==buy:
        buy_count+=1
    if A[i]-buy==benefit:
        sell_count+=1

ans+=min(buy_count,sell_count)
print(ans)
#print(benefit)
#print(buy_loc)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, T: int, A: "List[int]"):

    amin, amin_i = (INF, -1)
    gmax = 0
    cand = []

    for i, a in enumerate(A):
        if a < amin:
            amin = a
            amin_i = i
        else:
            if a-amin > gmax:
                gmax = a-amin
                cand = [(gmax, amin_i, i)]
            elif a-amin == gmax:
                cand.append((gmax, amin_i, i))
    print(len(cand))
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    T = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, T, A)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
A = list(map(int, input().split()))
B = [-1]*n
B[-1] = A[-1]
for i in range(n-2, 0, -1):
  B[i] = max(A[i], B[i+1])
L = [B[i+1]-A[i] for i in range(n-1)]
ans = L.count(max(L))
print(ans)

Submission status: Accepted

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

ma = a[-1]
m = 1
maxsaeki = 0
ans = 0

for i in range(n-2,-1,-1):
    if a[i] > ma:
        ma = a[i]
        m = 1
    elif a[i] < ma:
        if (ma - a[i]) > maxsaeki:
            maxsaeki = ma-a[i]
            ans = 1
        elif (ma - a[i]) == maxsaeki:
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
#14:18
n,t = map(int,input().split())
a = list(map(int,input().split()))
b = [0]
for i in range(n)[::-1]:
  b.append(max(b[-1],a[i]))
b.reverse()
b.pop()
#print(b)
c = []
for i in range(n):
  c.append(b[i]-a[i])
c.sort(reverse=True)
#print(c)
cnt = 0
for i in range(n):
  if c[0] == c[i]:
    cnt += 1
  else:
    print(cnt)
    break

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

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

diff = -1
num = 0
high = 0
for i in range(N)[::-1]:
    high = max(high,A[i])
    if high-A[i] > diff:
        diff = high-A[i]
        num = 1
    elif high-A[i] == diff:
        num += 1
    else:
        continue

print(num)

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
A = [int(c) for c in input().split()]
flag = True
ls = []
st = A[-1]
for i in range(N-2,-1,-1):
  if st-A[i]>0:
    ls += [st-A[i]]
  else:
    st = A[i]
ls.sort()
ans = ls.count(ls[-1])
print(ans)

Submission status: Accepted

Solution code:
N,T = map(int,input().split())
List = list(map(int,input().split()))
dic = {}
min = List[0]
for i in range(N):
  if List[i]<min:
    min = List[i]
  if List[i]-min not in dic:
    dic[List[i]-min] = 1
  else:
    dic[List[i]-min] += 1
dic2 = sorted(dic.items(), reverse=True)
Answer = dic2[0][1]
print(Answer)

Submission status: Accepted

Solution code:
n,t=map(int,input().split())
l=list(map(int,input().split()))
mi=l[0]
ri=0
t=0
ans=0
for i in l[1:]:
    mi=min(mi,i)
    ma=i 
    if ri<ma-mi:
        ri=ma-mi
        ans=1
    elif ri==ma-mi:
        if t:
            ans+=1
            t=0
    else:
        t=1
print(ans)

Submission status: Accepted

Solution code:
n, k, *a = map(int, open(0).read().split())

mn = 1e18
ans, got = 1, 0
for i in range(n):
    if(mn>a[i]): mn = a[i]
    elif got==a[i]-mn: ans += 1
    elif got<a[i]-mn:
        got = a[i]-mn
        ans = 1
print(ans)

Submission status: Accepted

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

mx = 0
b = []
for i in a[::-1]:
    if mx < i:
        mx = i
    b.append(mx-i if i < mx else 0)
c = Counter(b)

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

Submission status: Accepted

Solution code:
from itertools import accumulate
from collections import Counter
N, T = map(int, input().split())
A = list(map(int, input().split()))
B = list(accumulate(A, min))
C = Counter(b-a for a, b in zip(A, B))
print(C[min(C)])

Submission status: Accepted

Solution code:
N,T = map(int,input().split())
A = list(map(int,input().split()))
ar = A[::-1]
maxa = ar[0]
diff = 0
count = 0
for i in ar:
    if maxa < i:
        maxa = i
    else:
        if maxa - i == diff:
            count += 1
        elif maxa - i > diff:
            count = 1
            diff = maxa -i
print(count)

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
a = [int(x) for x in input().split()]

lowest = a[0]
d = []

for i in range(n):
    d.append(a[i]-lowest)
    lowest = min(lowest, a[i])

print(d.count(max(d)))

Submission status: Accepted

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

min_A = 10**10
max_A = 0
profit = []
for i in range(N):
    if A[i] <= min_A:
        profit.append(max_A - min_A)
        min_A = A[i]
        max_A = A[i]
    elif A[i] >= max_A:
        profit.append(max_A - min_A)
        max_A = A[i]
profit.append(max_A - min_A)        
        
max_profit = max(profit)
count = 0
for i in range(len(profit)):
    if profit[i] == max_profit:
        count += 1
        
print(count)

Submission status: Accepted

Solution code:
import sys,heapq
from collections import defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False

def ddprint(x):
  if DBG:
    print(x)

n,t = inm()
a = inl()
heappair = []
hpair = defaultdict(int)
hcnt = defaultdict(int)
hcnt[a[0]] = 1
mn = a[0]
dif = 0
for i in range(1,n):
    z = a[i]
    hcnt[z] += 1
    if z<mn:
        mn = z
    if z-mn>=dif:
        if z-mn>dif:
            dif = z-mn
        e = (-dif,mn,z)
        if e not in hpair:
            hpair[e] = 1
            heapq.heappush(heappair,e)
cnt = 0
v = 1
while len(heappair)>0:
    mdif,mn,z = heapq.heappop(heappair)
    if v==1:
        v = mdif
    if v != mdif:
        break
    cnt += min(hcnt[mn],hcnt[z])
print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,t=map(int,input().split())
a=list(map(int,input().split()))
m=a[0]
M=0
for i in range(1,n):
    m=min(m,a[i-1])
    M=max(M,a[i]-m)
m=a[0]
ans=0
for i in range(1,n):
    m=min(m,a[i-1])
    if a[i]-m==M:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
*a, = map(int, input().split())
m = a[0]
p = 0
ans = 0
for i in range(1, N):
    c = a[i] - m
    if a[i] < m:
        m = a[i]
    if c > p:
        p = c
        ans = 1
    elif c == p:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N, T = map(int, input().split())
*a, = map(int, input().split())
t = defaultdict(int)
m = a[0]
p = 0
ans = 0
for i in range(1, N):
    c = a[i] - m
    if t[m] < c:
        t[m] = c
    if a[i] < m:
        m = a[i]
    if c > p:
        p = c
        ans = 1
    elif c == p:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N, T = map(int, input().split())
*a, = map(int, input().split())
t = defaultdict(int)
m = a[0]
p = 0
for i in range(1, N):
    c = a[i] - m
    if t[m] < c:
        t[m] = c
    if a[i] < m:
        m = a[i]
    if c > p:
        p = c

ans = 0
for k, v in t.items():
    if v == p:
        ans += 1

print(ans)

Submission status: Accepted

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


import heapq
import collections
hq=[]
heapq.heapify(hq)
heapq.heappush(hq,A[0])
tmp=0
table=collections.defaultdict(int)
for i in range(1,N):
  j=heapq.heappop(hq)
  table[A[i]-j]+=1
  tmp=max(tmp,A[i]-j)
  heapq.heappush(hq,j)
  heapq.heappush(hq,A[i])
print(table[tmp])

Submission status: Accepted

Solution code:
def solve():
    N,T = map(int, input().split())
    A = list(map(int, input().split()))
    max_profit = 0
    min_val = 10**9
    max_profit_pair = []
    
    for a in A:
        # print(min_val, max_profit)
        if max_profit < a-min_val:
            max_profit = a-min_val
            max_profit_pair = [(min_val,a)]
        elif max_profit == a-min_val:
            max_profit_pair.append((min_val,a))
            
        if min_val > a:
            min_val = a
            
    # print(max_profit, max_profit_pair)
    print(len(max_profit_pair))
    
solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, T = map(int, input().split(' '))
A = list(map(int, input().split(' ')))

max_diff = 0
max_value = A[-1]
for i in range(N-2, -1, -1):
    if A[i] > max_value:
        max_value = A[i]
        buf = [i]
    elif A[i] == max_value:
        pass
    else:
        max_diff = max(max_diff, max_value - A[i])


indices = set()
buf = [N-1]
max_value = A[-1]
for i in range(N-2, -1, -1):
    if A[i] > max_value:
        max_value = A[i]
        buf = [i]
    elif A[i] == max_value:
        pass
    else:
        if max_value - A[i] == max_diff:
            indices.add(i)

# print(indices)
print(len(indices))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

# 現在の状態での最大利益を求める
mn = INF
base = -INF
for a in A:
    base = max(base, a - mn)
    mn = min(mn, a)

# 最大を作れるペアがあったら潰していく
S = set()
ans = 0
for a in A:
    if a-base in S:
        ans += 1
        S.remove(a-base)
    S.add(a)
print(ans)