Problem:


Score : 300 points

Problem Statement

You are given an array A of length N.
Your task is to divide it into several contiguous subarrays.
Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.
At least how many subarrays do you need to divide A into?

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3



Sample input:
6
1 2 3 2 2 1


Sample output:
2

Submission status: Runtime Error

Solution code:
import numpy as np

N, L, T = map(int, input().split())
xw_list = []
for n in range(N):
    xw_list.append(list(map(int, input().split())))

dic = {k : v for k, v in enumerate(xw_list)}
sort_dic = sorted(dic.items(), key=lambda x:x[1])
    
# 1 : 時計回り、2 : 反時計回り
for t in range(T):
    for n in range(N):
        t_xw = sort_dic[n][1]    # 対象アリ
        # 移動
        if t_xw[1]==1:
            t_xw[0] += 1
        else:
            t_xw[0] -= 1
        # 周回した時の変換
        if t_xw[0]<0:
            t_xw[0] = L + t_xw[0]
        elif t_xw[0]>L:
            t_xw[0] = t_xw[0] - L
    
    # 衝突
    for n in range(N):
        # 座標前後のみで検索
        t_xw = sort_dic[n][1]    # 対象アリ
        if n==0:
            next_xw = sort_dic[-1][1]    # 前のアリ
#             ato_xw = sort_dic[n+1][1]    # 後のアリ
        elif n==N-1:
            next_xw = sort_dic[n-1][1]    # 前のアリ
#             ato_xw = sort_dic[0][1]    # 後のアリ
        else:    
            next_xw = sort_dic[n-1][1]    # 前のアリ
#             ato_xw = sort_dic[n+1][1]    # 後のアリ
        
#         for next_xw in [mae_xw]:
#                         , ato_xw]:
        # １秒後に同じ場所　→　方向転換
        if t_xw[0]==next_xw[0]:
            t_xw[1] = next_xw[1]
            next_xw[1] = t_xw[1]
        # 0.5秒後にすれ違う
        if np.abs(t_xw[0]-next_xw[0])==1 and t_xw[1]!=next_xw[1]:
            if (t_xw[0]>next_xw[0] or (t_xw[0]==0 and next_xw[0]==L-1)) and t_xw[1]==1:
                t_xw[0] -= 1
                t_xw[1] = 2
                next_xw[0] += 1
                next_xw[1] = 1
            elif (next_xw[0]>t_xw[0] or (next_xw[0]==0 and t_xw[0]==L-1)) and next_xw[1]==1:
                t_xw[0] += 1
                t_xw[1] = 1
                next_xw[0] -= 1
                next_xw[1] = 2

for tup in sorted(sort_dic):
    print(tup[1][0])

Submission status: Runtime Error

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

cnt = 0
larger = 1
smaller = -1

d = [0] * (n - 1)
if (L[0] > L[1]):
    d[0] = -1
elif (L[0] < L[1]):
    d[0] = 1

cnt = 1
for i in range(n):
    if (i == 0):
        continue
    elif (i == n - 1):
        break
    else:
        if (L[i] < L[i + 1] and d[i - 1] >= 0):
            d[i] = 1
        elif (L[i] > L[i + 1] and d[i - 1] <= 0):
            d[i] = -1
        elif (L[i] < L[i + 1] and d[i - 1] < 0):
            cnt += 1
        elif (L[i] > L[i + 1] and d[i - 1] > 0):
            cnt += 1
        elif (L[i] == L[i + 1]):
            continue

print(cnt)

Submission status: Runtime Error

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

count = 1

for i in range(1,N-1):
  if A[i-1] > A[i]:
    up,down = 0,1
  elif A[i-1] < A[i]:
    up,down = 1,0
  if up == 1 and A[i] > A[i+1]:
    count += 1
  elif down == 1 and A[i] < A[i+1]:
    count += 1
    
print(count)

Submission status: Runtime Error

Solution code:
import sys

read= sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
MOD = 1000000007
sys.setrecursionlimit(10**7)

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

res = 0

in_i = 0
de_i = 0
i = 0
while i < N-1:
    for j in range(i+1,N):
        if A[j] >= A[j-1]:continue
        else:
            break

    in_i = j
    for j in range(i+1,N):
        if A[j] <= A[j-1]:continue
        else:
            break
    de_i = j

    i = max(de_i,in_i)
    res+=1
