[PYTHON] Solve AtCoder Beginner Contest 100-102

AtCoder Beginner Contest 100 A - Happy Birthday! E869120 You and square1001 Your 16th birthday is coming soon. Therefore, Mr. Takahashi of the Kingdom of AtCoder gave them a circular cake with radial cuts and 16 equal parts. E869120 You tried to eat A pieces, square1001 you tried to eat B pieces. However, when I looked at the paper that came with the cake, it said, "The same person must not take both two adjacent pieces of cake." Now, can they both take as many cakes as they want to eat, keeping what is written on the paper? A and B are integers between 1 and 16

hb.py


A,B = map(int,input().split())
if A <= 8 and B<=8:
  print('Yay!')
else:
  print(':(')

B - Ringo's Favorite Numbers Today, the memorable AtCoder Beginner Contest 100 will be held. Therefore, Mr. Takahashi decided to give Mr. Apple an integer. Today's contest is "At Coder Beginner Contest 100", so Apple would be happy to receive a positive integer that can be divided exactly D times by 100. Now, find the Nth smallest integer that you would be happy to receive as a gift.

ringo.py


D,N = map(int,input().split())
if N<100:
  print(100**D*N)   
else:
  print(100**D*(N+1))

Note that when N = 100, the number of breaks increases by one.

C - *3 or /2 AtCoder will display a sequence of length N a = {a1, a2, a3, ..., aN}. Employee Sunuke decided to play with this sequence. Specifically, I wanted to repeat the following operations as many times as possible. For all i that satisfy 1 ≤ i ≤ N, either "divide the value of ai by 2" or "multiply the value of ai by 3". However, it cannot be tripled for all i, and the value of ai after the operation must be an integer. Find out how many operations can be performed at the maximum.

3or2.py


N = int(input())
a = list(map(int, input().split()))
#a = [5,2,4]
count=0
for num in a:
  while(num%2==0):
    num//=2
    count+=1
   
print(count)

Find the total number of times you can continue to divide by the number 2 in the list.

D - Patisserie ABC The ABC pastry shop sells N kinds of cakes. Each type of cake has three values, "beauty", "deliciousness", and "popularity", and the beauty of the i-type cake is xi, the deliciousness is yi, and the popularity is zi. Is It can be less than or equal to 0. Mr. Ringo decided to eat M cakes at the ABC pastry shop. He decided to choose a combination of cakes to eat as follows. *** Do not eat more than one cake of the same type. *** Select so that (absolute value of total beauty) + (absolute value of total deliciousness) + (absolute value of total popularity) is maximized while satisfying the above conditions. At this time, find the maximum value of (absolute value of total beauty) + (absolute value of total deliciousness) + (absolute value of total popularity) of the cake selected by Mr. Apple.

pati.py


from itertools import product
N,M = map(int,input().split())
xyz=[list(map(int,input().split())) for _ in range(N)]
x=[]
for xs,ys,zs in xyz:
  x.append(xs)
 
def pati(N,M,xyz):
  ans=0
  for i in product((-1,1),repeat=3):
    xyz.sort(key=lambda x:x[0]*i[0]+x[1]*i[1]+x[2]*i[2],reverse=True)
    x, y, z = 0, 0, 0
    for j in range(M):
      x+=xyz[j][0]
      y+=xyz[j][1]
      z+=xyz[j][2]
    ans = max(ans,abs(x)+abs(y)+abs(z))
  print(ans)
 
pati(N,M,xyz)

Sort by each of the 8 ways (1,1,1) * (+,-) and find max.

AtCoder Beginner Contest 101 A - Eating Symbols Easy At first, the integer that Takahashi thinks of is 0. From now on, Takahashi is about to eat a total of four + or-symbols. When Takahashi eats +, the integer he has in mind increases by one. On the other hand, when Takahashi eats-, the integer he thinks of is one less. The symbol Takahashi is trying to eat is given by the string S. The i-th letter of S is the i-th symbol that Takahashi eats. After eating all the symbols, find the integer that Takahashi thinks of.

ese.py


s = input()
#s=++-+
ans=0
for i in s:
  if i == '+':
    ans+=1
  else:
    ans-=1
print(ans)

B - Digit Sums
For the integer n, let S (n) represent the sum of each digit when n is expressed in decimal notation. For example, S (101) = 1 + 0 + 1 = 2. Given the integer N, determine if N is divisible by S (N).

DigitSum.py


N=int(input())
div=0
for i in str(N):
  div += int(i)
if N%div == 0:
  print('Yes')
else:
  print('No')

C - Minimization There is a sequence of length N, A1, A2, ..., AN. Initially, the elements of this sequence are 1,2, ... , N are rearranged. Sunuke can do the following for this sequence: Select K consecutive elements from the sequence. After that, the value of each selected element is replaced with the minimum value among the selected elements. Sunuke wants to make all the elements of this sequence equal by repeating the above operation several times. Find the minimum number of operations required. Under the constraints of this problem, we can prove that such a thing is always possible.

min.py


N,K = map(int,input().split())
ans = 0
while(N>0):
  if N==K: 
    ans+=1
    break
  N-=(K-1) 
  ans+=1
print(ans)

D - Snuke Numbers For the integer n, the sum of each digit when n is expressed in decimal notation is represented by S (n). For example, S (123) = 1 + 2 + 3 = 6. A positive integer n for which n/S (n) ≤ m/S (m) holds for any positive integer m such that m> n is called a sneak number. To. Given the integer K, enumerate K numbers from the smallest number.

sN.py


K= 100

def snuke(N):
  out=0
  for i in str(N):
    out+=int(i)
  return N/out
  
