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:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**10
mod = 10**9 + 7


def f():
    n,t = list(map(int,input().split()))
    a = list(map(int,input().split()))
    m = 0
    mc = inf
    r = 0
    for c in a:
        if mc > c:
            mc = c
            continue
        if c - mc > m:
            m = c - mc
            r = 1
            continue
        if c - mc == m:
            r += 1
    return r

print(f())

Submission status: Accepted

Solution code:
n, t = map(int, input().split())
a = [int(i) for i in input().split()]
b = [0 for i in range(n)]
m = a[0]
for i in range(n):
    m = min(m, a[i])
    b[i] = a[i] - m
m = b[0]
k = 0
for i in range(n):
    if b[i] >= m:
        m = b[i]
        k = i
ans1 = 1
for i in range(n):
    if b[i] == m and i != k:
        ans1 += 1
b = [0 for i in range(n)]
m = a[n - 1]
for i in range(n - 1, -1, -1):
    m = max(m, a[i])
    b[i] = a[i] - m
m = b[n - 1]
k = 0
for i in range(n - 1, -1, -1):
    if b[i] <= m:
        m = b[i]
        k = i
ans2 = 1
for i in range(n):
    if b[i] == m and i != k:
        ans2 += 1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import numpy as np
N, T = map(int, raw_input().split())
A_list = map(int, raw_input().split())

dif_dict = dict()

A_min = np.minimum.accumulate(A_list)
A_max = np.maximum.accumulate(A_list[::-1])[::-1]

max_d = (A_max - A_min).max()
print len(set(A_min[max_d ==A_max - A_min]))

Submission status: Accepted

Solution code:
# coding: utf-8

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

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

Submission status: Accepted

Solution code:
N, T = map(int, input().split())
A = list(map(int, input().split()))
minv = A[0]
maxv = -1
L = [0]
for i in range(1, N):
    if A[i] - minv > maxv:
        L = [i]
        maxv = A[i] - minv
    elif A[i] - minv == maxv:
        L.append(i)
    minv = min(minv, A[i])
print(len(L))

Submission status: Accepted

