2012-12-01から1ヶ月間の記事一覧

AOJ 0087 - Strange Mathematical Expression

処理するのが面倒な問題ですね.Rubyさんで解きました. C++さんでも解けるようにしたいです. def plus(stack) a = stack.pop b = stack.pop stack.push(b+a) end def minus(stack) a = stack.pop b = stack.pop stack.push(b-a) end def times(stack) a = …

AOJ 0038 - Poker Hand

いまいちのコードのきれいさ def straight?(cards) _cards = cards.dup _cards[0] += 13 if _cards[0] == 1 && _cards[1] == 10#1,10,?,?,?だったら14,10,11,12,13に絞って良い _cards.sort! #再ソート f = true for i in 1..4 #1,2,3,4,5などの連番判定 f =…

AOJ 0058 - Orthogonal

aoj

直線なので内積つかえばよい. 線分ならば交点があるか調べないといけないはず. 汚いコード #include<iostream> #include<cstdio> struct P{ double x, y; }; P pointToVector(P p1, P p2){ P p = {p2.x-p1.x, p2.y-p1.y}; return p; } double dotProduct(P p1, P p2){ return</cstdio></iostream>…

AOJ 0030 - Sum of Integers

aoj

選んだものが増加していくように選ぶと重複がなくなる. #include<iostream> int res = 0; //least_n: 選べる最小のn void rec(int n, int s, int least_n){ if(n == 1 && least_n <= s && s < 10){ res++; } for(int i=least_n;i<10;i++){ rec(n-1, s-i, i+1); } } in</iostream>…

AOJ 0023 - Circles Intersection

aoj

円Aの中心と円Bの中心間の距離をdとすると, 重ならない 外接する 2点で接する 内接する AがBを内包する BがAを内包する となる. #include<iostream> #include<cmath> #include<cstdio> struct P{ double x, y; }; struct Circle{ P cp; double r; }; double dist(Circle a, Circle b</cstdio></cmath></iostream>…

AOJ 0015 - National Budget

aoj

C++だと多倍長を独自実装しないといけないのでだるいです. RubyがAOJで書けるようになったので書きました.短くていいですね.*1 n = gets.to_i for i in 1..n a = gets.to_i b = gets.to_i c = a + b if c.to_s.length > 80 puts "overflow" else puts c e…

AOJ 0558 - Cheese

aoj

チーズの数をcnとすると, (S->1までの最短路) + (1->2までの最短路) + ... + (cn-1->cn)までの最短路を求めればいい. それはbfsでできる. #include<iostream> #include<queue> struct P{ int x, y; }; const int MAX_H = 1000, MAX_W = 1000, MAX_CN = 10, INF = 1<<24; in</queue></iostream>…

AOJ 0179 - Mysterious Worm

aoj

いつNAになるかというと虫が巡回するとき(虫A->...->虫A)です. ただのDFSBFS.これははずかしい #include<iostream> #include<queue> #include<map> struct Bug{ int red, blue, green, days; std::string body; }; int main(){ std::string s; while(std::cin >> s, s != "0"){ i</map></queue></iostream>…

JOI 2012 - 暑い日々(問題4)

精進が必要だと思った問題. dpだとは思っていましたが,鍛錬不足で全探索していた屑です. 前提として,C[n]をn番目の服の派手さとします. i日目にj番目の服を着るときの価値の最大をdp[i][j]とします. i-1日目にkという服を来たとして, dp[i][j] = max(…

AOJ 0157 - Russian Dolls

aoj

この1年は溝に捨てたと気づいたので,今頃精進してます. DPで解きました.rを昇順で並べれば半径の条件は満たせます.(半径が同一のときがあるので結局調べないといけませんが) あとはhをうまく選んで多くのマトリョーシカを入れたいです. これは最長部分…

AOJ 0535 - 薄氷渡り

aoj

最長経路ってどう解くんだろうとか思って1時間ぐらい唸っていました. 渋々解説を読んだら,全探索に近いことをしていて萎えました. なので,全探索を書こうと思ったら難しい. 何とかして書いたコードは汚い. #include <iostream> #include <algorithm> #include <cstring> const int M</cstring></algorithm></iostream>…

AOJ 0567 - 最高のピザ

aoj

大きいトッピングから選べばいいんだろうなと考えてました. 何かわけわからない計算したら条件式ができてました. 価値が下がるか計算するだけでいいと気づきましたが,後の祭りでした. #include <iostream> #include <vector> #include <algorithm> int main(){ int n, a, b, dc; std::</algorithm></vector></iostream>…

AOJ 0556 - タイル

aoj

競技プログラミングはじめたとき見た問題でしたが, ただ計算するだけなので簡単でした.(数字の扱いは難しいですが) #include <iostream> #include <cstdio> void swap(int& x, int& y){ x ^= y; y ^= x; x ^= y; } int main(){ int n, m; std::cin >> n >> m; for(;m--;){ in</cstdio></iostream>…

AOJ 0527 - 碁石並べ

aoj

書きにくかったです.ノートで考えながら解きました. これで1問目(JOI2008本戦)なんですね. #include <iostream> #include <stack> //[begin, end)がcolor struct Range{ int begin, end, color; }; int main(){ int n; while(std::cin >> n, n){ std::stack<Range> rs; int c; st</range></stack></iostream>…