[PYTHON] Codeforces Round # 664 (Div. 2) Bacha Review (8/13)

This time's results

スクリーンショット 2020-08-13 14.18.37.png

Impressions of this time

I think it went well this time. However, I am disappointed because I could not solve the D problem that seemed to be solved in time due to the overlap with lunch. However, it seemed that I could solve the problem at that level, so I thought I had grown a little. This time, I was able to deal with the problem calmly, so I think that is the reason for the victory.

Problem A

Based on the given values of $ r, g, b, w $, it is judged whether or not it becomes a palindrome when each character is arranged. Also, you can select $ r, g, b $ one by one and change them to $ w $ as much as possible, but ** this operation does not change the evenness of each if you do it twice **, so this You only have to think about two ways to do it once or not.

Under this, when the sum of the values of $ r, g, b, w $ (the length of the palindrome) is even, any value of ** $ r, g, b, w $ is even **. If you have one, you can make it. Also, by changing $ r, g, b $ to $ w $ once, the even and odd numbers of ** $ r, g, b, w $ are reversed **, so $ r, g, b, w $ You can make a palindrome if any value is odd.

On the other hand, when the length of the palindrome is odd, only one of ** $ r, g, b, w $ needs to be odd **. Also, considering the inversion of even and odd numbers, it is possible to make a palindrome even if only one of $ r, g, b, w $ is even.

A.py


for _ in range(int(input())):
    r,g,b,w=map(int,input().split())
    if (r+g+b+w)%2==0:
        if r%2==0 and g%2==0 and b%2==0 and w%2==0:
            print("Yes")
        elif r%2==1 and g%2==1 and b%2==1 and w%2==1:
            print("Yes")
        else:
            print("No")
    else:
        x=[r%2,g%2,b%2,w%2]
        if x.count(1)==1:
            print("Yes")
        elif x.count(0)==1 and r>0 and g>0 and b>0:
            print("Yes")
        else:
            print("No")

Problem B

Think of playing chess like a rook and following the squares on any grid. Even if you follow the dark clouds, the 埒 will not open, so consider ** following in some order **.

At first I thought I would follow the swirl, but I found it cumbersome to implement, so I thought ** I would follow it line by line **. In other words, consider following as shown in the figure below.

IMG_0536.JPG

Here, only (1) after tracing all the upper rows ** and continuing to trace back to the lower row ** and (2) ** when moving between rows, you must move ** by the tangent square. Just be careful, the implementation looks like this:

① can be implemented by concatenating with list (range (x, -1, -1)) + list (range (x + 1, n)), and ② is whether the squares in contact are the leftmost column or the rightmost column. It can be implemented by dividing the case with.

B.py


n,m,x,y=map(int,input().split())
x-=1;y-=1
ans=[]
ans.append([x,y])

now=0
for i in list(range(x,-1,-1))+list(range(x+1,n)):
    now=1-now
    if i==x:
        ans+=[[i,j] for j in range(m) if j!=y]
    else:
        if now:
            ans+=[[i,j] for j in range(m)]
        else:
            ans+=[[i,j] for j in range(m)][::-1]
for i in ans:
    print(i[0]+1,i[1]+1)

C problem

At first, I thought about deciding not to set bits in order from the upper digit, but I felt that it would be difficult to greedily decide in the middle digit **.

When choosing something (number) like this problem, the outlook often improves when considering DP . Furthermore, it is also a decisive factor that bit calculation that makes it easy to save states and that or for each bit from $ 0 \ leqq a_i, b_i <2 ^ 9 $ takes only this range ( limit on the number of states **). Therefore, consider the following DP.

$ dp [i] [j]: = $ (Is there a case where it is $ j $ when taking or for each bit up to $ a_i $)

Considering such DP, if $ b_k (1 \ leqq k \ leqq m) $ is selected for $ a_i $ when $ dp [i-1] [j] = 1 $, it is as follows. Will be.

dp[i][j\|(a\_i \\& b\_k)]=1

Therefore, this DP can be calculated in about $ n \ times m \ times 2 ^ 9 $ times, so you can write a sufficiently fast program.

C.py


n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
change=[[a[i]&b[j] for j in range(m)]for i in range(n)]
dp=[[0]*(2**9) for i in range(n)]

for i in range(n):
    if i==0:
        for k in range(m):
            dp[0][change[i][k]]=1
        continue
    for j in range(2**9):
        if dp[i-1][j]==1:
            for k in range(m):
                dp[i][j|change[i][k]]=1

for j in range(2**9):
    if dp[-1][j]==1:
        print(j)
        break

D problem

At first I misunderstood the problem and thought I would just handle the array $ a $. ** If you look at the sample, you shouldn't have made a mistake **, so reflect on it and make use of it next (in this case, you can solve it by using DP).

First of all, it is self-evident that you want to select from the largest one. At this time, in the case of $ a \ _i> m $, it will not be possible to select it in the subsequent $ d $ day, so first divide it into the ** $ a \ _i> m $ element and the $ a \ _i \ leqq m $ element. ** Store in the array ʻe, f. Also, it is assumed that ʻe and f are arranged in descending order.

Here, if you know the number of elements that are ** $ a \ _i> m $, you can know the number of elements that can be selected from the elements that are ** $ a \ _i \ leqq m $, so ** $ a \ _i Let's assume that there are $ k $ elements that are> m $ **. Also, as you can easily see, $ k $ is less than or equal to len (f). Here, considering that $ k $ $ a \ _i (> m) $ should be arranged so that $ a \ _i (\ leqq m) $ can be selected as much as possible, the following arrangement is optimal.

IMG_0537.PNG

