Virtually participated in M-SOLUTIONS Procon Open 2020. It was ABCD's 4 question AC. I'm using Python3.
Conditional branch with if.
import sys
def input():
return sys.stdin.readline()[:-1]
def main():
X = int(input())
if X >= 1800:
print(1)
elif X >= 1600:
print(2)
elif X >= 1400:
print(3)
elif X >= 1200:
print(4)
elif X >= 1000:
print(5)
elif X >= 800:
print(6)
elif X >= 600:
print(7)
else:
print(8)
if __name__ == "__main__":
main()
Operate until the conditions are met, and if the number of operations is K or less, it is considered that the magic was successful.
This is possible because the input A, B, C, K are small.
Since Mr. M performs the operation of doubling the number, it can be seen that he does not operate on A, which should be the smallest number.
In other words, consider the value of A fixed.
In the while statement, operate so that B is larger than A.
This fixes the value of B.
Similarly, you can find C larger than B with a while statement.
import sys
def input():
return sys.stdin.readline()[:-1]
def main():
A, B, C = map(int,input().split())
K = int(input())
count = 0
while A >= B:
B = B * 2
count += 1
while B >= C:
C = C * 2
count += 1
if count <= K:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
This question only needs to determine if the score is higher or lower than in the previous semester, not the actual score.
The grade for the N semester is calculated by A [0] * A [1] ~ * A [N-1].
The score for the N + 1 semester is calculated by A [1] ~ * A [N-1] * A [N].
It is enough to compare A [0] and A [N] because we only compare the high and low scores.
This is repeated with a for statement, and N-K results are displayed.
import sys
def input():
return sys.stdin.readline()[:-1]
def main():
N, K = map(int,input().split())
A = list(map(int,input().split()))
for i in range(N - K):
if A[i] < A[i + K]:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
The basic goal is to buy when the stock price is low and sell when the stock price is high.
First, compare the i-th and i + 1-th, and make a list of 1 if it is larger and -1 if it is smaller. (Use of list comprehension notation)
When it is 1, I will buy as much as I can.
If 1 continues, it means that the stock price goes up, so it is OK to buy at the point where it became 1 at the beginning.
Similarly, when it reaches -1, all stocks are sold.
If -1 continues, it means that the stock price will go down, so it is OK to sell at the point where it first became -1.
import sys
def input():
return sys.stdin.readline()[:-1]
def main():
N = int(input())
A = list(map(int,input().split()))
zougen = [-1 if A[i] > A[i + 1] else 1 for i in range(N - 1)]
money = 1000
kabu = 0
for i in range(N-1):
if zougen[i] == 1:
kabu += money // A[i]
money = money % A[i]
else:
money += kabu * A[i]
kabu = 0
print(A[-1] * kabu + money)
if __name__ == "__main__":
main()