2012-01-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>…

AOJ 10023 - Shuffle

aoj

substrが便利ですね. そして,Problem 100はこれで全て終わりました.2年前はこの程度で唸っていたので成長したと感じています. #include<iostream> int main(){ std::string s; while(std::cin >> s, s != "-"){ int m, i; std::cin >> m; for(;m--;){ std::cin >> </iostream>…

AOJ 10033 - Stacking Block II

aoj

なるべくきれいに書きたかった. 山の番号付けは1-100(x 0-99)ではないことに留意する. #include<iostream> #include<stack> #include<queue> int main(){ std::string command; std::stack<char> stack[101]; std::queue<char> p_queue; while(std::cin >> command, command != "quit"){ if(com</char></char></queue></stack></iostream>…

AOJ 10032 - Stacking Block I

aoj

空白を含むときはgetlineをつかおう. #include<iostream> #include<stack> #include<queue> int main(){ std::string command; std::stack<char> stack; std::queue<char> p_queue; while(std::cin >> command, command != "quit"){ char argument; if(command == "pop"){ p_queue.push(stack.to</char></char></queue></stack></iostream>…

AOJ 10031 - Search II

aoj

さっきと同じコードでとおった. algorithmライブラリを制限したらどうすればいいかを考える. ここの基数ソートを参考にしたんですよ. 限界まで依存するライブラリを減らしたかったからStackをやめたんです. そしたら,全然違うじゃないですか.ちょっと…

AOJ 10030 - Search I

aoj

set_intersection: 積集合をもとめる. 第5変数はback_inserter(D)みたいにする. ようやく一回でとおった. #include<iostream> #include<algorithm> #include<vector> int main(){ int n, q, i, j; std::vector<int> T, Q, D; std::cin >> n; for(i=0;i<n;i++){ std::cin >> j; T.push_back(j); } std::cin >> q;</n;i++){></int></vector></algorithm></iostream>…

AOJ 10029 - Sort II

aoj

あ,要素数足りない: x 降順だろうな: x もうだめだ. #include<iostream> #include<algorithm> int main(){ int n, arr[1000000]; std::cin >> n; for(int i=0;i<n;i++){ std::cin >> arr[i]; } std::sort(arr, arr+n); for(int i=0;i</n;i++){></algorithm></iostream>

AOJ 10028 - Sort I

aoj

PEだったなんて言えない #include<iostream> #include<algorithm> int main(){ int n, arr[1000]; std::cin >> n; for(int i=0;i<n;i++){ std::cin >> arr[i]; } std::sort(arr, arr+n); for(int i=0;i</n;i++){></algorithm></iostream>

AOJ 10027 - Card Game

aoj

文字列の比較は"a"と"abc"ならばaが先(短いほうが先?)になるらしい. main(){ int n, ap=0, bp=0; char a[110], b[110]; scanf("%d", &n); for(;n--;){ scanf("%s", a); scanf("%s", b); int comp = strcmp(a, b); if(comp > 0){ ap += 3; }else if(comp ==…

AOJ 10026 - Standard Deviation

aoj

はm(平均)がわからないと計算できない. 平均をもとめるには全得点がわからないといけない. そうすると,すべての点を配列として保持しないといけない. それはめんどうなので,を利用した. #include<math.h> main(){ int n, i, s; double a, b, c, m, alpha_s; wh</math.h>…

AOJ 10025 - Triangle, math.hをincludeするとき

aoj

math.hをincludeするときは-lmオプションをつける #include <math.h> main(){ double a, b, C; scanf("%lf %lf %lf", &a, &b, &C); double rad = C*M_PI/180, S = a*b*sin(rad)/2, h = b*sin(rad), ar = a+b+sqrt(a*a+b*b-2*a*b*cos(rad)); printf("%lf\n%lf\n%lf\n"</math.h>…

AOJ 1041 - Kyudo: A Japanese Art of Archery

菫さんに影響されてやりたくなった競技。 C言語 #include<stdio.h> s; main(n,m){ while(scanf("%d",&n),n/=4){ s=0; for(;n--;) scanf("%d",&m),s+=m; printf("%d\n",s); } return 0; }</stdio.h>

AOJ 10017 - How many ways?

aoj

O(n^3)だったはず。n>=1000でTLEするはず。 #include<iostream> int CountCombination(int n, int x){ int res = 0; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ for(int k=1;k<=n;k++){ if(i+j+k == x && i != j && i != k && j != k){//i,j,kに重複がないように</iostream>…

AOJ 10022 - Finding a word

aoj

大文字と小文字を区別しない。読んでない。 単語ごと。読んでない。 小さな液晶に2つのウィンドウ表示するので見落としがちです その代わり、transformという関数を知ったのでよかったです。 ラムダ式もつかいました。便利すぎます。 #include<iostream> #include<algorithm> #in</algorithm></iostream>…

AOJ 10015 - Finding Missing Cards

aoj

AOJ先生が4x13の配列つくるといいよと言ってくれましたが、俺はこのset_differenceを使うぜ。 まず、すべてのカードの配列をつくります。 次に、持っているカードの配列をつくります。 その差は失くしたカードになります。set_differenceの第5引数は第一のit…

AOJ 0001 - List of Top 3 Hills

AOJって他者のコードが見られるのですね. よく考えても解法が浮かばない場合使わせて頂きます. i = 0 arr = list() while i<10: j = int(raw_input()) arr.append(j) i+=1 for h in sorted(arr, reverse = True)[:3]: print h 先人(6人)の力を借りた.即興…

AOJ 0000 - QQ

いつの間にかPythonとRuby,PHPがAOJで使えるようになってた. 使ったことのないPythonを使ってみた. y = 1 while y<10: x = 1 while x<10: print str(y)+'x'+str(x)+'='+str(y*x) x += 1 y += 1