Therefore, at this time, $ a \ _i (\ leqq m) $ can be selected for $ n- $ {$ (k-1) \ times (d + 1) + 1 $}. Also note that $ (k-1) \ times (d + 1) + 1 \ leqq n $ must be satisfied (the maximum $ k $ that satisfies this is $ K'$). .. Furthermore, even if ** $ n- $ {$ (k-1) \ times (d + 1) + 1 $} exceeds len (e), only those included in ʻe` can be selected (✳︎). ) ** Please note.

Also, if you increase ** $ k $ by 1, the number that can be selected from ʻe decreases by $ d + 1 $ **, so ** $ k $ to $ 0 $ ~ $ K'$ while managing the difference ** Look for the largest number of them. Also, $ k = 0 \ rightarrow 1 $ increases by $ 1 $ instead of $ d + 1 $, so consider $ k = 0 $ separately. At this time, you should consider the sum of ʻe.

In this way, the maximum value obtained for each $ k $ should be output as the answer.

(I noticed later, but I think that the implementation of (✳︎) was clearer if I tried to reduce ** $ k $ **.)

D.py


n,d,m=map(int,input().split())
a=list(map(int,input().split()))
e,f=[i for i in a if i<=m],[i for i in a if i>m]
e.sort(reverse=True)
f.sort(reverse=True)
c=len(e)
if c==n:
    print(sum(a))
    exit()
#a[i]>m can be selected appropriately up to max
l=1
r=min((n-1)//(d+1)+1,len(f))
#k's range 
#e,f 's sum
ans=sum(e)
nowe,nowf=0,0
for i in range(l,r+1):
    if i==l:
        nowe=sum(e[:n-(1+(l-1)*(d+1))])
        nowf=sum(f[:l])
        ans=max(nowe+nowf,ans)
        continue
    nowe-=sum(e[n-(1+(i-1)*(d+1)):n-(1+(i-1-1)*(d+1))])
    nowf+=f[i-1]
    ans=max(nowe+nowf,ans)
print(ans)

After the E problem

I will skip this time

Recommended Posts

Codeforces Round # 658 (Div. 2) Bacha Review (7/29)
Codeforces Round # 654 (Div. 2) Bacha Review (8/18)
Codeforces Round # 594 (Div. 2) Bacha Review (10/29)
Codeforces Round # 609 (Div. 2) Bacha Review (10/8)
Codeforces Round # 597 (Div. 2) Bacha Review (10/27)
Codeforces Round # 651 (Div. 2) Bacha Review (8/20)
Codeforces Round # 659 (Div. 2) Bacha Review (8/5)
Codeforces Round # 610 (Div. 2) Bacha Review (10/5)
Codeforces Round # 479 (Div. 3) Bacha Review (9/25)
Codeforces Round # 603 (Div. 2) Bacha Review (10/15)
Codeforces Round # 600 (Div. 2) Bacha Review (10/21)
Codeforces Round # 481 (Div. 3) Bacha Review (9/24)
Codeforces Round # 639 (Div. 2) Bacha Review (9/4)
Codeforces Round # 521 (Div. 3) Bacha Review (10/9)
Codeforces Round # 652 (Div. 2) Bacha Review (8/24)
Codeforces Round # 606 (Div. 3) Bacha Review (10/13)
Codeforces Round # 613 (Div. 2) Bacha Review (10/1)
Codeforces Round # 665 (Div. 2) Bacha Review (8/23)
Codeforces Round # 592 (Div. 2) Bacha Review (11/03)
Codeforces Round # 662 (Div. 2) Bacha Review (8/8)
Codeforces Round # 618 (Div. 2) Bacha Review (9/26)
Codeforces Round # 648 (Div. 2) Bacha Review (9/5)
Codeforces Round # 676 (Div. 2) Bacha Review (10/31)
Codeforces Round # 675 (Div. 2) Bacha Review (10/30)
Codeforces Round # 486 (Div. 3) Bacha Review (9/23)
Codeforces Round # 671 (Div. 2) Bacha Review (9/22)
Codeforces Round # 669 (Div. 2) Bacha Review (9/9)
Codeforces Round # 672 (Div. 2) Bacha Review (10/16)
Codeforces Round # 638 (Div. 2) Bacha Review (9/16)
Codeforces Round # 663 (Div. 2) Bacha Review (8/13)
Codeforces Round # 668 (Div. 2) Bacha Review (9/7)
Codeforces Round # 663 (Div. 2) Bacha Review (8/16)
Codeforces Round # 609 (Div. 2) Bacha Review (10/6)
Codeforces Round # 645 (Div. 2) Bacha Review (9/10)
Codeforces Round # 664 (Div. 2) Bacha Review (8/13)
Codeforces Round # 660 (Div. 2) Bacha Review (8/4)
Codeforces Round # 643 (Div. 2) Review
Codeforces Round # 679 (Div. 2) Review (10/25)
Codeforces Round # 657 (Div. 2) Review
Educational Codeforces Round 93 Bacha Review (8/17)
Educational Codeforces Round 94 Bacha Review (9/3)
Educational Codeforces Round 91 Bacha Review (7/28)
Educational Codeforces Round 88 Bacha Review (8/4)
Educational Codeforces Round 86 Bacha Review (9/17)
Educational Codeforces Round 89 Bacha Review (9/8)
Educational Codeforces Round 92 Bacha Review (7/30)
Educational Codeforces Round 90 Bacha Review (8/19)
Codeforces Round # 609 (Div. 2) (up to B)
Educational Codeforces Round 87
Codeforces Beta Round # 13
Codeforces Beta Round # 1
Codeforces Beta Round # 2
DP100 Question Bacha Review 1 ~ 10 (8 / 26,27)
Codeforces Round # 626 B. Count Subrectangles