#print(res)

if (A[-3] - A[-2])*(A[-2] - A[-1]) >= 0: print(res)
else: print(res+1)

Submission status: Runtime Error

Solution code:
import sys

read= sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
MOD = 1000000007
sys.setrecursionlimit(10**7)

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

res = 0

in_i = 0
de_i = 0
i = 0
while i <= N-1:
    for j in range(i+1,N):
        if A[j] >= A[j-1]:continue
        else:
            break

    in_i = j
    for j in range(i+1,N):
        if A[j] <= A[j-1]:continue
        else:
            break
    de_i = j

    i = max(de_i,in_i)
    res+=1
print(i)

print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
L = list(map(int, input().split()))
cnt = 1
state = None
i = 1
while i < n:
    if state is None:
        if L[i] > L[i - 1]:
            state = 'up'
        elif L[i] < L[i - 1]:
            state = 'down'
        i += 1
    else:
        if state == 'up':
            while L[i] >= L[i - 1]:
                i += 1
                if i == n - 1:
                    print(cnt)
                    exit()
            cnt += 1
            i += 1
            state = None
        else:
            while L[i] <= L[i - 1]:
                i += 1
                if i == n - 1:
                    print(cnt)
                    exit()
            cnt += 1
            i += 1
            state = None
print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input().split())
num_list = [int(i) for i in input().split()]
if len(num_list)==1:
  print(1)
else:
  res = 1
  flag = None
  pre = num_list[0]
  for i in num_list[1:]:
    if pre > i and not flag:
      flag = 'down'
  
    elif pre < i and not flag:
      flag = 'up'
      
    elif (pre > i and flag=='up') or (pre < i and flag=='down'):
      res += 1
      flag = None
    pre = i
  print(res)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

if len(A)==0:
    print(1)
    sys.exit()

A = [A[0]] + [y for x,y in zip(A,A[1:]) if x != y]
sq = np.diff(A)

ans = 1
flg = 0
prev = sq[0]
for i in range(1, len(sq)):
    if flg==1:
        prev = sq[i]
        flg = 0
        continue
    current = sq[i]
    if (prev * current) > 0:
        continue
    else:
        ans += 1
        prev = current
        flg = 1

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

A = [A[0]] + [y for x,y in zip(A,A[1:]) if x != y]
sq = np.diff(A)

ans = 1
flg = 0
prev = sq[0]
for i in range(1, len(sq)):
    if flg==1:
        prev = sq[i]
        flg = 0
        continue
    current = sq[i]
    if (prev * current) > 0:
        continue
    else:
        ans += 1
        prev = current
        flg = 1

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

sq = np.diff(A)

ans = 1
flg = 0
prev = sq[0]
for i in range(1, len(sq)):
    if flg==1:
        prev = sq[i]
        flg = 0
        continue
    current = sq[i]
    if (prev * current) >= 0:
        continue
    else:
        ans += 1
        prev = current
        flg = 1

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(hoge) for hoge in input().split()]
ans = 1
cur = A[0]
Decided = A[0] != A[1]
Flat = A[0] == A[1]
Up = A[0] < A[1]
for n in range(1,N):
    if Decided:
        if (A[n]>=cur) == Up:#数列が続く
            cur = A[n]
        else:
            cur = A[n]
            Up = 1 -  Up
            Decided = False
            ans += 1
    else:
        if A[n] != cur:
            Up = A[n] > cur
            cur = A[n]
            Decided = True
print(ans)

Submission status: Runtime Error

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=qb
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def dq():
	return queue.deque()
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): # root
		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 -1*self.parents[self.find(x)]

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

	def members(self,x): # much time
		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()} # 1~n
def bitArr(n):#ビット全探索
	x = 1
	zero = "0"*n
	ans = []
	ans.append([0]*n)
	for i in range(2**n-1):
		ans.append(list(map(lambda x:int(x),list((zero+bin(x)[2:])[-1*n:]))))
		x+=1
	return ans;
def arrsSum(a1,a2):
	for i in range(len(a1)):
		a1[i]+=a2[i]
	return a1
