0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtCoderをPythonで取り組んだ振り返り(ABC432 A~B)

Posted at

どうもこんにちは!

今週のコンテストはBまで完答、振り返りもBまで。
Cがわかりそうでわからなかったです・・・最初は全員に大きい飴を渡すとして、その中で最小値に近づけるように小さい飴に切り替えて、一致しなかったら次の最小値にあわせていくというのを考えたんですが、計算量が多そうなんですよねぇ。
Eも同様で書かれている通りに実装してもTLE。リストの値を変更したときに何かの操作をしておけば計算量落とせそうかなとは思いましたがそこまで。

問題と公式の解説のリンク

問題は以下のリンクから。

A - Permute to Maximize -

問題

3つの数字が与えられ、それを組み合わせて3桁の整数を作る場合の最大値を答える問題。

考え方とコード

3つの数字を文字としてリストに入れて逆順にソートして、その順番でつなぎ合わせるとしました。
文字の操作は苦手で、もっと簡単なやり方がある気がします・・・

tmp = list(map(str,input().split()))
tmp.sort(reverse=True)
a,b,c = tmp
print(a+b+c)

B - Permute to Minimize -

問題

整数が与えられるので各桁の数字を先頭は0以外になるように並び替えたときの最小値を答える問題。

考え方とコード

整数の各桁をいったん文字にして昇順にソート、0以外で一番小さい数字を抜き出して、残りを後ろにつないで出力としました。
文字の操作は苦手で、もっと簡単なやり方がある気がします・・・(2回目)

x = int(input())
n = int(input())
w = [int(x) for x in input().split()]
q = int(input())

t = [False] * n
for _ in range(q):
    p = int(input()) - 1
    if t[p] == False:
        t[p] = True
        x += w[p]
    else:
        t[p] = False
        x -= w[p]
    print(x)

ではでは。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?