AtCoder Beginner Contest 182 题解

题意:

AtCoder Beginner Contest 182 题解_第1张图片

题解

利用一个数能被3整除当且仅当其各位之和sum能被3整除。

如果sum本身能被3整除,则不需要删除。

否则统计原数的每一位数%3后的个数,比较%3 =1与%3 =2 的个数,有两种方法可以使其sum变为 %3 =0:

%3=1 与%3=2,相互抵消,还剩下的差值即为答案。

%3=1 与%3=2,先内部消化,%3 =1的 三个一消除,%3 =2的三个一消除,最后再相互抵消,差值即为答案。

IA=lambda:map(int,input().strip().split())s=input()n=len(s)num=[0foriinrange(3)]summ=0foritins:x=int(it)num[x%3]+=1summ=(summ+x)%3ifsumm%3==0:print(0)else:# print(num)cha=abs(num[1]-num[2])num[1]%=3num[2]%=3cha=min(cha,abs(num[1]-num[2]))ifcha==n:print(-1)else:print(cha)

IA = lambda: map(int, input().strip().split())

s = input()

n = len(s)

num = [0 for i in range(3)]

summ = 0

for it in s:

    x = int(it)

    num[x % 3] += 1

    summ = (summ + x) % 3

if summ % 3 == 0:

    print(0)

else:

    # print(num)

    cha = abs(num[1] - num[2])

    num[1] %= 3

    num[2] %= 3

    cha = min(cha, abs(num[1] - num[2]))

    if cha == n:

        print(-1)

    else:

        print(cha)

模拟即可。

记录最远位置maxx,当前位置res,前缀和sum,以及前缀和的最大值r。

在每一轮中,首先更新前缀和,然后更新前缀和的最大值,本轮能达到的最大值显然是res+r,用其更新maxx,再用res+sum更新res。

https://www.iqiyi.com/manhua/search-keyword=%E5%91%BC%E4%BD%A0%E6%AD%BB%E8%BD%AF%E4%BB%B6%E5%9C%A8%E6%B7%98%E5%AE%9D%E4%B8%8A%E5%8F%AB%E4%BB%80%E4%B9%88%E3%80%90%E5%BE%AEnc2206%E3%80%91

https://www.iqiyi.com/manhua/search-keyword=%E5%91%BC%E4%BD%A0%E6%AD%BB%E8%BD%AF%E4%BB%B6%E5%9C%A8%E6%B7%98%E5%AE%9D%E4%B8%8A%E5%8F%AB%E4%BB%80%E4%B9%88%E3%80%90%E5%A8%81nc2206%E3%80%91

https://www.iqiyi.com/manhua/search-keyword=%E5%91%BC%E4%BD%A0%E6%AD%BB%E8%BD%AF%E4%BB%B6%E5%9C%A8%E6%B7%98%E5%AE%9D%E4%B8%8A%E5%8F%AB%E4%BB%80%E4%B9%88%2B%E5%A8%81nc2206

https://www.iqiyi.com/manhua/search-keyword=%E5%91%BC%E4%BD%A0%E6%AD%BB%E8%BD%AF%E4%BB%B6%E5%9C%A8%E6%B7%98%E5%AE%9D%E4%B8%8A%E5%8F%AB%E4%BB%80%E4%B9%88%2B%E8%96%87nc2206

https://www.iqiyi.com/manhua/search-keyword=%E5%91%BC%E4%BD%A0%E6%AD%BB%E8%BD%AF%E4%BB%B6%E5%9C%A8%E6%B7%98%E5%AE%9D%E4%B8%8A%E5%8F%AB%E4%BB%80%E4%B9%88%2B%E5%BE%AEnc2206

https://www.iqiyi.com/manhua/search-keyword=%E5%91%BC%E4%BD%A0%E6%AD%BB%E8%BD%AF%E4%BB%B6%E5%9C%A8%E6%B7%98%E5%AE%9D%E4%B8%8A%E5%8F%AB%E4%BB%80%E4%B9%88%EF%BC%88%E5%A8%81nc2206%EF%BC%89

https://www.iqiyi.com/manhua/search-keyword=%E5%91%BC%E4%BD%A0%E6%AD%BB%E8%BD%AF%E4%BB%B6%E5%9C%A8%E6%B7%98%E5%AE%9D%E4%B8%8A%E5%8F%AB%E4%BB%80%E4%B9%88%EF%BC%88%E5%BE%AEnc2206%EF%BC%89

https://www.iqiyi.com/manhua/search-keyword=%E5%91%BC%E4%BD%A0%E6%AD%BB%E8%BD%AF%E4%BB%B6%E5%9C%A8%E6%B7%98%E5%AE%9D%E4%B8%8A%E5%8F%AB%E4%BB%80%E4%B9%88%EF%BC%88%E5%BE%AE%E8%8A%AFnc2206%EF%BC%89

https://www.iqiyi.com/manhua/search-keyword=%E5%91%BC%E4%BD%A0%E6%AD%BB%E8%BD%AF%E4%BB%B6%E5%9C%A8%E6%B7%98%E5%AE%9D%E4%B8%8A%E5%8F%AB%E4%BB%80%E4%B9%88%EF%BC%88%E5%A8%81%E4%BF%A1nc2206%EF%BC%89

https://www.iqiyi.com/manhua/search-keyword=%E9%AA%9A%E6%89%B0%E7%A5%9E%E5%99%A824%E5%B0%8F%E6%97%B6%E8%BD%B0%E7%82%B8%EF%BC%8C%E5%A8%81nc2206

https://www.iqiyi.com/manhua/search-keyword=%E9%AA%9A%E6%89%B0%E7%A5%9E%E5%99%A824%E5%B0%8F%E6%97%B6%E8%BD%B0%E7%82%B8%EF%BC%8C%E5%A8%81%E4%BF%A1nc2206

https://www.iqiyi.com/manhua/search-keyword=%E9%AA%9A%E6%89%B0%E7%A5%9E%E5%99%A824%E5%B0%8F%E6%97%B6%E8%BD%B0%E7%82%B8%EF%BC%8C%E3%80%90%E5%A8%81%E4%BF%A1nc2206%E3%80%91

https://www.iqiyi.com/manhua/search-keyword=%E9%AA%9A%E6%89%B0%E7%A5%9E%E5%99%A824%E5%B0%8F%E6%97%B6%E8%BD%B0%E7%82%B8%EF%BC%8C%E3%80%90%E8%96%87%E4%BF%A1nc2206%E3%80%91

https://www.iqiyi.com/manhua/search-keyword=%E9%AA%9A%E6%89%B0%E7%A5%9E%E5%99%A824%E5%B0%8F%E6%97%B6%E8%BD%B0%E7%82%B8%EF%BC%8C%E3%80%90%E8%96%87nc2206%E3%80%91

https://www.iqiyi.com/manhua/search-keyword=%E9%AA%9A%E6%89%B0%E7%A5%9E%E5%99%A824%E5%B0%8F%E6%97%B6%E8%BD%B0%E7%82%B8%EF%BC%8C%E3%80%90%E5%A8%81nc2206%E3%80%91

你可能感兴趣的:(AtCoder Beginner Contest 182 题解)