def maxValue(a,b,v):
	v2 = v
	for i in range(v2,-1,-1):
		for j in range(v2//a+1): #j:aの個数
			k = i-a*j
			if k%b==0:
				return i
	return -1

n = readInt()
a = readInts()

ans = 1

i = 0

def plumin(i):
	if a[i+1]-a[i]>0:
		return 1
	elif a[i+1]-a[i]<0:
		return -1
	else:
		return 0

pmarr = []
for i in range(n-1):
	x = plumin(i)
	if x!=0:
		pmarr.append(x)

#print(pmarr)
be = pmarr[0]
for i in range(1,len(pmarr)):
	if pmarr[i]==be:
		continue
	else:
		ans+=1
		if i+1==len(pmarr):
			break
		be = pmarr[i+1]

print(ans)

Submission status: Runtime Error

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=qb
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def dq():
	return queue.deque()
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): # root
		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 -1*self.parents[self.find(x)]

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

	def members(self,x): # much time
		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()} # 1~n
def bitArr(n):#ビット全探索
	x = 1
	zero = "0"*n
	ans = []
	ans.append([0]*n)
	for i in range(2**n-1):
		ans.append(list(map(lambda x:int(x),list((zero+bin(x)[2:])[-1*n:]))))
		x+=1
	return ans;
def arrsSum(a1,a2):
	for i in range(len(a1)):
		a1[i]+=a2[i]
	return a1
def maxValue(a,b,v):
	v2 = v
	for i in range(v2,-1,-1):
		for j in range(v2//a+1): #j:aの個数
			k = i-a*j
			if k%b==0:
				return i
	return -1

n = readInt()
a = readInts()

ans = 1

i = 0

def plumin(i):
	if a[i+1]-a[i]>0:
		return 1
	elif a[i+1]-a[i]<0:
		return -1
	else:
		return 0

pmarr = []
b= 0
for i in range(n-1):
	pmarr.append(plumin(i))
while 0 in pmarr:
	pmarr.remove(0)

#print(pmarr)
be = pmarr[0]
for i in range(1,len(pmarr)):
	if pmarr[i]==be:
		continue
	else:
		ans+=1
		if i+1==len(pmarr):
			break
		be = pmarr[i+1]

print(ans)

Submission status: Runtime Error

Solution code:

import numpy as np

n  = int(input())
a = np.array(list(map(int,input().split())))


ret = 0
i = 0
while i < len(a):
    j = i
    while j < n -1 and a[j] <= a[j+1]:
        j += 1
    h = i
    while h < n - i and a[h] >= a[h +1 ]:
        h += 1
        i = max(h,j) + 1
        ret += 1

print(ret)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))

a_ = A[1]
d_ = np.sign(A[1] - A[0])
turn_p = 0
for a in A[2:]:
    d = np.sign(a - a_)
    #print(a, d, d_, turn_p)
    if d != 0:
        if d_ == 0:
            d_ = d
        elif d_ != d:
            turn_p += 1
            d_ = 0
    a_ = a 

