Epidemic in Monstropolis

Epidemic in Monstropolis

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

贪心

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

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

代码如下:

 1 import sys
 2 
 3 n = int(input())
 4 a = [int(x) for x in input().split()]
 5 m = int(input())
 6 b = [int(x) for x in input().split()]
 7 
 8 sum1,sum2 = 0,0 
 9 for i in a:
10     sum1 += i
11 for i in b:
12     sum2 += i
13 
14 if sum1 != sum2:
15     print('NO')
16     sys.exit(0)
17     
18 s = []
19 idx = 0
20 for i in range(m):
21     t = b[i]
22     oo = idx
23     mm = idx
24     
25     while t > 0:
26         t -= a[idx]
27         if a[mm] < a[idx]:
28             mm = idx
29         idx += 1
30     if t < 0:
31         print('NO')
32         sys.exit(0)
33     
34     if mm == oo:
35         while mm+1<idx and a[mm]==a[mm+1]:
36             mm = mm+1
37     
38     flag = 0
39     if mm-1>=oo and a[mm]>a[mm-1]:
40         flag = 1
41     elif mm+1<idx and a[mm]>a[mm+1]:
42         flag = 2
43     elif idx-oo == 1:
44         continue
45     
46     if flag == 0:
47         print('NO')
48         sys.exit(0)
49     elif flag == 1:
50         for x in range(mm,oo,-1):
51             s.append([x-oo+i,'L'])
52         for x in range(mm,idx-1):
53             s.append([i,'R'])
54     elif flag == 2:
55         for x in range(mm,idx-1):
56             s.append([mm-oo+i,'R'])
57         for x in range(mm,oo,-1):
58             s.append([x-oo+i,'L'])
59 
60 print('YES')
61 for x in s:
62     print(x[0]+1,x[1])
原文地址:https://www.cnblogs.com/barrier/p/6059428.html