首页 > 其他分享 >Weekly Contest 313

Weekly Contest 313

时间:2022-10-08 22:34:43浏览次数:72  
标签:return Contest 313 self int while grid c2 Weekly

Weekly Contest 313

Problem A

Number of Common Factors

思路

数据范围小,直接暴力就完事了

代码

class Solution:
    def commonFactors(self, a: int, b: int) -> int:
        t = []
        for i in range(1,min(a,b)+1):
            if a%i==0 and b%i==0:
                t.append(i)
        return len(t)

Problem B

Maximum Sum of an Hourglass

思路

数据范围也挺小的,暴力就完事了

代码

class Solution:
    def maxSum(self, grid: List[List[int]]) -> int:
        ans = 0
        n = len(grid)
        m = len(grid[0])
        for i in range(0,n-2):
            for j in range(0,m-2):
                t = grid[i][j]+grid[i+2][j]
                t+=grid[i][j+1]+grid[i+1][j+1]+grid[i+2][j+1]
                t+=grid[i][j+2]+grid[i+2][j+2]
                ans = max(ans,t)
        return ans

Problem C

Minimize XOR

思路

转二进制算一下两个数各有多少1,构造结果,优先把1分配到num1的1的位置,多的1再从低到高分配,写的有点拖沓

代码

import numpy as np
class Solution:
    def minimizeXor(self, num1: int, num2: int) -> int:
        a,b,c = np.zeros(32,dtype=np.int),np.zeros(32,dtype=np.int),np.zeros(32,dtype=np.int)
        t = 0
        c1,c2 = 0,0
        t1,t2 = num1,num2
        while num1!=0:
            a[t] = num1%2
            if a[t]==1:
                c1+=1
            num1 = num1//2
            t+=1
        t = 0
        while num2!=0:
            b[t] = num2%2
            if b[t]==1:
                c2+=1
            num2 = num2//2
            t+=1
        if c1==c2:
            return t1
        l = 31
        while l>=0 and a[l] == 0:
            l-=1
        while l>=0 and c2!=0:
            if a[l]==1:
                c[l]=1
                c2-=1
            l-=1
        l = 0
        while c2!=0:
            if c[l]==0:
                c[l]=1
                c2-=1
            l+=1
        l = 31
        # print(c)
        while l>=0 and c[l]==0:
            l-=1
        t = 0
        while l>=0:
            # print(l)
            t = (t<<1)+c[l]
            # print(t)
            l-=1
        return t

Problem D

Maximum Deletions on a String

思路

数据范围小,暴力也能过,大致就是枚举长度做DFS

代码

class Solution:
    def deleteString(self, s: str) -> int:
        if (len(set(s)) == 1): return len(s)
        self.memo = {}
        return self.helper(s) + 1
        
    def helper(self, s):
        # print(s)
        if s in self.memo:
            return self.memo[s]
        total = 0
        for i in range(1, len(s) // 2 + 1):
            if s[:i] == s[i: i * 2]:
                total = max(total, self.helper(s[i:]) + 1)
        self.memo[s] = total
        return self.memo[s]

总结

出了三题,最后一题没整明白,没想到大家都是n^3我循环DP过不了 它DFS能过

标签:return,Contest,313,self,int,while,grid,c2,Weekly
From: https://www.cnblogs.com/baihualiaoluan/p/16770512.html

相关文章

  • CCPC Finals 2021 H Harie Programming Contest (网络流&支配树的妙用)
    Link题意:给一个二分图,求有多少种方案删去恰好两个点,使得最大匹配数不变。\(n,m\le2\times10^5\)。二话不说先跑一遍Dinic网络流,设残量网络形成的图为\(G\)。然后......
  • MSU Trinity Contest Petrozavodsk Winter Camp 2014
    题目列表A.MEX-QueryD.ShortEnoughTaskF.JustAnotherSequenceProblemA.ABBA题意:Solution思路:CodeconstintN=1000010;intn,......
  • The 2021 ICPC Asia Shenyang Regional Contest
    比赛链接:https://codeforces.com/gym/103427B.BitwiseExclusive-ORSequence题意:给定\(m\)个限制,要求构造一个全是非负整数的序列\(a\),每个限制告诉\(u,v,w\),......
  • 「题解」Codeforces GYM 102268 J Jealous Split(300iq Contest 1 J)
    怎么想到的结论?结论是,如果把看成最小化\(\sum{s_i}^2\),那么一定满足条件。证明是考虑如果相邻两段\(s>t\),如果不满足条件即\(s-t>\max\),说明将\(s\)和\(t\)交界处......
  • AtCoder Beginner Contest 271
    咕咕咕咕。E-SubsequencePath最短路问题变种,Dijkstra最短路改改就行了。AC代码//Problem:E-SubsequencePath//Contest:AtCoder-KYOCERAProgrammingC......
  • 「题解」Codeforces 1313E Concatenation with intersection
    考虑这个\(l_1\)一定是\(s\)的开头,\(r_2\)一定是\(t\)的结尾,那么就考虑假如固定了\(l_1,r_2\)之后怎么计算对答案的贡献。一个河狸的想法是,固定\(l_1\)之后可......
  • AtCoder Regular Contest 149
    ARC149A-RepdigitNumber符合条件的数一共只有\(9N\)个,随便怎么做都行。ACCodeARC149B-TwoLISSum这个操作相当于我们可以将\(A\)任意排列,然后对\(B\)进行......
  • AtCoder Regular Contest 149(持续更新)
    Preface最近国庆在外面玩的有点high啊,欠了一篇AT和两篇CF没写,今天先浅浅写一下这场当时10.2号在外面玩得有点晚了,到寝室刚好比赛开始,晚饭都没吃就开干了主要是C写的太久......
  • AtCoder Regular Contest 149
    A发现所有数字都相同的数一共只有\(10^6\)种,考虑枚举每种情况,关键在于如何判断一个数\(\bmodm\)是否为\(0\)。考虑\(9\bmod8=1\),而\(99\bmod8=(9\times10+9)\b......
  • AtCoder Beginner Contest 271
    AtCoderBeginnerContest271D-FlipandAdjust一共有\(N\)张牌,每一面都写着一个整数。卡\(i(1\lei\leN)\)前面写着整数\(a_i\),后面写着整数\(b_i\)。你可......