print(turn_p+1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 0
    i = 0
    while 1:
        if A[i] <= A[i+1] <= A[i+2] or A[i] >= A[i+1] >= A[i+2]:
            i += 1
        else:
            ans += 1
            i += 2
        if i > N - 3:
            ans += 1
            break
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
now=a[0]
start=1
while now==a[start]:
    start+=1
if a[0]<a[start]:#増加
    jugh=1
else:#現象
    jugh=2
#print(start,jugh)
i=start
while i<n:
    if jugh==0:
        while now==a[start]:
            start+=1
        if now<a[start]:#増加
            jugh=1
        else:#現象
            jugh=2
    if jugh==1:
        if a[i-1]>a[i]:
            jugh=0
            ans+=1
            now=a[i]
            start=i+1
            #print(a[i-1],a[i])
                
    else:
        if a[i-1]<a[i]:
            jugh=0
            ans+=1
            now=a[i]
            start=i+1
            #print(a[i-1],a[i])
            
    i+=1   

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
def solve(N,A):
    liss = []
    lis = []
    lis.append(A[0])
    a = A[0]
    for i in range(1,N):
        if len(lis)==1 or prev==0:
            if A[i]>a:
                prev = 1
            elif A[i]<a:
                prev = -1
            else:
                prev = 0
        elif ((A[i]-a)*prev<0:
            liss.append(lis)
            lis = []
        lis.append(A[i])
        a = A[i]
    liss.append(lis)
    ans = len(liss)
    return ans
print(solve(N,A))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
c = 1
x = a[1]-a[0]
for i in range(1,n-1):
    y = a[i+1]-a[i]
    if x*y<0:
        if i<n-2:x = a[i+2]-a[i+1]
        c+=1
print(c)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
c = 1
x = a[1]-a[0]
for i in range(1,n-1):
    y = a[i+1]-a[i]
    if x*y<0:
        if i!=n-2:x = a[i+2]-a[i+1]
        c+=1
print(c)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
a+=[0]
a=[a[i] for i in range(n) if a[i]!=a[i+1]]
s=0
while a:
  n=len(a)
  print(a)
  if n==1:
    s+=1
    break
  else:
    d=a[0]-a[1]>0
    for i in range(n-1):
      if d==(a[i]-a[i+1]<0):
        a=a[i+1:]
        break
    else:
      a=[]
    s+=1
print(s)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

v = A[1:] - A[:-1]
# print(v)

cnt = 0
i = 0
while i <= len(v) - 1:
    # print('i', i)
    if v[i] * v[i + 1] < 0:
        cnt += 1
        i += 1
    i += 1
ans = cnt + 1
print(ans)

# 単調非減少 : v[i + 1] - v[i] >= 0
# 単調非増加 : v[i + 1] - v[i] <= 0
# 折り返し点 : v[i] * v[i + 1] < 0 and v[i + 1] * v[i + 2] < 0

Submission status: Runtime Error

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

ans = 0
i = 0
while i < N-3:
  tmp = i
  while (A[tmp+1]-A[tmp])*(A[tmp+2]-A[tmp+1])>=0:
    tmp += 1
    if tmp == N-3:
      break
  ans += 1 
  i = tmp+2
else:
  if (A[N-2]-A[N-3])*(A[N-1]-A[N-2])>=0:
    ans += 1
  else:
    ans += 2
print(ans)

Submission status: Runtime Error

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

ans = 0
i = 0
while i < N-3:
  tmp = i
  while (A[tmp+1]-A[tmp])*(A[tmp+2]-A[tmp+1])>=0:
    tmp += 1
    if tmp == N-3:
      break
  ans += 1 
  i = tmp+2
else:
  if (A[N-2]-A[N-3])*(A[N-1]-A[N-3])>=0:
    ans += 1
  else:
    ans += 2
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    n=int(input())
    a=list(map(int, input().split()))
    ud=[]
    ans=1
    cnt=[0,0]
    for i in range(n-1):
        if a[i]==a[i+1]:
            ud.append('eq')
        elif a[i]<a[i+1]:
            ud.append('up')
        elif a[i]>a[i+1]:
            ud.append('down')
    if ud[0]=='up':
        cnt[0]=1
    elif ud[0]=='down':
        cnt[1]=1
    for i in range(1,n-1):
        if (cnt[0]==cnt[1]==0 or cnt[0]!=0) and ud[i]=='up':
            cnt[0]+=1
        elif (cnt[0]==cnt[1]==0 or cnt[1]!=0) and ud[i]=='down':
            cnt[1]+=1
        elif cnt[0]!=0 and ud[i]=='down':
            ans+=1
            cnt[0]=0
        elif cnt[1]!=0 and ud[i]=='up':
            ans+=1
            cnt[1]=0
        elif cnt[0]!=0 and cnt[1]==0 and i==n-2:
            ans+=1
        elif cnt[1]!=0 and cnt[0]==0 and i==n-2:
            ans+=1
    print(ans)
    
if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input());A=list(map(int,input().split(' ')))
if n <= 2:
    ans = 1
else:
    ans = 1;i = 2;l = 1
    if A[0]<A[1]:
        state = 'p'
    elif A[0]>A[1]:
        state = 'n'
    else:
        state = '0'
    while i < n:
        if A[i] > A[i-1]:
            if state == 'n' and l != 1:
                ans += 1
                l = 1
            else:
                l +=1
            state = 'p'
            
        elif A[i] < A[i-1]:
            if state == 'p' and l != 1:
                ans += 1
                l = 1
            else:
                l += 1
            state = 'n'
        i += 1
if l == 1:
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
def solve(ary):
  prev = ary[0]
  mem = "equal"
  for i in ary:
    if prev > i:
      mem = "minus"
      break
    elif prev < i:
      mem = "plus"
      break
  if mem == "minus":
    return M(ary)
  elif mem == "plus":
    return P(ary)
  else:
    return 1

def M(ary):
  prev = ary[0]
  for i in range(len(ary)):
    if ary[i] > prev:
      return solve(ary[i:])+1
    prev = ary[i]
  return 1
    
def P(ary):
  prev = ary[0]
  for i in range(len(ary)):
    if ary[i] < prev:
      return solve(ary[i:])+1
    prev = ary[i]
  return 1

N = int(input())
line = [int(x) for x in input().split(' ')]
print(solve(line))

Submission status: Runtime Error

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)
input = sys.stdin.readline

