Created
September 11, 2012 09:50
-
-
Save evandrix/3697302 to your computer and use it in GitHub Desktop.
Codeforces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.{File, PrintWriter} | |
import java.util.{Locale, Scanner} | |
import scala.util.control.Breaks._ | |
object B extends App { | |
val test = "B-small-attempt1" | |
val input = new Scanner(new File(test + ".in")) | |
val output = new PrintWriter(test + ".out") | |
val T = input.nextInt() | |
for (t <- 1 to T) { | |
println("%s of %s" format(t, T)) | |
val N = input.nextInt() | |
val W = input.nextInt() | |
val L = input.nextInt() | |
val r = (for (i <- 1 to N) yield (i, input.nextInt.toDouble)).toArray | |
r.sortBy(-_._2) | |
val x = Array.ofDim[Double](N) | |
val y = Array.ofDim[Double](N) | |
var ok = true | |
breakable { | |
var attt = 0; | |
while (true) { | |
attt += 1 | |
println(" attempt " + attt) | |
breakable { | |
for (i <- 0 until N) { | |
breakable { | |
for (att <- 1 to 100) { | |
x(i) = math.random * W | |
y(i) = math.random * L | |
ok = true | |
breakable { | |
for (j <- 0 until i) { | |
val dx = x(i) - x(j) | |
val dy = y(i) - y(j) | |
val dr = r(i)._2 + r(j)._2 + 0.0001 | |
if (dx * dx + dy * dy < dr * dr) { | |
ok = false | |
break | |
} | |
} | |
} | |
if (ok) break | |
} | |
} | |
if (!ok) {println("Given up at " + i); break} | |
} | |
} | |
if (ok) break | |
} | |
} | |
val result = (for (i <- 0 until N) yield (r(i)._1, x(i), y(i))).sortBy(_._1) | |
output.print("Case #%s:" format (t)) | |
result.foreach { x => output.print(" %.5f %.5f" formatLocal(Locale.US, x._2, x._3)) } | |
output.println() | |
} | |
output.close() | |
input.close() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nefariouszhen.cj2012.rnd2 | |
object Vine { | |
def apply(s: String): Vine = { | |
val l = s.split(" ") | |
Vine(l(0).toInt, l(1).toInt) | |
} | |
} | |
case class Vine(d: Int, l: Int) | |
object SwingingWild { | |
def main(args: Array[String]) { | |
val in = io.Source.stdin.getLines() | |
val t = in.next().toInt | |
for (tc <- 1 to t) { | |
val n = in.next().toInt | |
val v = { for (_ <- 0 until n) yield Vine(in.next()) }.toArray | |
val D = in.next().toInt | |
val b = v.map(_ => -1) | |
b(0) = math.min(v(0).l, v(0).d) | |
for (i <- 0 until n) { | |
var j = i | |
while (j < n && v(j).d <= v(i).d + b(i)) { | |
b(j) = math.max(b(j), math.min(v(j).l, v(j).d - v(i).d)) | |
j += 1 | |
} | |
} | |
var good = false | |
for (i <- 0 until n) { | |
if (b(i) + v(i).d >= D) | |
good = true | |
} | |
println("Case #%d: %s".format(tc, if (good) "YES" else "NO")) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import logging | |
# Work out input and output file names | |
if len(sys.argv) > 1: | |
sampledata = False | |
infname = sys.argv[1] | |
else: | |
sampledata = True | |
scriptname = sys.argv[0] | |
problemletter = scriptname[:scriptname.index('.')] | |
infname = problemletter + '-example.in' | |
outfname = infname[:infname.index('.')] + '.out' | |
# Set up input | |
with open(infname) as f: | |
text = f.read() | |
lines = text.splitlines() | |
linesiter = iter(lines) | |
nextline = linesiter.next | |
# Set up output | |
ofile = open(outfname, 'w') | |
sys.stdout = ofile | |
# Set up logging | |
# ^^^^^^^^^^^^^^^^^^^ | |
# Template code above | |
##################### | |
T = int(nextline()) | |
for t in range(1, T+1): | |
M,F,N = map(int, nextline().split()) | |
meals = [map(int, nextline().split()) for _ in xrange(N)] | |
bmeals = [] | |
while meals: | |
meals.sort() | |
cheap = meals[0][0] | |
cheapest = [m for m in meals if m[0]==cheap] | |
cheapest.sort(reverse=True) | |
best = cheapest[0] | |
meals.remove(best) | |
bmeals.append(best) | |
day = best[1] | |
meals = [m for m in meals if m[1]>day] | |
bdays = 0 | |
bdels = -1 | |
def deldays(dels): | |
global bdays, bdels | |
m = M - dels*F | |
if m <= 0: return 0 | |
tdays = 0 | |
day = -1 | |
for meal in bmeals: | |
p,s = meal | |
dd = s-day | |
day = s | |
most = int(m/p) | |
most = min(most, dels*dd) | |
if most==0: break | |
tdays += most | |
m -= most*p | |
bdays = max(bdays, tdays) | |
if bdays==tdays: | |
bdels = dels | |
for r in xrange(len(bmeals)+1): | |
dp = F | |
day = -1 | |
for p,s in bmeals[:r]: | |
dd = s-day | |
day = s | |
dp += p*dd | |
dels = int(M/dp) | |
deldays(dels) | |
deldays(dels+1) | |
# for dels in xrange(1,2*M): | |
# deldays(dels) | |
print 'Case #%s:'%t, bdays | |
##################### | |
# Template code below | |
# vvvvvvvvvvvvvvvvvvv | |
sys.stdout = sys.__stdout__ | |
ofile.close() | |
if sampledata: | |
base = problemletter+'-example.' | |
outfile = base+'out' | |
rightfile = base+'right' | |
out = open(outfile).read() | |
right = open(rightfile).read() | |
if out==right: | |
print 'Congrats, your output matches sample output' | |
else: | |
print 'OUTPUT MISMATCH' | |
import os | |
os.system('diff %s %s'%(outfile,rightfile)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment