Epidemic in Monstropolis

时间:2022-06-19 07:35:02

Epidemic in Monstropolis

题目链接:http://codeforces.com/contest/733/problem/C

贪心

新序列的m个数肯定是由原序列的连续的m个子序列构成的,只需要找到每个连续子序列中最大的数即可。

细节中需要注意:一个连续子序列可能有很多个最大数,最大数的左右两个数必须要有一个小于这个数;还有下标的处理。

代码如下:

 import sys

 n = int(input())
a = [int(x) for x in input().split()]
m = int(input())
b = [int(x) for x in input().split()] sum1,sum2 = 0,0
for i in a:
sum1 += i
for i in b:
sum2 += i if sum1 != sum2:
print('NO')
sys.exit(0) s = []
idx = 0
for i in range(m):
t = b[i]
oo = idx
mm = idx while t > 0:
t -= a[idx]
if a[mm] < a[idx]:
mm = idx
idx += 1
if t < 0:
print('NO')
sys.exit(0) if mm == oo:
while mm+1<idx and a[mm]==a[mm+1]:
mm = mm+1 flag = 0
if mm-1>=oo and a[mm]>a[mm-1]:
flag = 1
elif mm+1<idx and a[mm]>a[mm+1]:
flag = 2
elif idx-oo == 1:
continue if flag == 0:
print('NO')
sys.exit(0)
elif flag == 1:
for x in range(mm,oo,-1):
s.append([x-oo+i,'L'])
for x in range(mm,idx-1):
s.append([i,'R'])
elif flag == 2:
for x in range(mm,idx-1):
s.append([mm-oo+i,'R'])
for x in range(mm,oo,-1):
s.append([x-oo+i,'L']) print('YES')
for x in s:
print(x[0]+1,x[1])