N,L,T = MI()
for_l = []
rev_l = []
final = []
for i in range(N):
    if i==0:
        x0,w0 = MI()
        x,w = x0,w0
    else:
        x,w = MI()
    if w==1:
        for_l += [x]
        final += [(x+T)%L]
    else:
        rev_l += [x]
        final += [(x-T)%L]

final.sort()
opposite_l = rev_l if w0==1 else for_l
opp_num = len(opposite_l)
cicle = (2*T)//L
res = (2*T)%L
cnt = 0 #i=1のアリが衝突する回数
cnt += opp_num*cicle
if res!=0 and res!=1:
    xr = (x0 + res -1)%L if w0==1 else x0
    xl = x0 if w0==1 else (x0-res+1)%L
    for i in opposite_l:
        if xl<=xr and xl<=i<=xr:
            cnt += 1
        if xl>xr and (i>=xl or i<=xr):
            cnt += 1
c = (cnt)%N if w0==1 else (-cnt)%N #x0+Tにくるアリの番号
if w0==1:
    c_idx = final.index((x0+T)%L)
else:
    c_idx_l = [i for i,v in enumerate(final) if v==(x0-T)%L]
    c_idx = c_idx_l[-1]
ans = [-1 for i in range(N)]
ans[c] = final[c_idx]
for i in range(1,N):
    ans[(c+i)%N] = final[(c_idx+i)%N]
[print(x) for x in ans]

Submission status: Runtime Error

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)
input = sys.stdin.readline

N,L,T = MI()
for_l = []
rev_l = []
final = []
for i in range(N):
    if i==0:
        x0,w0 = MI()
        x,w = x0,w0
    else:
        x,w = MI()
    if w==1:
        for_l += [x]
        final += [(x+T)%L]
    else:
        rev_l += [x]
        final += [(x-T)%L]

final.sort()
opposite_l = rev_l if w0==1 else for_l
opp_num = len(opposite_l)
cicle = (2*T)//L
res = (2*T)%L
cnt = 0 #i=1のアリが衝突する回数
cnt += opp_num*cicle
if res!=0 and res!=1:
    xr = (x0 + res -1)%L if w0==1 else x0
    xl = x0 if w0==1 else (x0-res+1)%L
    for i in opposite_l:
        if xl<=xr and xl<=i<=xr:
            cnt += 1
        if xl>xr and (i>=xl or i<=xr):
            cnt += 1
c = (cnt)%N #x0+Tにくるアリの番号
if w0==1:
    c_idx = final.index((x0+T)%L)
else:
    c_idx_l = [i for i,v in enumerate(final) if v==(x0-T)%L]
    c_idx = c_idx_l[-1]
ans = [-1 for i in range(N)]
ans[c] = final[c_idx]
for i in range(1,N):
    ans[(c+i)%N] = final[(c_idx+i)%N]
[print(x) for x in ans]

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 0
up = 0
down = 0
before = A[0]
tmp = 0
for i in range(1,N):
  tmp = A[i]
  if (up == 0) and (down == 0):
    if tmp > before:
      up = 1
      before = tmp
    elif tmp < before:
      down = 1
      before = tmp
    else:
      continue
  elif up == 1:
    if tmp < before:
      count += 1
      up = 0
      before = tmp
  elif down = 1:
    if tmp > before:
      count += 1
      down = 0
      before = tmp
