Codeforces Round #669 (Div. 2)

Powered by:AB_IN 局外人

A Ahahahahahahahaha

题目突破点是在于 k k 的值, n 2 k n \frac {n} {2} \le k \le n .这告诉我们最多删一半。
那这个题就好做了,因为这个串只由 0 0 1 1 组成。

  • 0 0 的数量 > \gt 1 1 的数量,把 1 1 删光,不用管 k k 的奇偶。
  • 0 0 的数量 < \lt 1 1 的数量,把 0 0 删光, k k 必须为偶数。
  • 0 0 的数量 = = 1 1 的数量,随便删。
for _ in range(int(input())):
    n=int(input())
    lst=input().split()
    one=lst.count("1")
    zero=lst.count("0")
    if one>zero:
        lst=list(filter(lambda x: x=="1",lst))
        if len(lst)&1:
            lst.pop()
    else:
        lst=list(filter(lambda x: x=="0",lst))
    print(len(lst))
    print(" ".join(lst))

B Big Vova

  • from math import gcd当只用这个库的一个函数时比较方便。
  • 还是再记一下 m a x max 函数对包含元组的列表的应用。
lst=[(1,2,3) , (3,1,2) ,(2,10000,2200)]
lst.sort()
print(max(lst))
print(max(lst,key=lambda x: x[1]))
print(min(lst))
print(*lst)

#(3, 1, 2)
#(2, 10000, 2200)
#(1, 2, 3)
#(1, 2, 3) (2, 10000, 2200) (3, 1, 2)

可以看出,默认元组之间的大小比较是根据元组的第一个元素决定的
如果需要更改则在后面加限制条件即可。

  • 再说这个题,第一个数一定是最大的数。然后就找与这个数 G C D GCD 最大的数。再把这两个数的 G C D GCD 和别的数比,挑最大的。以此类推,直到列表为空。
from math import gcd
for _ in range(int(input())):
    n=int(input())
    lst=list(map(int,input().split()))
    maxn=max(lst)
    ans=[]
    while lst:
        tmp = max((gcd(maxn,i),i) for i in lst)
        lst.remove(tmp[1])
        ans.append(tmp[1])
        maxn = tmp[0]
    print(*ans)

完结。

猜你喜欢

转载自blog.csdn.net/qq_45859188/article/details/108496385