mitosoupのブログ

何かあれば、お気軽にご連絡ください。twitter:@mitsoup

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)                
    

 

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