Codeforces Round #697 (Div. 3)补题

题目链接

文章目录

  • A. Odd Divisor
  • B. New Year's Number
  • C. Ball in Berland
  • D. Cleaning the Phone
  • E. Advertising Agency
  • F. Unusual Matrix
  • G. Strange Beauty

A. Odd Divisor

You are given an integer n. Check if n has an odd divisor, greater than one (does there exist such a number x (x>1) that n is divisible by x and x is odd).

For example, if n=6, then there is x=3. If n=4, then such a number does not exist.
思路:是奇数就一定有奇数因子,否则不断除以2看是否会产生奇数

s=int(input())
for j in range(s):
    flag = 0
    n=int(input())
    if n%2==1:
        flag=1
    else:
        while n%2==0:
            n=n//2
        if n!=1:
            flag=1
    if flag==1:
        print("YES")
    else:
        print("NO")

B. New Year’s Number

Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.

For example, if:
n=4041, then the number n can be represented as the sum 2020+2021;
n=4042, then the number n can be represented as the sum 2021+2021;
n=8081, then the number n can be represented as the sum 2020+2020+2020+2021;
n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
思路:除以2020,比较商和余数即可。或者不断做减法直到0为止

a=int(input())
for i in range(a):
    n=int(input())
    x, y = n % 2020, n // 2020
    if y >= x:
        print("YES")
    else:
        print("NO")
for _ in range(int(input())):
    a=int(input())
    while a>0:
        if a%10==0:
            a=a-2020
        else:
            a=a-2021
    if a==0:
        print('YES')
    else:
        print('NO')

C. Ball in Berland

At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls.

Each class must present two couples to the ball. In Vasya’s class, a boys and b girls wish to participate. But not all boys and not all girls are ready to dance in pairs.

Formally, you know k possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair.

For example, if a=3, b=4, k=4 and the couples (1,2), (1,3), (2,2), (3,4) are ready to dance together (in each pair, the boy’s number comes first, then the girl’s number), then the following combinations of two pairs are possible (not all possible options are listed below):

(1,3) and (2,2);
(3,4) and (1,3);
But the following combinations are not possible:

(1,3) and (1,2) — the first boy enters two pairs;
(1,2) and (2,2) — the second girl enters two pairs;
Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs.
思路:ans += 总共的方案-当前男生出现过的次数-当前女生出现过的次数+当前男生和当前女生同时出现的次数(每个元组记为一次,故这里加1)。因为会有重复统计故最后除以2

T=int(input())
for i in range(T):
    ls=[]
    ans=0
    sa,sb={
     },{
     }
    a,b,k=map(int,input().split())
    ak=list(map(int,input().split()))
    bk=list(map(int,input().split()))
    for j in range(len(ak)):
    	#ls存放配对的元组
        ls.append((ak[j],bk[j]))
        #字典统计数字出现次数
        sa[ak[j]] =sa.get(ak[j],0)+1
        sb[bk[j]] =sb.get(bk[j],0)+1
    for i in range(0,len(ls)):
        ans+=k-sa[ls[i][0]]-sb[ls[i][1]]+1
    print(ans//2)

D. Cleaning the Phone

Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up ai units of memory.

Polycarp wants to free at least m units of memory (by removing some applications).

Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer bi to each application:

bi=1 — regular application;
bi=2 — important application.
According to this rating system, his phone has b1+b2+…+bn convenience points.

Polycarp believes that if he removes applications with numbers i1,i2,…,ik, then he will free ai1+ai2+…+aik units of memory and lose bi1+bi2+…+bik convenience points.

For example, if n=5, m=7, a=[5,3,2,1,4], b=[2,1,1,2,1], then Polycarp can uninstall the following application sets (not all options are listed below):

applications with numbers 1,4 and 5. In this case, it will free a1+a4+a5=10 units of memory and lose b1+b4+b5=5 convenience points;
applications with numbers 1 and 3. In this case, it will free a1+a3=7 units of memory and lose b1+b3=3 convenience points.
applications with numbers 2 and 5. In this case, it will free a2+a5=7 memory units and lose b2+b5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.

大致内容
n:手机中app数量
m:想要腾出的内存
a:每个app占用的内存
b:每个app的重要指数(1和2)
本题要求a的和大于等于m时b最小的情况

思路:把重要程度为1和2的分别写在两个列表里并排序,一开始全用重要性为2的,然后通过前缀和比较不断用重要性为1的做替换

from bisect import bisect_left,bisect_right
T=int(input())
for i in range(T):
    v1 = []
    v2 = []
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    for i in range(n):
        if b[i]==1:
            v1.append(a[i])
        else:
            v2.append(a[i])
    v1.sort(reverse=True)
    v2.sort(reverse=True)
    ls1 = []
    ls2 = []
    temp=0
    for i in v1:
        temp+=i
        ls1.append(temp)
    temp=0
    for i in v2:
        temp+=i
        ls2.append(temp)
    ls1.insert(0,0)
    ls2.insert(0,0)
    ans=float("inf")
    #以下类似写法用c++能过,python会超时所以还要加个二分
    # for i in range(len(ls1)):
    #     cur=len(ls2)-1
    #     while (cur>=1 and ls2[cur-1]+ls1[i]>=m):
    #         cur-=1
    #     if ls2[cur]+ls1[i]>=m:
    #         ans=min(ans,i+2*cur)
    # if ans==float("inf"):
    #     print(-1)
    # else:
    #     print(ans)
    for i in range(len(ls1)):
        cur = bisect_left(ls2, m - ls1[i])
        if cur >= len(ls2):
            cur-=1
        if ls1[i] + ls2[cur] >= m:
            ans = min(ans, i + 2 * cur)
    if ans==float("inf"):
        print(-1)
    else:
        print(ans)

E. Advertising Agency

Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has ai followers.

Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.

Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).

