样例题:https://codeforces.com/contest/1234/problem/B1
主要是需要对每个数字 Ai 保存的位置进行记录,Ai 最大是1e9,会导致数组保存不了,于是想到python中的list
list声明:
mylist = []
print (type(mylist))
list增加:
list = [1,2,3,4]
list.append(5)
list.insert(2,6)
print (list)
#[1,2,6,3,4,5]
list删除:
####
x = list.pop()
#x = 5
#list = [1,2,6,3,4]
list.remove(7)
#ValueError: list.remove(x): x not in list
list.remove(6)
print (list)
#list = [1,2,3,4]
list修改:
####
list[1] = 10
print (list)
#list = [1, 10, 3, 4]
list查找:
#list = [1, 10, 3, 4]
list.count(1)
#1
list.count(0)
#0
list.index(1)
#0
list.index(3)
#2
list.index(0)
#ValueError: 0 is not in list
if 0 in list:
print ("0 is in list")
else:
print ("0 is not in list")
其他操作:
list = [1, 10, 3, 4, 'a', 'Aa', 'Abb', 'Aba']
list.reverse()
list = list[::-1]
list.sort()
#list = [1, 3, 4, 10, 'Aa', 'Aba', 'Abb', 'a']
for i, v in enumerate(list):
print ('index = %s, value = %s' % (i, v))
print (max(list))
#'a'
print (min(list))
#1
备注:
reverse操作是按照输入顺序倒序;sort操作是按照先数字从小到大,再按照字母序从小到大排序
样例题代码:
n, k = map(int, input().split())
#print ('n =', n, end = '\n')
#print ('k =', k, end = '\n')
x = map(int, input().split())
#print ('x =', x)
ans = []
for i in x:
if i not in ans:
if len(ans) == k:
#delete last one
del ans[-1]
#ans.append(i)
ans.insert(0, i)
#print (ans)
print (len(ans))
for i in range(0, len(ans) - 1):
print (ans[i], end = ' ')
print (ans[-1])