div = []
for i in range(1,10000):
  div.append(snuke(i))
max_num=snuke(K)
count=0
#print(div)
for i,n in enumerate(div):
  if n <= min(div[i+1:]):
    print(i+1)
    count+=1
    if count == K: break

I output it steadily, but TLE. Rewrite the code from the law of output values. Turn in the future

AtCoder Beginner Contest 102 A - Multiple of 2 and N A positive integer N is given. Find the smallest positive integer that is divisible by either 2 or N.

mo2n.py


N = int(input())
if N%2==0:
  print(N)
else:
  print(2*N)

B - Maximum Difference Given an integer sequence A of length N. Find the maximum absolute value of the difference between two different (subscripted) elements of A.

MD.py


N = int(input())
a = list(map(int,input().split()))
print(max(a)-min(a))

C - Linear Approximation Sunuke has an integer sequence A of length N. Sunuke is free to choose the integer b. At this time, if Ai and b + i are separated, you are sad. More specifically, the value of Sunuke's sadness is calculated by the following formula. Here, abs (x) is a function that returns the absolute value of x. abs(A1−(b+1))+abs(A2−(b+2))+...+abs(AN−(b+N)) Find the minimum value of your sadness.

LA.py


import statistics
N=int(input())
a=list(map(int,input().split()))
for i in range(N):
  a[i]-=(i+1)
a.sort()
med=a[N//2]
 
ans=0
for i in range(N):
  ans+= abs(a[i]-med)
print(ans)

D - Equal Cut Sunuke has an integer sequence A of length N. Sunuke cuts A in three places and breaks it down into four (non-empty) contiguous subcolumns B, C, D, E. You can freely choose the cutting position. Here, for the integer sequences B, C, D, and E, let P, Q, R, and S be the sum of the elements, respectively. Sunuke is happy that the smaller the absolute value of the difference between the maximum and minimum values ​​of P, Q, R, S is. Find the smallest possible absolute value of the difference between the maximum and minimum values ​​of P, Q, R, and S.

EC.py


N=int(input())
a=list(map(int,input().split()))
 
sum_a = sum(a)
for i in range(1,N):
  a[i]+=a[i-1]
  
res=float('inf')
l=0
r=2
for c in range(1,N-2):
  r = max(r, c+1)
  
  while l+1<c and abs(a[c]-2*a[l])>=abs(a[c]-2*a[l+1]):
    l+=1
  while r+1<N and abs(a[-1]-2*a[r]+a[c])>=abs(a[-1]-2*a[r+1]+a[c]):
    r+=1
    
  x=[a[l],a[c]-a[l],a[r]-a[c],a[-1]-a[r]]
  x.sort()
  res=min(res,x[3]-x[0])
print(res)

Recommended Posts

Solve AtCoder Beginner Contest 100-102
AtCoder Beginner Contest 177
AtCoder Beginner Contest 179
AtCoder Beginner Contest 172
AtCoder Beginner Contest 180
AtCoder Beginner Contest 173
Atcoder Beginner Contest 153
AtCoder Beginner Contest 152 Review
AtCoder Beginner Contest 181 Note
AtCoder Beginner Contest 187 Note
AtCoder Beginner Contest 160 Review
AtCoder Beginner Contest 178 Review
AtCoder Beginner Contest 180 Note
AtCoder Beginner Contest 166 Review
AtCoder Beginner Contest 167 Review
AtCoder Beginner Contest 182 Note
AtCoder Beginner Contest 164 Review
AtCoder Beginner Contest 169 Review
AtCoder Beginner Contest 181 Review
AtCoder Beginner Contest 171 Review
AtCoder Beginner Contest 182 Review
AtCoder Beginner Contest 180 Review
AtCoder Beginner Contest 156 WriteUp
AtCoder Beginner Contest 177 Review
AtCoder Beginner Contest 168 Review
AtCoder Beginner Contest 179 Review
AtCoder Beginner Contest 167 Memorandum
AtCoder Beginner Contest 172 Review
AtCoder Beginner Contest 183 Note
AtCoder Beginner Contest 176 Review
AtCoder Beginner Contest 184 Note
AtCoder Beginner Contest 175 Review
AtCoder Beginner Contest 174 Review
AtCoder Beginner Contest 153 Review
AtCoder Beginner Contest 156 Review
AtCoder Beginner Contest 161 Review
AtCoder Beginner Contest 170 Review
AtCoder Beginner Contest 165 Review
AtCoder Beginner Contest 173 Review
AtCoder Beginner Contest 188 Note
AtCoder Beginner Contest 155 Review
AtCoder Beginner Contest 162 Review
AtCoder Beginner Contest 181 Participation Report
AtCoder Beginner Contest 175 Virtual entry
AtCoder Beginner Contest 161 Participation Report
AtCoder Beginner Contest 151 Participation Report
AtCoder Beginner Contest 176 Participation Report
AtCoder Beginner Contest 154 Participation Report
AtCoder Beginner Contest # 003 Participation Note
AtCoder Beginner Contest 166 Participation Report
AtCoder Beginner Contest # 002 C Problem
AtCoder Beginner Contest 145 Participation Report
AtCoder Beginner Contest 184 Participation Report
AtCoder Beginner Contest 165 Participation Report
AtCoder Beginner Contest 160 Participation Report
AtCoder Beginner Contest 169 Participation Report
AtCoder Beginner Contest 159 Participation Report
AtCoder Beginner Contest 164 Participation Report
AtCoder Beginner Contest 168 Participation Report
AtCoder Beginner Contest 150 Participation Report
AtCoder Beginner Contest 158 Participation Report