For example, if n=4, k=3, a=[1,3,1,2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:

conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a1+a2+a4=6.
conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a2+a3+a4=6.
Since the answer can be quite large, output it modulo 109+7.
思路:组合问题

import math
for i in range(int(input())):
    n,k=map(int,input().split())
    ls=list(map(int,input().split()))
    ls.sort(reverse=True)
    if ls[k-1] not in ls[k:]:
        print(1)
    else:
        s=ls.count(ls[k-1])  #一共有几个这样的数
        m=ls[0:k].count(ls[k-1]) #前k个中有几个这样的数
        print(math.comb(s,m)%(10**9+7))#10e9+7防止溢出

F. Unusual Matrix

You are given two binary square matrices a and b of size n×n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more):

vertical xor. You choose the number j (1≤j≤n) and for all i (1≤i≤n) do the following: ai,j:=ai,j⊕1 (⊕ — is the operation xor (exclusive or)).
horizontal xor. You choose the number i (1≤i≤n) and for all j (1≤j≤n) do the following: ai,j:=ai,j⊕1.

Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b.
思路:经过分析不难发现,当第一行被固定好时(即和目标矩阵第一行一样),剩下的行就不能再改了。呢么只要检查剩下的行是否和目标矩阵对应行相等或者异或1以后相等就行了。

for _ in range(int(input())):
    m=int(input())
    matrixa=[]
    matrixb=[]
    for _ in range(m):
        matrixa.append(list(map(int,input())))
    input()
    for _ in range(m):
        matrixb.append(list(map(int,input())))
    for i in range(m):
        if matrixa[0][i]!=matrixb[0][i]:
            for j in range(m):
                matrixa[j][i]^=1
    flag=1
    for row in range(1,m):
        if matrixa[row]==matrixb[row]:
            flag=1
            continue
        else:
            for j in range(m):
                matrixa[row][j]^=1
            if matrixa[row]==matrixb[row]:
                flag=1
            else:
                flag=0
                break
    if flag==1:
        print("YES")
    else:
        print("NO")

G. Strange Beauty

Polycarp found on the street an array a of n elements.

Polycarp invented his criterion for the beauty of an array. He calls an array a beautiful if at least one of the following conditions must be met for each different pair of indices i≠j:

ai is divisible by aj;
or aj is divisible by ai.
For example, if:

n=5 and a=[7,9,3,14,63], then the a array is not beautiful (for i=4 and j=2, none of the conditions above is met);
n=3 and a=[2,14,42], then the a array is beautiful;
n=4 and a=[45,9,3,18], then the a array is not beautiful (for i=1 and j=4 none of the conditions above is met);
Ugly arrays upset Polycarp, so he wants to remove some elements from the array a so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array a beautiful.
思路:动态规划(枚举因子和倍数都行),其中dp[i]含义:列表满足条件且最后一项为i,此时列表的最大长度。

for _ in range(int(input())):
    length=int(input())
    array=list(map(int,input().split()))
    array.sort()
    cnt=[0]*(max(array)+1)
    dp=[0]*(max(array)+1)
    for x in array:
        cnt[x]+=1
    for i in range(1,len(dp)):
        dp[i]+=cnt[i]
        #从2倍i开始枚举i的倍数
        for j in range(2*i,len(dp),i):
            dp[j]=max(dp[j],dp[i])
    print(length-max(dp))

你可能感兴趣的:(OJ题解,python)