Solution code:
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def fib(n): return [(seq.append(seq[i-2] + seq[i-1]), seq[i-2])[1] for seq in [[0, 1]] for i in range(2, n)]
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def eucl(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0
def pscl(num,l=[1]):
    for i in range(num):
        l = map(lambda x,y:x+y,[0]+l,l+[0])
    return l

#n=int(raw_input())
n,t=map(int,raw_input().split())
a=map(int,raw_input().split())
ans=0
chk=1
x=IS
y=0
for i in a:
    if x>i:
        x=i
    elif y<i:
        y=i
    if i-x>chk:
        ans=1
    elif i-x==chk:
        ans+=1
    chk=max(chk,i-x)
print ans

Submission status: Accepted

Solution code:
import copy

n, t = map(int, raw_input().split())
a = map(int, raw_input().split())

minA = copy.copy(a)
maxA = copy.copy(a)
for i in range(n-1):
    minA[i+1] = min(minA[i], a[i+1])

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

T = [maxA[i]-minA[i] for i in range(n)]

maxT = max(T)

ans_list = set()
for i in range(n):
    if T[i] == maxT:
        ans_list.add(minA[i])

print len(ans_list)

Submission status: Accepted

Solution code:
from sys import stdin
from collections import defaultdict
def main():
    n, k = map(int, stdin.readline().split())
    a = map(int, stdin.readline().split())
    mn = 1001001001
    p = 0
    d = defaultdict(int)
    for x in a:
        if mn > x:
            mn = x
        if p < x - mn:
            p = x - mn
        d[x] += 1
    ans = 0
    for x in reversed(a):
        if x not in d:
            continue
        if x - p in d:
            ans += min(d[x], d[x-p])
            d.pop(x-p)
        d[x] -= 1
    print ans
main()

Submission status: Accepted

Solution code:
# def makelist(n, m):
# 	return [[0 for i in range(m)] for j in range(n)]

# n = int(input())
# a, b = map(int, input().split())
# s = input()


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

diff = [0]*N

mini = A[0]
for i in range(1, N):
	now = A[i]
	if now <= mini:
		mini = now
	else:
		diff[i] = now - mini

sa = max(diff)

ans = 0
mini = A[0]
l = 1
r = 0
for i in range(1, N):
	now = A[i]
	if mini == now:
		l += 1
	elif now < mini:
		if r > 0:
			ans += min(l, r)
			r = 0
		mini = now
		l = 1
	else: # now > mini
		if now - mini == sa:
			r += 1

if r > 0:
	ans += min(l, r)
print(ans)

Submission status: Accepted

Solution code:
n,t=map(int,raw_input().split())
a=map(int,raw_input().split())
ma=[a[n-1]]*n
for i in reversed(range(n-1)):
    ma[i]=max(ma[i+1],a[i])
tt=0;cnt=0;u=[]
for i in range(n-1):
    if ma[i+1]-a[i]>=tt:tt=ma[i+1]-a[i]
for i in range(n-1):
    if ma[i+1]-a[i]==tt:u+=[i]
w=max(u)
ans=0
for x in u:
    c=1
    while x+tt in u:c+=2
    ans+=c
print ans

Submission status: Accepted

Solution code:
n,t = [int(i) for i in raw_input().split()]

a = [int(i) for i in raw_input().split()]

good = []

mmm = []

mmm.append(a[n-1])

for i in range(n-1):
    mmm.append(max(mmm[i],a[n-i-2]))

mmm.reverse()

for i in range(len(a)-1):
    good.append(mmm[i+1]-a[i])

m = max(good)

count = 0

for g in good:
    if g==m:count+=1

print count

Submission status: Accepted

Solution code:
n,t=map(int,raw_input().split())
a=map(int,raw_input().split())
now=a[0]
cnt=[]
for i in xrange(1,n):
    if now<a[i]:
        cnt.append(a[i]-now)
    else:
        now=a[i]
print(cnt.count(max(cnt)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from __future__ import division, print_function, absolute_import, unicode_literals

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

maxA = [0] * N
minA = [0] * N

maxA[N-1] = A[-1]
for i in range(1, N):
    maxA[N-i-1] = max(maxA[N-i], A[N-i])
# print(maxA)

max_diff = -10000000000
for i in range(N-1):
    max_diff = max(maxA[i] - A[i], max_diff)
# print(max_diff)
cnt = 0
for i in range(N-1):
    if maxA[i] - A[i] == max_diff:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math,string,itertools,fractions,heapq,collections,re,array,bisect

def s():
    return raw_input().strip()
def n():
    return int(raw_input())
def d():
    return float(raw_input())

def ls():
    return raw_input().strip().split()
def ln():
    return map(int, raw_input().strip().split())
def ld():
    return map(float, raw_input().strip().split())

def fs():
    return [raw_input().strip() for i in xrange(input())]
def fn():
    return [int(raw_input().strip()) for i in xrange(input())]
def fd():
    return [float(raw_input().strip()) for i in xrange(input())]

EPS = 1e-9

#http://www.deqnotes.net/acmicpc/2d_geometry/lines
def dot(a, b):
    return a.real * b.real + a.imag * b.imag

def cross(a, b):
    return a.real * b.imag - a.imag * b.real

def distance(a, b, c):
    if dot(b-a, c-a) <= EPS:
        return abs(c-a)
    if dot(a-b, c-b) <= EPS:
        return abs(c-b)
    return abs(cross(b-a, c-a)/(b-a))

N, T = ln()
A = ln()

count = 0
max_diff = -1
minimum = 10**10
for a in A:
    if a < minimum:
        minimum = a
    diff = a - minimum
    if diff > max_diff:
        max_diff = diff
        count = 1
    elif diff == max_diff:
        count += 1

print count

Submission status: Accepted

Solution code:
import sys

N, T = map(int, sys.stdin.readline().rstrip().split())

As = []
As = list(map(int, sys.stdin.readline().rstrip().split()))

mins = []
m = 1e10
vs = []
for A in As:
    m = min(m, A)
    vs.append(A - m)
max_v = max(vs)
res = 0
for v in vs:
    if v == max_v:
        res += 1
print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
N,T = map(int,input().split())
As = list(map(int,input().split()))

mns = [0] * N
mxs = [0] * N

mns[0] = As[0]
mxs[N-1] = As[N-1]

for i in range(1, N):
    j = N - i - 1
    mns[i] = min(As[i], mns[i-1])
    mxs[j] = max(As[j], mxs[j+1])

# print(mns)
# print(mxs)

mx_diff = -1
pairs = set()
for i in range(1, N):
    diff = mxs[i] - mns[i]
    if diff > mx_diff:
        mx_diff = diff
        pairs.clear()
    if diff >= mx_diff:
        pairs.add((mns[i], mxs[i]))
print(len(pairs))

Submission status: Accepted

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

m = 10**10
M = 0
ans = 1
for x in xs:
    if x < m:
        m = x
    elif x - m == M:
        ans += 1
    elif x - m > M:
        ans = 1
        M = x - m

print(ans)

Submission status: Accepted

Solution code:
N,T = map(int, raw_input().split())
A = map(int, raw_input().split())
x = 10**9
y = 0
ans = 0
for a in A:
    t = a-x
    if t>y:
        y = t
        ans = 0
    if t==y:
        ans += 1
    x = min(x, a)
print ans

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

setrecursionlimit(1000000)

(N, T) = reads()
A = reads()

B = [0] * N

B[0] = A[0]
for i in range(1, N):
  B[i] = min(B[i-1], A[i])

C = [max(A[i] - B[i], 0) for i in range(N)]

M = max(C)
result = sum(c == M for c in C)

print(result)

Submission status: Runtime Error

Solution code:
n,t = map(int,input().split())
a = list(map(int,input().split()))
l = []
count = 0
for i in range(n-1):
    if a[i] <= a[i+1]:
        count = count + a[i+1] - a[i]
    else:
        l.append(count)
        count = 0
l.append(count)
l.remove(0)
d = max(l)
ans = l.count(d)
print(ans)

Submission status: Runtime Error

Solution code:
s=input().split()
l=[]
for i in range(1,len(s)):
  l.append(int(s[i])-int(s[i-1]))
l.sort()
m=l[-1]
for i in range(2,len(s)+1):
  if l[-i]!=m:
    break
print(i-1)

Submission status: Runtime Error

Solution code:
from numpy import *
input()
a=array(input().split())
d=a-minimum.accumulate(a)
print((d==d.max()).sum())

Submission status: Runtime Error

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

ans = 0
max_diff = 0
min_a = A[0]
for a in A:
    min_a = min(min_a, a)
    if (a - min_a) == max_diff:
        ans += 1
    elif (a - min_a) > max_diff:
        ans = 1
        max_diff = (a - min_a)
print(ans)

Submission status: Runtime Error

Solution code:
N, T = map(int, raw_input().split())
A_n = map(int, raw_input().split())

min_element = min(A_n[0], A_n[1])
for i in xrange(1, len(A_n)):
    if A_n[i] - min_element > max_profit:
        max_profit = A_n[i] - min_element
    if A_n[i] < min_element:
        min_element = A_n[i]

count = 0
for j in xrange(0, len(A_n)):
    if (A_n[j] + max_profit) in A_n[j:]:
        count += 1

print count

Submission status: Runtime Error

Solution code:
def main():
    N = input()
    edges = [raw_input() for _ in range(N-1)]
    K = input()
    VP = [raw_input() for _ in range(K)]

    VP = [map(int, _.strip().split()) for _ in VP]
    edges = [map(int, _.strip().split()) for _ in edges]
    # print VP
    # print edges
    scores = {}
    for n_idx, value in VP:
        scores[n_idx] = [value]
    pre_defined = scores.keys()
    # print scores
    def check(value, n1, n2):
        if value == (n1 - n2) or value == (n2 - n1):
            return True
        return False

    for idx in pre_defined:
        for n1, n2 in [l for l in edges if idx in l]:
            v = scores[idx][0]
            n = n1 if idx == n2 else n2
            scores[n] = [_ for _ in scores.get(n, []) if check(_, n1, n2)] + [v - 1, v+1]
    for n1, n2 in edges:
        # print n1, n2
        if scores.get(n1) is None and scores.get(n2) is None:
            pass
        else:
            if scores.get(n1) is None:
                for v in scores.get(n2, []):
                    scores[n1] = [_ for _ in scores.get(n1, []) if check(_, n1, n2)] + [v - 1, v+1]
            elif scores.get(n2) is None:
                for v in scores.get(n1, []):
                    scores[n2] = [_ for _ in scores.get(n2, []) if check(_, n1, n2)] + [v - 1, v+1]

        # print scores.get(n1), scores.get(n2) 
        # print "----------"

    # decide step
    adding = []
    for n in range(1, N+1):
        v = scores.get(n, [None])[0]
        adding.append(v)
    if all([_ is not None for _ in adding]):
        print "Yes"
        for a in adding:
            print a
    else:
        print "No"


    # print scores


if __name__ == '__main__':
    main()