memo

GitHub: izuna385

AtCoder Beginner Contest 032 C 列 (しゃくとり法)

 C++始めると言ったのに最近精進できていない。D問題に解けるものが無く一つの壁に来た感じがある(典型アルゴやれ)

問題

https://beta.atcoder.jp/contests/abc032/tasks/abc032_c 
部分列に含まれる全ての要素の値の積が、K 以下であるようなものの最長列を求める。

解法

しゃくとり法が手っ取り早い

コード(Python

微調整を繰り返したので、釈然としないしゃくとり法になった。

他にもどこをインデックスとして回すかは問題に依るので、もっと柔軟に書けるようにしておく必要がある。

n,k = map(int,input().split())
sl = []
for i in range(n):
    sl.append(int(input()))
left = 0 
right = 0    
kouho = 0
seki = 1
for i in range(n):
    
    if sl[i] == 0:
        break
    
    right = i
    if i == 0:
        seki = sl[left]
    else:
        seki = seki * sl[right]
        
    if seki <= k:
        kouho = max(kouho,right-left+1)
        continue
    else:
        while left <= right and seki > k:
            p = left
            seki = seki / sl[p]
            left = left + 1

if 0 in sl:
    kouho = n
print(kouho)

AtCoder Beginner Contest 054 C One stroke pass

問題

https://beta.atcoder.jp/contests/abc054/tasks/abc054_c

無向グラフについて、一筆書きの経路が有るかを判定する。

 

やった方法

節点の数が少ないので、今回は深さ優先探索を行った。

 

コード

簡単な部分について、内包表記を少しずつ取り入れるようにしています。

n,m = map(int,input().split())
H = [[0 for _ in range(n)] for _ in range(n) ]

for _ in range(m):
    a, b = map(int,input().split())
    H[a-1][b-1] = 1
    H[b-1][a-1] = 1
    
l = [0 for _ in range(n)]
ans = 0

def dfs(node,visited):
    global ans
    if visited.count(0) == 0:
        ans += 1 
        return 0
    else:
        visited[node] = 1
        for node_,edge_ in enumerate(H[node]):
            if edge_ == 1 and visited[node_] != 1:
                visited[node_] = 1
                visited[node] = 1
                dfs(node_,visited)

                visited[node_] = 0
dfs(0,l)
print(ans)

AtCoder Beginner Contest 030 C-飛行機乗り

問題

https://beta.atcoder.jp/contests/abc030/tasks/abc030_c

 条件を満たしつつ飛行機をうまく乗り継いで、最善で往復した場合にどれだけ往復できるかを試す。

 試していないが、毎回、今いる空港の次にいる空港で、条件を満たすもので最小の便を探した場合、TLEすると考えられる。

 今回は、簡単な工夫ではあるものの、delを用いて探索要素を減らすことで解決しようとした。

 最善をつくすと言う意味で貪欲法?になるのだろうか。 

コード

 Pythonを用いた。

 次の空港に乗り継ぐ表現として、今回は二次元配列の2番目に0,1を用いた。

 改善できる部分は多そう。。。

 

a,b = map(int,input().split())
x,y = map(int,input().split())

a_port = list(map(int,input().split()))
b_port = list(map(int,input().split()))

all_d = []

for i in range(len(a_port)):
    all_d.append([a_port[i], 1])

for j in range(len(b_port)):
    all_d.append([b_port[j] , 0])
    
all_d_s = list(sorted(all_d, key=lambda x: x[0]))

counts = 0
times = a_port[0]
here = 1

while times <= max(b_port):
    jj = 0
    countsbefore = counts
    for j in range(len(all_d_s)):
        if times < all_d_s[j][0]:
            if all_d_s[j][1] == (here + 1) % 2 and all_d_s[j][1] == 0 and all_d_s[j][0] - times >= x :
                here = (here + 1) % 2
                times = all_d_s[j][0] 
                counts += 1
                jj = j
            elif all_d_s[j][1] == (here + 1) % 2 and all_d_s[j][1] == 1 and all_d_s[j][0] - times >= y :
                here = (here + 1) % 2
                times = all_d_s[j][0] 
                counts += 1
                jj = j
            else:
                pass
        else:
            continue
    if here <= jj:
        del all_d_s[here:jj]
    if counts == countsbefore:
        break
    
print((counts +1 ) // 2)                
    

 

改善点あればコメント頂けると大変嬉しいです。。。