Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added solutions to codejam 2012 qual round.

  • Loading branch information...
commit 14f3f53e6ce849dacd9144e9e87853ddc4ecee9a 1 parent 8050ed0
@smly authored
Showing with 109 additions and 0 deletions.
  1. +25 −0 puzzle/jam2012q_a.cc
  2. +23 −0 puzzle/jam2012q_b.cc
  3. +61 −0 puzzle/jam2012q_c.rb
View
25 puzzle/jam2012q_a.cc
@@ -0,0 +1,25 @@
+#include <iostream>
+#include <map>
+#include <sstream>
+using namespace std;
+int main()
+{
+ map<char, char> m;
+ string source("ejp mysljylc kd kxveddknmc re jsicpdrysirbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcdde kr kd eoya kw aej tysr re ujdr lkgc jvzq");
+ string target("our language is impossible to understandthere are twenty six factorial possibilitiesso it is okay if you want to just give upqz");
+ for (int i = 0; i < source.size(); ++i)
+ m[ source[i] ] = target[i];
+ string line;
+ int sz, now = 1;
+ cin >> sz;
+ cin.ignore();
+ while (getline(cin, line)) {
+ if (line.empty()) break;
+ stringstream ss;
+ ss << "Case #" << now++ << ": ";
+ for (int i = 0; i < line.size(); ++i) {
+ ss << m[line[i]];
+ }
+ cout << ss.str() << endl;
+ }
+}
View
23 puzzle/jam2012q_b.cc
@@ -0,0 +1,23 @@
+#include <iostream>
+#include <sstream>
+#include <cmath>
+using namespace std;
+int main()
+{
+ int T; cin >> T;
+ for (int now = 1; now <= T; ++now) {
+ stringstream ss;
+ ss << "Case #" << now << ": ";
+ int N, S, p, ans = 0, ans_dec = 0;
+ cin >> N >> S >> p;
+ for (int i = 0; i < N; ++i) {
+ int max_one, tmp;
+ cin >> tmp;
+ max_one = (tmp == 0) ? 0 : static_cast<int>(ceil(tmp / 3.0));
+ if (max_one >= p) ans++;
+ if (max_one == p - 1 && max_one != 0) ans_dec++;
+ }
+ ss << ans + min(ans_dec, S);
+ cout << ss.str() << endl;
+ }
+}
View
61 puzzle/jam2012q_c.rb
@@ -0,0 +1,61 @@
+# -*- coding: euc-jp -*-
+def count_recycled(a, end_val)
+ orig = String.new(a)
+ count = 0
+ loop do
+ digit = a.slice!(0)
+ a << digit
+ break if orig == a
+ next if a[0] == '0'
+ count += 1 if orig < a and a.to_i <= end_val
+ end
+
+ count
+end
+
+def listing_recycled(a, end_val, pre_pairs)
+ orig = String.new(a)
+ pairs = []
+ loop do
+ digit = a.slice!(0)
+ a << digit
+ break if orig == a
+ next if a[0] == '0'
+ if orig < a and a.to_i <= end_val
+ pairs << a.to_i
+ end
+ end
+
+ pre_pairs[orig.to_i] = pairs
+end
+
+HASH_OBJ = "c_hash.dump"
+
+pre_pairs = Hash.new
+if not FileTest.exists?(HASH_OBJ)
+ 10.upto(99) {|i| listing_recycled(i.to_s, 99, pre_pairs) }
+ 100.upto(999) {|i| listing_recycled(i.to_s, 999, pre_pairs) }
+ 1000.upto(9999) {|i| listing_recycled(i.to_s, 9999, pre_pairs) }
+ 10000.upto(99999) {|i| listing_recycled(i.to_s, 99999, pre_pairs) }
+ 100000.upto(999999) {|i| listing_recycled(i.to_s, 999999, pre_pairs) }
+ 1000000.upto(2000000) {|i| listing_recycled(i.to_s, 2000000, pre_pairs) }
+
+ hash_dump = Marshal.dump(pre_pairs)
+ open(HASH_OBJ, 'w') do |dst|
+ dst.write(hash_dump)
+ end
+else
+ pre_pairs = Marshal.load(open(HASH_OBJ, 'r').read)
+end
+
+1.upto(readline.to_i) do |k|
+ start_val, end_val = readline.split(' ').map{|v| v.to_i}
+ cnt = 0
+ start_val.upto(end_val) do |i|
+ next if not pre_pairs.has_key?(i)
+ pre_pairs[ i ].each do |v|
+ cnt += 1 if v <= end_val
+ end
+ end
+ puts "Case ##{k}: #{cnt}"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.