<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>KazukiTheShitのコード日記</title>
<link>https://ameblo.jp/kazukitheshit/</link>
<atom:link href="https://rssblog.ameba.jp/kazukitheshit/rss20.xml" rel="self" type="application/rss+xml" />
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com" />
<description>学生、プログラミング独学、備忘録、javaから始めて今はほとんどpython、まだまだヘボだが頑張る</description>
<language>ja</language>
<item>
<title>python、プロコン、二分探索</title>
<description>
<![CDATA[ <p>今日は二分探索の練習問題。</p><p>二分探索とは、予めソートされている列に対して、中央値と探索対象の値の大小を比べることで探索範囲を半分づつに絞っていく方法。</p><p>計算量はO(logn)で、O(n)の線形探索に比べてだいぶはやい。</p><p>&nbsp;</p><p>問題は<a href="http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_4_B&amp;lang=jp" target="_blank">これ</a>。</p><p>んで解答。</p><p>&nbsp;</p><p>n = int(input())</p><p>s = [int(x) for x in input().split()]</p><p>q = int(input())</p><p>t = [int(x) for x in input().split()]</p><p>c = 0</p><p>&nbsp;</p><p>for i in t:</p><p>&nbsp; &nbsp; left = 0</p><p>&nbsp; &nbsp; right = n</p><p>&nbsp; &nbsp; mid = lambda x,y:(x + y)/2</p><p>&nbsp; &nbsp; while left &lt; right:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; if s[int(mid(left,right))] == i:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c += 1</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break</p><p>&nbsp; &nbsp; &nbsp; &nbsp; elif s[int(mid(left,right))] &gt; i:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; right = int(mid(left,right))</p><p>&nbsp; &nbsp; &nbsp; &nbsp; else:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left = int(mid(left,right)) + 1</p><p>&nbsp;</p><p>print(c)</p><p>&nbsp;</p><p>&nbsp;</p><p>そろそろpython用のアルゴリズム本なり、最適化本なり買わないとどこで差がついてるのイマイチわからん。</p><p>これでだいたい1.3secとかで１位は0.03sec。</p><p>まあ上位の人達はset型の関数とか使ってるんでぶっちゃけ二分探索はしていないくせこである。</p><p>&nbsp;</p><p>おしまい。</p>
]]>
</description>
<link>https://ameblo.jp/kazukitheshit/entry-12319331800.html</link>
<pubDate>Fri, 13 Oct 2017 23:51:48 +0900</pubDate>
</item>
<item>
<title>python、プロコン問題、線形探索</title>
<description>
<![CDATA[ <p>今日は線形探索の練習問題。</p><p>&nbsp;</p><p>かんたんすぎるが練習なのでご愛嬌。</p><p>&nbsp;</p><p>問題は<a href="http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_4_A&amp;lang=jp" target="_blank">これ</a>。</p><p>&nbsp;</p><p>解答コード。</p><p>&nbsp;</p><pre>n = int(input())S = [int(i) for i in input().split()] + [0]q = int(input())T = [int(i) for i in input().split()]c = 0for i in T:    S[n] = i    j = 0    while i != S[j]:        j += 1    if j != n:        c += 1print(c)</pre><pre></pre><pre>解答の肝は番兵と呼ばれる探索対象配列末尾に設置される目的キー</pre><pre>と同じデータ。</pre><pre>これによって探索が終わることが保証されるので一回一回の探索で等</pre><pre>価演算を行わなくて済むというメリットが有る。</pre><pre></pre><pre>これでだいたい0.11sec。</pre><pre>一位は0.02sec。</pre><pre>なんでこんなに差が出るのかしらべたところ、一位の人のコードは</pre><pre>配列読み込みのときにリスト内包表記ではなくmapをつかっていた。</pre><pre>てっきり内包表記のほうが早いのかと思っていたのだがどうやらそうで</pre><pre>もないらしい。</pre><pre>戻り値がリストなのかイテレータなのかでmapの方が最大６倍近く速く</pre><pre>なるそうだ。</pre><pre>詳しいことは</pre><pre><a href="http://utgwkk.hateblo.jp/entry/2017/03/09/154314">Python の map とfor内包表記(リスト内包表記)は結局どっちが速い？</a></pre><h1><a href="http://pheromone.hatenablog.com/entry/20110521/1305964314">mapと内包表記の速度の差について</a></h1><div>を読んで下さい。</div><div>&nbsp;</div><div>おしまい。</div>
]]>
</description>
<link>https://ameblo.jp/kazukitheshit/entry-12318489561.html</link>
<pubDate>Tue, 10 Oct 2017 23:38:50 +0900</pubDate>
</item>
<item>
<title>pythonで解く数値解析　ニュートン法続き</title>
<description>
<![CDATA[ <p>今日はニュートン法の続きです。</p><p>&nbsp;</p><p>関数はy = cosh(x)cos(x)-1。</p><p>コードはこの間のやつを関数部分を少し変更しただけだから割愛。</p><p>&nbsp;</p><p>グラフはこんな感じ。<a href="https://stat.ameba.jp/user_images/20171009/15/kazukitheshit/82/e8/p/o0800055014045024712.png"><img alt="" height="289" src="https://stat.ameba.jp/user_images/20171009/15/kazukitheshit/82/e8/p/o0800055014045024712.png" width="420"></a></p><p>注意は適切に初期値を選ばないと違う方の解に収束してしまうとこ。</p><p>&nbsp;</p><p>次は多分２変数のニュートン法。</p><p>&nbsp;</p><p>おしまい。</p><p>&nbsp;</p>
]]>
</description>
<link>https://ameblo.jp/kazukitheshit/entry-12318049167.html</link>
<pubDate>Mon, 09 Oct 2017 15:51:47 +0900</pubDate>
</item>
<item>
<title>pythonで解く数値解析-ニュートン法-</title>
<description>
<![CDATA[ <p>今期の授業に数値解析と言うものがある。</p><p>数値解析とは代数的に解くことが難しい方程式を、初期予想値から解に近づいていく反復関数を用いて近似解を求めるもの。</p><p>&nbsp;</p><p>今回はその１手法であるニュートン法と言うものを用いてf(x)=x^3-125の近似解を求める。</p><p>&nbsp;</p><p>ニュートン法とは詳しくは自分で調べてほしいのだが、</p><p>新しい近似解xを一つ前の近似解x'を用いて</p><p>x=x'-f(x')/f'(x')によって反復生成していくという方法。</p><p>&nbsp;</p><p>反復法はめちゃめちゃ汚い数が出てくるプログラミングの出番である。</p><p>&nbsp;</p><p>以下がそのコード。</p><p>&nbsp;</p><pre>from matplotlib import pyplot as pltimport numpy as npxs = []def f(x):    return x**3 - 125def f_prime(x):    return 3*x**2def newton(ini,e):    xs.append(ini)    count = 0    while True:        count += 1        nx = xs[-1] - f(xs[-1])/f_prime(xs[-1])        xs.append(nx)        if abs(xs[-1] - xs[-2]) &lt; e:            breakdef tangent(x,a):    return f_prime(a)*(x-a) + f(a)newton(10,1/100000000)print(xs)x = np.arange(-1.2*max(xs),1.2*max(xs),0.1)plt.ylim(-1.2*f(max(xs)), 1.2*f(max(xs)))plt.xlim(xs[-1] - max(xs),1.2*max(xs),0.1)for i in xs:    y = tangent(x,i)    plt.plot(x,y)y = f(x)y0 = 0*xplt.plot(x,y)plt.plot(x,y0)plt.show()ちなみにこれは収束している様子をプロットするために色々しているので少し長い。</pre><pre>本当は収束条件とか色々細かいことがあるのだがそれを自作するのは骨が折れるため今回はパス。</pre><pre><a href="https://stat.ameba.jp/user_images/20171007/19/kazukitheshit/e4/18/p/o0640048014043679910.png"><img alt="" height="315" src="https://stat.ameba.jp/user_images/20171007/19/kazukitheshit/e4/18/p/o0640048014043679910.png" width="420"></a>初期値が１０でこんな感じ。</pre><pre>結構きれい。</pre><pre>次はもっと難しい方程式でやってみようかな。</pre>
]]>
</description>
<link>https://ameblo.jp/kazukitheshit/entry-12317515494.html</link>
<pubDate>Sat, 07 Oct 2017 19:08:44 +0900</pubDate>
</item>
<item>
<title>python、スタック、プロコン問題</title>
<description>
<![CDATA[ <p>最近はプロコン用のアルゴリズム本を解いている。</p><p>&nbsp;</p><div contenteditable="false" style="border:1px dotted;padding:15px;border-radius:4px;"><table border="0" cellpadding="0" cellspacing="0" style="margin:0;table-layout:fixed;" width="100%"><tbody width="100%"><tr><td aligin="center" style="vertical-align:middle;" width="95"><span style="display:block;text-align:center;"><a alt0="BlogAffiliate" href="http://click.affiliate.ameba.jp/affiliate.do?affiliateId=35647771" rel="nofollow" target="_blank"><img alt="プログラミングコンテスト攻略のためのアルゴリズムとデータ構造" border="0" data-img="affiliate" src="https://images-fe.ssl-images-amazon.com/images/I/51oWwpzibRL._SL160_.jpg" style="max-width:95px;vertical-align:middle;margin:0;"></a></span></td><td style="line-height:1.5;padding-left:15px;vertical-align:middle;"><a alt0="BlogAffiliate" href="http://click.affiliate.ameba.jp/affiliate.do?affiliateId=35647771" rel="nofollow" target="_blank">プログラミングコンテスト攻略のためのアルゴリズムとデータ構造</a><div style="padding: 3px 0;">&nbsp;</div><div style="font-size:0.83em;">Amazon</div></td></tr></tbody></table></div><p>これ。</p><p>解答はcかc++。</p><p>まあ楽なんでpythonで解いてますけどね。</p><p>&nbsp;</p><p>問題は水がたまる部分の面積計算のやつ。</p><h1><a href="http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_D&amp;lang=jp" target="_blank">Areas on the Cross-Section Diagram</a></h1><p>'\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\'の形で入力される断面データから水たまりの総面積と各面積を求める問題。</p><p>いろんな解き方がありそうだけど素直に解答に従ってスタックを用いて解く。</p><p>&nbsp;</p><pre><code>s1 = []   #/の位置を積んでいくスタックs2 = []   #各々の水たまりの面積を[一番左の\の位置、その水たまりの面積]の形で積んでいくスタックsu = 0   #総面積st = list(input())   #入力を一文字ずつ格納したリストfor i in range(len(st)):   #実際に一文字ずつ読み取って総面積計算    if st[i] == '\\':        s1.append(i)    elif st[i] == '/' and len(s1) != 0:        left = s1.pop()        su += i - left        area = 0        while len(s2) != 0 and left &lt; s2[-1][0]:            area += s2.pop()[1]        s2.append([left,area + i -left])print(su)print(' '.join([str(len(s2))] + [str(i[1]) for i in s2]))</code></pre><pre></pre><p>&nbsp;</p><p>&nbsp;</p><p>最初は面積を普通に三角形の公式で求めるのかと思っていたけど同じ行にある部分の面積は端同士の距離で表せる事が判明。</p><p>&nbsp;</p><p>しゅごい。</p><p>&nbsp;</p><p>ジャッジに出したらだいたい0.06secくらいだった。</p><p>一位は0.02sec。</p><p>どこで差がついてるのだろうか。</p><p>リスト内包表記とかあんま使わないほうがいいのだろうか。</p><p>誰かおしえてくだぱい。</p><p>&nbsp;</p>
]]>
</description>
<link>https://ameblo.jp/kazukitheshit/entry-12317310364.html</link>
<pubDate>Fri, 06 Oct 2017 23:47:34 +0900</pubDate>
</item>
</channel>
</rss>