count += 1
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = 0
up = 0
down = 0
before = A[0]
tmp = 0
for i in range(1,N):
  tmp = A[i]
  if (up == 0) and (down == 0):
    if tmp > before:
      up = 1
      before = tmp
    elif tmp < before:
      down = 1
      before = tmp
    else:
      continue
  elif up == 1:
    if tmp < before:
      count += 1
      up = 0
      before = tmp
  elif down = 1:
    if tmp > before:
      count += 1
      down = 0
      before = tmp
count += 1
print(count)

Submission status: Runtime Error

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


cnt = 0
for i in range(N - 1):
    if A[i] == A[i + 1]:
        A[i] = '#'
        cnt += 1

for _ in range(cnt):
    A.remove('#')
#print(A)

if A[0] < A[1]:
    plus = 1
else:
    plus = -1

i = 1
ans = 1
while i < len(A) - 1:
    if A[i-1] < A[i] < A[i+1] or A[i-1] > A[i] > A[i+1]:
        pass
    else:
        i += 1
        ans += 1
    i += 1
print(ans)

Submission status: Runtime Error

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


cnt = 0
for i in range(N - 1):
    if A[i] == A[i + 1]:
        A[i] = '#'
        cnt += 1

for _ in range(cnt):
    A.remove('#')
print(A)

if A[0] < A[1]:
    plus = 1
else:
    plus = -1

i = 1
ans = 1
while i < len(A) - 1:
    if A[i-1] < A[i] < A[i+1] or A[i-1] > A[i] > A[i+1]:
        pass
    else:
        i += 1
        ans += 1
    i += 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from queue import Queue


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


def main():
	n = INT()
	a = LIST()
	p = 0
	q = 0
	l = []
	temp = 0

	if a[0] < a[1]:
		sign = 1
	elif a[0] > a[1]:
		sign = -1
	else:
		sign = 0

	for i in range(n-1):
		if a[i] < a[i+1] and sign == -1:
			p += 1
			sign = 1
			# print("a")
			if temp == 0:
				q += 1
				temp = 1

		elif a[i] > a[i+1] and sign == 1:
			q += 1
			sign = -1
			# print("b")
			if temp == 0:
				p += 1
				temp = 1


	# print(p, q)
	print(max(p, q) + 1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from queue import Queue


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


def main():
	n = INT()
	a = LIST()
	p = 0
	q = 0
	l = []

	sign = (a[1]-a[0]) / abs(a[1]-a[0])

	for i in range(n-1):
		if a[i] < a[i+1] and sign == -1:
			p += 1
			sign = 1
			#print("a")

		elif a[i] > a[i+1] and sign == 1:
			q += 1
			sign = -1
			#print("b")

	print(p + 2)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from queue import Queue


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


def main():
	n = INT()
	a = LIST()
	p = 0
	q = 0
	l = []

	sign = (a[1]-a[0]) / abs(a[1]-a[0])

	for i in range(n-1):
		if a[i] < a[i+1] and sign == -1:
			p += 1
			sign *= -1

		elif a[i] > a[i+1] and sign == 1:
			q += 1
			sign *= -1


	print(max(p, q) + 1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from queue import Queue


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


def main():
	n = INT()
	a = LIST()
	p = 0
	q = 0
	l = []

	sign = (a[1]-a[0]) / abs(a[1]-a[0])

	for i in range(n-1):
		print(i)
		if a[i] < a[i+1] and sign == -1:
			p += 1
			sign *= -1

		elif a[i] > a[i+1] and sign == 1:
			q += 1
			sign *= -1


	print(max(p, q) + 1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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 groupby, 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 functools import reduce
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 ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
A = [k for k, g in groupby(A)]
i = 1
ans = 0

while i < N-1:
	if A[i-1] < A[i] > A[i+1] or A[i-1] > A[i] < A[i+1]:
		ans += 1
		i += 1
	i += 1

print(ans+1)

Submission status: Runtime Error

Solution code:
from collections import deque

n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n - 1):
    if a[i] != a[i + 1]:
        b.append(a[i])
if b[len(b) - 1] != a[n - 1]:
    b.append(a[n - 1])
l = len(b)
m = 0
ans = 1
for i in range(l - 1):
    if m == 0:
        if b[i] < b[i + 1]:
            m = 1
        else:
            m = -1
    elif m == 1:
        if b[i] > b[i + 1]:
            ans += 1
            m = 0
    else:
        if b[i] < b[i + 1]:
            ans += 1
            m = 0
print(ans)