import sys NEW_LINE stdin = sys . stdin NEW_LINE def computeEtries ( p ) : NEW_LINE INDENT pr = p / 100.0 NEW_LINE re = 1 NEW_LINE running = pr NEW_LINE multiplier = 2 NEW_LINE while multiplier < 10000 or multiplier * running > 1e-9 : NEW_LINE INDENT re += multiplier * running NEW_LINE running *= pr NEW_LINE multiplier += 1 NEW_LINE DEDENT return ( 1 - pr ) * re NEW_LINE DEDENT ets = [ computeEtries ( p ) for p in range ( 100 ) ] NEW_LINE T = int ( stdin . readline ( ) ) NEW_LINE for tcase in range ( T ) : NEW_LINE INDENT N = int ( stdin . readline ( ) ) NEW_LINE Ls = map ( int , stdin . readline ( ) . split ( ) ) NEW_LINE Ps = map ( int , stdin . readline ( ) . split ( ) ) NEW_LINE etries = [ ets [ p ] for p in Ps ] NEW_LINE seq = [ ( index , Ls [ index ] , etries [ index ] ) for index in range ( N ) ] NEW_LINE for k in range ( N ) : NEW_LINE INDENT best = k NEW_LINE for s in range ( k + 1 , N ) : NEW_LINE INDENT in1 = seq [ best ] [ 0 ] NEW_LINE L1 = seq [ best ] [ 1 ] NEW_LINE E1 = seq [ best ] [ 2 ] NEW_LINE in2 = seq [ s ] [ 0 ] NEW_LINE L2 = seq [ s ] [ 1 ] NEW_LINE E2 = seq [ s ] [ 2 ] NEW_LINE extra_if_1_goes_first = ( E2 - 1 ) * E1 * L1 NEW_LINE extra_if_2_goes = ( E1 - 1 ) * E2 * L2 NEW_LINE if abs ( extra_if_1_goes_first - extra_if_2_goes ) < 1e-9 : NEW_LINE INDENT if in2 < in1 : NEW_LINE INDENT best = s NEW_LINE DEDENT DEDENT elif extra_if_2_goes < extra_if_1_goes_first : NEW_LINE INDENT best = s NEW_LINE DEDENT DEDENT swap = seq [ best ] NEW_LINE seq [ best ] = seq [ k ] NEW_LINE seq [ k ] = swap NEW_LINE DEDENT print " Case ▁ # % d : " % ( tcase + 1 ) , NEW_LINE print ' ▁ ' . join ( map ( str , [ seq [ i ] [ 0 ] for i in range ( N ) ] ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . Arrays ; import java . util . Locale ; import java . util . StringTokenizer ; public class A { BufferedReader in ; StringTokenizer str ; PrintWriter out ; String SK ; String next ( ) throws IOException { while ( ( str == null ) || ( ! str . hasMoreTokens ( ) ) ) { SK = in . readLine ( ) ; if ( SK == null ) return null ; str = new StringTokenizer ( SK ) ; } return str . nextToken ( ) ; } int nextInt ( ) throws IOException { return Integer . parseInt ( next ( ) ) ; } double nextDouble ( ) throws IOException { return Double . parseDouble ( next ( ) ) ; } long nextLong ( ) throws IOException { return Long . parseLong ( next ( ) ) ; } class Sob implements Comparable < Sob > { int t ; int p ; int num ; public int compareTo ( Sob o ) { return o . p * t - p * o . t ; } } void solve ( ) throws IOException { int n = nextInt ( ) ; Sob [ ] a = new Sob [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = new Sob ( ) ; a [ i ] . num = i ; } for ( int i = 0 ; i < n ; i ++ ) { a [ i ] . t = nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { a [ i ] . p = nextInt ( ) ; } Arrays . sort ( a ) ; for ( int i = 0 ; i < n ; i ++ ) { out . print ( " ▁ " + a [ i ] . num ) ; } out . println ( ) ; } void run ( ) throws IOException { in = new BufferedReader ( new FileReader ( " input . txt " ) ) ; out = new PrintWriter ( " output . txt " ) ; int t = nextInt ( ) ; for ( int i = 0 ; i < t ; i ++ ) { out . print ( " Case ▁ # " + ( i + 1 ) + " : " ) ; solve ( ) ; } out . close ( ) ; } public static void main ( String [ ] args ) throws IOException { new A ( ) . run ( ) ; } }

import sys NEW_LINE T = int ( sys . stdin . readline ( ) ) NEW_LINE for t in range ( 1 , T + 1 ) : NEW_LINE INDENT n = int ( sys . stdin . readline ( ) ) NEW_LINE L = map ( int , sys . stdin . readline ( ) . strip ( ) . split ( ) ) NEW_LINE P = map ( int , sys . stdin . readline ( ) . strip ( ) . split ( ) ) NEW_LINE l = [ ] NEW_LINE for i , p in enumerate ( P ) : NEW_LINE INDENT if p != 0 : NEW_LINE INDENT l . append ( ( float ( L [ i ] ) / p , i ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT l . append ( ( 1E30 , i ) ) ; NEW_LINE DEDENT DEDENT l . sort ( ) NEW_LINE print ' Case ▁ # % d : ▁ ' % t , NEW_LINE for p , i in l : NEW_LINE INDENT print i , NEW_LINE DEDENT print ' ' NEW_LINE DEDENT


import sys NEW_LINE def cmp_fn ( x1 , x2 ) : NEW_LINE INDENT L1 , q1 = x1 [ 1 ] , x1 [ 2 ] NEW_LINE L2 , q2 = x2 [ 1 ] , x2 [ 2 ] NEW_LINE if q1 == 100 and q2 == 100 : NEW_LINE INDENT return x1 [ 0 ] - x1 [ 1 ] NEW_LINE DEDENT if q1 == 100 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if q2 == 100 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if L1 * q2 != L2 * q1 : NEW_LINE INDENT return L2 * q1 - L1 * q2 NEW_LINE DEDENT return x1 [ 0 ] - x1 [ 1 ] NEW_LINE DEDENT def compute ( L , P ) : NEW_LINE INDENT x = [ ( i , L [ i ] , 100 - P [ i ] ) for i in xrange ( len ( L ) ) ] NEW_LINE x = sorted ( x , cmp = cmp_fn ) NEW_LINE return " ▁ " . join ( [ ( ' % d ' % x [ i ] [ 0 ] ) for i in xrange ( len ( x ) ) ] ) NEW_LINE DEDENT def parse ( ) : NEW_LINE INDENT N = int ( sys . stdin . readline ( ) . strip ( ) ) NEW_LINE L = map ( int , sys . stdin . readline ( ) . strip ( ) . split ( ) ) NEW_LINE P = map ( int , sys . stdin . readline ( ) . strip ( ) . split ( ) ) NEW_LINE return L , P NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sys . setrecursionlimit ( 100000 ) NEW_LINE T = int ( sys . stdin . readline ( ) . strip ( ) ) NEW_LINE count = 1 NEW_LINE part = 0 NEW_LINE if len ( sys . argv ) == 3 : NEW_LINE INDENT part = int ( sys . argv [ 1 ] ) NEW_LINE count = int ( sys . argv [ 2 ] ) NEW_LINE DEDENT for i in xrange ( T ) : NEW_LINE INDENT data = parse ( ) NEW_LINE if i * count >= part * T and i * count < ( part + 1 ) * T : NEW_LINE INDENT result = compute ( * data ) NEW_LINE print " Case ▁ # % d : ▁ % s " % ( i + 1 , result ) NEW_LINE DEDENT DEDENT DEDENT


from __future__ import division NEW_LINE import os NEW_LINE import sys NEW_LINE import operator NEW_LINE import string NEW_LINE import re NEW_LINE import time NEW_LINE from os . path import splitext NEW_LINE from copy import copy NEW_LINE from math import * NEW_LINE from operator import * NEW_LINE from collections import * NEW_LINE from itertools import * NEW_LINE from functools import * NEW_LINE try : NEW_LINE INDENT from do import report_working_on NEW_LINE DEDENT except ImportError : NEW_LINE INDENT report_working_on = lambda a , b : None NEW_LINE DEDENT if len ( sys . argv ) > 1 : NEW_LINE INDENT fin = file ( sys . argv [ 1 ] , ' r ' ) NEW_LINE fout = file ( splitext ( sys . argv [ 1 ] ) [ 0 ] + ' . out ' , ' w + ' ) NEW_LINE DEDENT else : NEW_LINE INDENT fin = sys . stdin NEW_LINE fout = sys . stdout NEW_LINE DEDENT def dorun ( ) : NEW_LINE INDENT cases = int ( fin . next ( ) ) NEW_LINE for case in xrange ( cases ) : NEW_LINE INDENT report_working_on ( case , cases ) NEW_LINE print >> fout , ' Case ▁ # % d : ▁ % s ' % ( 1 + case , solve ( fin ) ) NEW_LINE DEDENT else : NEW_LINE INDENT report_working_on ( 0 , 0 ) NEW_LINE DEDENT DEDENT def solve ( fin ) : NEW_LINE INDENT N = int ( fin . next ( ) . strip ( ) ) NEW_LINE Lx = map ( int , fin . next ( ) . split ( ) ) NEW_LINE Px = map ( int , fin . next ( ) . split ( ) ) NEW_LINE LL = list ( enumerate ( Px ) ) NEW_LINE LL . sort ( key = lambda a : - a [ 1 ] ) NEW_LINE return ' ▁ ' . join ( str ( a [ 0 ] ) for a in LL ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : dorun ( ) NEW_LINE


fin = open ( " A - small - attempt0 . in " , " r " ) NEW_LINE fout = open ( " A . out " , " w " ) NEW_LINE numcases = int ( fin . readline ( ) ) NEW_LINE numcases += 1 NEW_LINE for cas in xrange ( 1 , numcases ) : NEW_LINE INDENT fout . write ( " Case ▁ # " + str ( cas ) + " : " ) NEW_LINE n = int ( fin . readline ( ) ) NEW_LINE l = map ( float , fin . readline ( ) . split ( ) ) NEW_LINE p = map ( float , fin . readline ( ) . split ( ) ) NEW_LINE q = [ p [ i ] / l [ i ] for i in xrange ( n ) ] NEW_LINE r = [ ( q [ i ] , - i ) for i in xrange ( n ) ] NEW_LINE r . sort ( ) NEW_LINE r . reverse ( ) NEW_LINE for i in r : NEW_LINE INDENT fout . write ( " ▁ " + str ( - i [ 1 ] ) ) NEW_LINE DEDENT fout . write ( " \n " ) NEW_LINE DEDENT


import java . util . * ; import java . io . * ; import java . math . * ; import java . awt . * ; public class A { public static Comparator < Point > cmp = new Comparator < Point > ( ) { public int compare ( Point a , Point b ) { if ( a . y > b . y ) return - 1 ; if ( a . y < b . y ) return 1 ; return a . x - b . x ; } } ; public static void main ( String args [ ] ) { Scanner scan = new Scanner ( System . in ) ; int T = scan . nextInt ( ) ; for ( int ca = 1 ; ca <= T ; ca ++ ) { int n = scan . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) scan . nextInt ( ) ; Point [ ] a = new Point [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = new Point ( i , scan . nextInt ( ) ) ; Arrays . sort ( a , cmp ) ; System . out . print ( " Case ▁ # " + ca + " : " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( " ▁ " + a [ i ] . x ) ; System . out . println ( ) ; } } }


import java . io . FileReader ; import java . io . FileWriter ; import java . io . IOException ; import java . io . PrintWriter ; import java . util . ArrayList ; import java . util . List ; import java . util . Scanner ; public class Main implements Runnable { public static void main ( String [ ] args ) throws IOException { new Thread ( new Main ( ) ) . start ( ) ; } public void run ( ) { try { run1 ( ) ; } catch ( IOException e ) { throw new RuntimeException ( ) ; } } public void run1 ( ) throws IOException { Scanner sc = new Scanner ( new FileReader ( " input . txt " ) ) ; PrintWriter pw = new PrintWriter ( new FileWriter ( " output . txt " ) ) ; int tN = sc . nextInt ( ) ; sc . nextLine ( ) ; for ( int tn = 0 ; tn < tN ; tn ++ ) { int n = sc . nextInt ( ) ; int [ ] p = new int [ n ] ; int [ ] len = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { p [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { len [ i ] = sc . nextInt ( ) ; } boolean [ ] oppa = new boolean [ n ] ; pw . print ( " Case ▁ # " + ( tn + 1 ) + " : " ) ; c : for ( int i = 0 ; i < n ; i ++ ) { int be = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ! oppa [ j ] ) { if ( be == - 1 || len [ j ] * p [ be ] > len [ be ] * p [ j ] ) { be = j ; } } } oppa [ be ] = true ; pw . print ( " ▁ " + be ) ; } pw . println ( ) ; } pw . close ( ) ; } }


import java . util . Arrays ; import java . util . Scanner ; public class A { static Scanner sc = new Scanner ( System . in ) ; static class Solver { int N ; int [ ] L , P ; void solve ( ) { N = sc . nextInt ( ) ; L = new int [ N ] ; P = new int [ N ] ; for ( int i = 0 ; i < N ; ++ i ) { L [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < N ; ++ i ) { P [ i ] = sc . nextInt ( ) ; } Stage [ ] stages = new Stage [ N ] ; for ( int i = 0 ; i < N ; ++ i ) { stages [ i ] = new Stage ( L [ i ] , P [ i ] , i ) ; } Arrays . sort ( stages ) ; for ( int i = 0 ; i < N ; ++ i ) { System . out . print ( stages [ i ] . idx ) ; if ( i != N - 1 ) { System . out . print ( " ▁ " ) ; } } System . out . println ( ) ; } } static class Stage implements Comparable < Stage > { int l , p , idx ; Stage ( int l , int p , int idx ) { this . l = l ; this . p = p ; this . idx = idx ; } public int compareTo ( Stage o ) { return o . l * o . p - this . l * this . p ; } } public static void main ( String [ ] args ) { int T = sc . nextInt ( ) ; for ( int i = 1 ; i <= T ; ++ i ) { System . out . print ( " Case ▁ # " + i + " : ▁ " ) ; Solver solver = new Solver ( ) ; solver . solve ( ) ; } } }


import java . io . * ; import java . util . * ; public class A { public static void main ( String [ ] args ) throws IOException { BufferedReader br = new BufferedReader ( new FileReader ( " in . txt " ) ) ; PrintWriter pw = new PrintWriter ( new FileWriter ( " out . txt " ) ) ; int N = Integer . parseInt ( br . readLine ( ) ) ; for ( int pp = 0 ; pp < N ; pp ++ ) { String [ ] ss = br . readLine ( ) . split ( " ▁ " ) ; int d = Integer . parseInt ( ss [ 0 ] ) ; int n = Integer . parseInt ( ss [ 1 ] ) ; double max = 0.0 ; for ( int i = 0 ; i < n ; i ++ ) { ss = br . readLine ( ) . split ( " ▁ " ) ; int k = Integer . parseInt ( ss [ 0 ] ) ; int s = Integer . parseInt ( ss [ 1 ] ) ; double time = ( d - k ) / ( ( double ) s ) ; System . out . println ( d + " ▁ " + k + " ▁ " + s + " ▁ " + time ) ; max = Math . max ( max , time ) ; } double ret = d / max ; pw . println ( " Case ▁ # " + ( pp + 1 ) + " : ▁ " + ret ) ; } pw . flush ( ) ; pw . close ( ) ; } }

def solve ( ) : NEW_LINE INDENT d , n = map ( int , input ( ) . split ( ) ) NEW_LINE ans = 10 ** 100 NEW_LINE for i in range ( n ) : NEW_LINE INDENT k , s = map ( int , input ( ) . split ( ) ) NEW_LINE ans = min ( ans , d * s / ( d - k ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT t = int ( input ( ) ) NEW_LINE for i in range ( t ) : NEW_LINE INDENT print ( " Case ▁ # % d : ▁ % .20f " % ( i + 1 , solve ( ) ) ) NEW_LINE DEDENT


def initialize_solver ( ) : NEW_LINE INDENT pass NEW_LINE DEDENT def solve_testcase ( ) : NEW_LINE INDENT d , n = read ( ) NEW_LINE mx = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT k , s = read ( ) NEW_LINE mx = max ( ( d - k ) / s , mx ) NEW_LINE DEDENT return str ( d / mx ) NEW_LINE DEDENT def read ( callback = int , split = True ) : NEW_LINE INDENT if sfile : NEW_LINE INDENT input_line = sfile . readline ( ) . strip ( ) NEW_LINE DEDENT else : NEW_LINE INDENT input_line = input ( ) . strip ( ) NEW_LINE DEDENT if split : NEW_LINE INDENT return list ( map ( callback , input_line . split ( ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return callback ( input_line ) NEW_LINE DEDENT DEDENT def write ( value = " \n " ) : NEW_LINE INDENT if value is None : return NEW_LINE try : NEW_LINE INDENT if not isinstance ( value , str ) : NEW_LINE INDENT value = " ▁ " . join ( map ( str , value ) ) NEW_LINE DEDENT DEDENT except : NEW_LINE INDENT pass NEW_LINE DEDENT if tfile : NEW_LINE INDENT tfile . write ( value ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( value , end = " " ) NEW_LINE DEDENT DEDENT output_format = " Case ▁ # % d : ▁ " NEW_LINE filename = input ( ) . strip ( ) NEW_LINE sfile = None NEW_LINE tfile = None NEW_LINE if filename != " " : NEW_LINE INDENT sfile = open ( filename + " . in " , " r " ) NEW_LINE sfile . seek ( 0 ) NEW_LINE tfile = open ( filename + " . out " , " w " ) NEW_LINE DEDENT if output_format == None : NEW_LINE INDENT solve_testcase ( ) NEW_LINE DEDENT else : NEW_LINE INDENT initialize_solver ( ) NEW_LINE total_cases , = read ( ) NEW_LINE for case_number in range ( 1 , total_cases + 1 ) : NEW_LINE INDENT write ( output_format . replace ( " % d " , str ( case_number ) ) ) NEW_LINE write ( solve_testcase ( ) ) NEW_LINE write ( ) NEW_LINE DEDENT DEDENT if tfile is not None : tfile . close ( ) NEW_LINE


TEST = ' large ' NEW_LINE IN = ' A - { } . in ' . format ( TEST ) NEW_LINE OUT = ' A - { } . out ' . format ( TEST ) NEW_LINE def run ( d , h ) : NEW_LINE INDENT t = max ( ( d - k ) / s for k , s in h ) NEW_LINE return d / t NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT with open ( IN ) as fin , open ( OUT , ' w ' ) as fout : NEW_LINE INDENT t = int ( fin . readline ( ) . strip ( ) ) NEW_LINE for i in range ( t ) : NEW_LINE INDENT d , n = map ( int , fin . readline ( ) . split ( ) ) NEW_LINE h = [ tuple ( map ( int , fin . readline ( ) . split ( ) ) ) for i in range ( n ) ] NEW_LINE res = run ( d , h ) NEW_LINE print ( ' Case ▁ # { } : ▁ { } ' . format ( i + 1 , res ) , file = fout ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


from sys import stdin NEW_LINE for cn in xrange ( 1 , 1 + int ( stdin . readline ( ) ) ) : NEW_LINE INDENT ( D , N ) = tuple ( int ( z ) for z in stdin . readline ( ) . split ( ) ) NEW_LINE slowest = 0 NEW_LINE for h in xrange ( N ) : NEW_LINE INDENT ( K , S ) = tuple ( int ( z ) for z in stdin . readline ( ) . split ( ) ) NEW_LINE time = float ( D - K ) / S NEW_LINE slowest = max ( slowest , time ) NEW_LINE DEDENT speed = D / slowest NEW_LINE print " Case ▁ # { } : ▁ { } " . format ( cn , speed ) NEW_LINE DEDENT


with open ( ' A - large . in ' , ' r ' ) as f , open ( ' out . txt ' , ' w ' ) as fout : NEW_LINE INDENT t = int ( f . readline ( ) . strip ( ) ) NEW_LINE for case in range ( 1 , t + 1 ) : NEW_LINE INDENT d , n = [ int ( s ) for s in f . readline ( ) . strip ( ) . split ( ) ] NEW_LINE last_arrive = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ki , si = [ int ( s ) for s in f . readline ( ) . strip ( ) . split ( ) ] NEW_LINE last_arrive = max ( last_arrive , ( ( d - ki ) / si ) ) NEW_LINE DEDENT print ( ' Case ▁ # % d : ▁ % .6f ' % ( case , d / last_arrive ) , file = fout ) NEW_LINE DEDENT DEDENT


package year2017 . round1b ; import java . io . File ; import java . io . PrintWriter ; import java . util . Scanner ; public class CruiseControl { public static void main ( String [ ] args ) throws Exception { File inputFile = new File ( " A - large . in " ) ; Scanner in = new Scanner ( inputFile ) ; File outputFile = new File ( " output . txt " ) ; PrintWriter out = new PrintWriter ( outputFile ) ; int T = in . nextInt ( ) ; for ( int t = 0 ; t < T ; t ++ ) { double D = in . nextDouble ( ) ; int N = in . nextInt ( ) ; double maxTime = 0 ; for ( int n = 0 ; n < N ; n ++ ) { double K = in . nextDouble ( ) ; double S = in . nextDouble ( ) ; double time = ( D - K ) / S ; maxTime = Math . max ( time , maxTime ) ; } double speed = D / maxTime ; out . println ( " Case ▁ # " + ( t + 1 ) + " : ▁ " + speed ) ; } out . close ( ) ; } }


import static java . lang . Double . parseDouble ; import static java . lang . Integer . parseInt ; import static java . lang . Long . parseLong ; import static java . lang . Math . max ; import static java . lang . System . exit ; import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStreamReader ; import java . io . OutputStreamWriter ; import java . io . PrintWriter ; import java . util . Locale ; import java . util . StringTokenizer ; public class A { static BufferedReader in ; static PrintWriter out ; static StringTokenizer tok ; static int test ; static void solve ( ) throws Exception { int d = nextInt ( ) ; int n = nextInt ( ) ; double ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int k = nextInt ( ) ; int s = nextInt ( ) ; ans = max ( ans , ( double ) ( d - k ) / s ) ; } printCase ( ) ; out . printf ( Locale . US , " % .9f \n " , d / ans ) ; } static void printCase ( ) { out . print ( " Case ▁ # " + test + " : ▁ " ) ; } static void printlnCase ( ) { out . println ( " Case ▁ # " + test + " : " ) ; } static int nextInt ( ) throws IOException { return parseInt ( next ( ) ) ; } static long nextLong ( ) throws IOException { return parseLong ( next ( ) ) ; } static double nextDouble ( ) throws IOException { return parseDouble ( next ( ) ) ; } static String next ( ) throws IOException { while ( tok == null || ! tok . hasMoreTokens ( ) ) { tok = new StringTokenizer ( in . readLine ( ) ) ; } return tok . nextToken ( ) ; } public static void main ( String [ ] args ) { try { in = new BufferedReader ( new InputStreamReader ( System . in ) ) ; out = new PrintWriter ( new OutputStreamWriter ( System . out ) ) ; int tests = nextInt ( ) ; for ( test = 1 ; test <= tests ; test ++ ) { solve ( ) ; } in . close ( ) ; out . close ( ) ; } catch ( Throwable e ) { e . printStackTrace ( ) ; exit ( 1 ) ; } } }


package codejam ; import java . io . BufferedReader ; import java . io . FileReader ; import java . io . IOException ; import java . nio . file . Files ; import java . nio . file . Paths ; public class Horse { public static void main ( String [ ] args ) throws IOException { BufferedReader br = new BufferedReader ( new FileReader ( " src / codejam / A - large . in " ) ) ; StringBuilder sb = new StringBuilder ( ) ; int T = Integer . parseInt ( br . readLine ( ) ) ; for ( int t_i = 1 ; t_i <= T ; t_i ++ ) { String [ ] split = br . readLine ( ) . split ( " ▁ " ) ; int D = Integer . parseInt ( split [ 0 ] ) ; int N = Integer . parseInt ( split [ 1 ] ) ; int [ ] k = new int [ N ] ; int [ ] s = new int [ N ] ; double latest = 0 ; for ( int n_i = 0 ; n_i < N ; n_i ++ ) { split = br . readLine ( ) . split ( " ▁ " ) ; k [ n_i ] = Integer . parseInt ( split [ 0 ] ) ; s [ n_i ] = Integer . parseInt ( split [ 1 ] ) ; double time = 1. * ( D - k [ n_i ] ) / s [ n_i ] ; if ( time > latest ) latest = time ; } sb . append ( " Case ▁ # " ) . append ( t_i ) . append ( " : ▁ " ) . append ( D / latest ) . append ( " \n " ) ; } Files . write ( Paths . get ( " src / codejam / horse . out " ) , sb . toString ( ) . getBytes ( ) ) ; System . out . println ( sb . toString ( ) ) ; } }


package Y2017 . r1b ; import java . io . FileNotFoundException ; import java . io . PrintWriter ; import java . io . UnsupportedEncodingException ; import java . util . Scanner ; public class CruiseControl { public static void main ( String [ ] args ) throws FileNotFoundException , UnsupportedEncodingException { Scanner scanner = new Scanner ( System . in ) ; int n = scanner . nextInt ( ) ; scanner . nextLine ( ) ; try ( PrintWriter writer = new PrintWriter ( " solution . txt " , " UTF - 8" ) ) { for ( int i = 1 ; i <= n ; ++ i ) { writer . print ( String . format ( " Case ▁ # % d : ▁ " , i ) ) ; solve ( scanner , writer ) ; } } } public static void solve ( Scanner scanner , PrintWriter writer ) { int D = scanner . nextInt ( ) ; int N = scanner . nextInt ( ) ; double duration = 0 ; for ( int i = 0 ; i < N ; ++ i ) { int K = scanner . nextInt ( ) ; int S = scanner . nextInt ( ) ; double d = ( D - K ) / ( ( double ) S ) ; duration = Math . max ( duration , d ) ; } writer . println ( D / duration ) ; } }


import java . io . * ; import java . util . * ; public class Round0B { int cases ; int count ( String stack ) { int flips = 0 ; for ( int n = 1 ; n < stack . length ( ) ; n ++ ) { if ( stack . charAt ( n - 1 ) != stack . charAt ( n ) ) flips ++ ; } return flips ; } void process ( Scanner scanner , PrintStream out ) throws IOException { cases = scanner . nextInt ( ) ; scanner . nextLine ( ) ; for ( int curCase = 0 ; curCase < cases ; curCase ++ ) { String stack = scanner . nextLine ( ) . trim ( ) ; out . println ( " Case ▁ # " + ( curCase + 1 ) + " : ▁ " + count ( stack + " + " ) ) ; } } Round0B ( ) throws IOException { Scanner in = new Scanner ( new File ( " C : \\ Users \\ Olaf \\ Downloads \\ B - large . in " ) ) ; PrintStream out = new PrintStream ( " out - B - large . txt " ) ; process ( in , out ) ; in . close ( ) ; out . close ( ) ; } public static void main ( String [ ] args ) throws IOException { new Round0B ( ) ; } }

import sys NEW_LINE with open ( sys . argv [ 1 ] ) as f : NEW_LINE INDENT lines = f . readlines ( ) NEW_LINE DEDENT T = int ( lines [ 0 ] , 10 ) NEW_LINE for tt , l in enumerate ( lines [ 1 : ] ) : NEW_LINE INDENT pancakes = l . strip ( ) NEW_LINE done = False NEW_LINE flips = 0 NEW_LINE while 1 : NEW_LINE INDENT index = pancakes . rfind ( " - " ) NEW_LINE if index == - 1 : NEW_LINE INDENT break NEW_LINE DEDENT flips += 1 NEW_LINE new_cakes = " " NEW_LINE for i in xrange ( index + 1 ) : NEW_LINE INDENT if pancakes [ i ] == " + " : NEW_LINE INDENT new_cakes += " - " NEW_LINE DEDENT elif pancakes [ i ] == " - " : NEW_LINE INDENT new_cakes += " + " NEW_LINE DEDENT else : NEW_LINE INDENT raise Exception ( " wtf " ) NEW_LINE DEDENT DEDENT pancakes = new_cakes + pancakes [ index + 1 : ] NEW_LINE DEDENT print ( " Case ▁ # % d : " % ( tt + 1 ) ) , flips NEW_LINE DEDENT


f = open ( ' C : \\Users\\djspence\\Downloads\\B - large . in ' , ' r ' ) NEW_LINE tries = int ( f . readline ( ) ) NEW_LINE for i in range ( 0 , tries ) : NEW_LINE INDENT pans = f . readline ( ) . strip ( ) NEW_LINE flips = 0 NEW_LINE for j in range ( 1 , len ( pans ) ) : NEW_LINE INDENT if pans [ j ] != pans [ j - 1 ] : NEW_LINE INDENT flips += 1 NEW_LINE DEDENT DEDENT if pans [ len ( pans ) - 1 ] == " - " : NEW_LINE INDENT flips += 1 NEW_LINE DEDENT print ( " Case ▁ # " + str ( i + 1 ) + " : ▁ " + str ( flips ) ) NEW_LINE DEDENT


with open ( ' / home / gauravjs / Documents / Google ▁ Code ▁ Jam / 2016Q / B - large . in ' , ' r ' ) as f : NEW_LINE INDENT cases = int ( f . readline ( ) ) NEW_LINE lines = f . readlines ( ) NEW_LINE DEDENT for i in range ( cases ) : NEW_LINE INDENT n = 0 NEW_LINE if lines [ i ] [ - 2 ] == ' - ' : NEW_LINE INDENT n += 1 NEW_LINE DEDENT s = lines [ i ] [ - 2 ] NEW_LINE for j in range ( len ( lines [ i ] ) - 3 , - 1 , - 1 ) : NEW_LINE INDENT if lines [ i ] [ j ] != s : NEW_LINE INDENT s = lines [ i ] [ j ] NEW_LINE n += 1 NEW_LINE DEDENT DEDENT print " Case ▁ # " + str ( i + 1 ) + " : ▁ " + str ( n ) NEW_LINE DEDENT


import math NEW_LINE import itertools NEW_LINE import numpy as np NEW_LINE import devtools NEW_LINE def read_case ( f ) : NEW_LINE INDENT return read_letters ( f ) NEW_LINE DEDENT def solve_small ( case ) : NEW_LINE INDENT S = case NEW_LINE S . append ( ' + ' ) NEW_LINE return sum ( S [ i ] != S [ i + 1 ] for i in range ( len ( S ) - 1 ) ) NEW_LINE DEDENT def solve_large ( case ) : NEW_LINE INDENT return solve_small ( case ) NEW_LINE DEDENT def write_case ( f , i , res ) : NEW_LINE INDENT f . write ( ' Case ▁ # % d : ▁ ' % i ) NEW_LINE f . write ( ' % s ' % res ) NEW_LINE f . write ( ' \n ' ) NEW_LINE DEDENT def read_word ( f ) : NEW_LINE INDENT return next ( f ) . strip ( ) NEW_LINE DEDENT def read_int ( f , b = 10 ) : NEW_LINE INDENT return int ( read_word ( f ) , b ) NEW_LINE DEDENT def read_letters ( f ) : NEW_LINE INDENT return list ( read_word ( f ) ) NEW_LINE DEDENT def read_digits ( f , b = 10 ) : NEW_LINE INDENT return [ int ( x , b ) for x in read_letters ( f ) ] NEW_LINE DEDENT def read_words ( f , d = ' ▁ ' ) : NEW_LINE INDENT return read_word ( f ) . split ( d ) NEW_LINE DEDENT def read_ints ( f , b = 10 , d = ' ▁ ' ) : NEW_LINE INDENT return [ int ( x , b ) for x in read_words ( f , d ) ] NEW_LINE DEDENT def read_floats ( f , d = ' ▁ ' ) : NEW_LINE INDENT return [ float ( x ) for x in read_words ( f , d ) ] NEW_LINE DEDENT def read_arr ( f , R , reader = read_ints , * args , ** kwargs ) : NEW_LINE INDENT return [ reader ( f , * args , ** kwargs ) for i in range ( R ) ] NEW_LINE DEDENT def solve ( solver , fn , out_fn = None ) : NEW_LINE INDENT in_fn = fn + ' . in ' NEW_LINE if out_fn is None : NEW_LINE INDENT out_fn = fn + ' . out ' NEW_LINE DEDENT with open ( in_fn , ' r ' ) as fi : NEW_LINE INDENT with open ( out_fn , ' w ' ) as fo : NEW_LINE INDENT T = read_int ( fi ) NEW_LINE for i in range ( T ) : NEW_LINE INDENT case = read_case ( fi ) NEW_LINE res = solver ( case ) NEW_LINE write_case ( fo , i , res ) NEW_LINE DEDENT DEDENT DEDENT DEDENT from run import * NEW_LINE


import collections NEW_LINE import math NEW_LINE import re NEW_LINE import sys NEW_LINE INPUT = " tiny " NEW_LINE INPUT = " B - large . in " NEW_LINE def debug ( * args ) : NEW_LINE INDENT return NEW_LINE sys . stderr . write ( str ( args ) + " \n " ) NEW_LINE DEDENT def do_trial ( s ) : NEW_LINE INDENT count = 0 NEW_LINE last = " + " NEW_LINE for c in reversed ( s ) : NEW_LINE INDENT if c != last : NEW_LINE INDENT count += 1 NEW_LINE DEDENT last = c NEW_LINE DEDENT return count NEW_LINE DEDENT f = file ( INPUT ) NEW_LINE T = int ( f . readline ( ) [ : - 1 ] ) NEW_LINE for i in range ( T ) : NEW_LINE INDENT s = f . readline ( ) [ : - 1 ] NEW_LINE v = do_trial ( s ) NEW_LINE print " Case ▁ # % d : ▁ % s " % ( i + 1 , v ) NEW_LINE DEDENT


package lab6zp ; import java . util . ArrayList ; import java . util . Collections ; import java . util . Scanner ; public class Lab6ZP { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; Scanner sin = new Scanner ( System . in ) ; int n = Integer . parseInt ( sin . next ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { String s = sin . next ( ) ; s += ' + ' ; int counter = 0 ; for ( int j = 0 ; j < s . length ( ) - 1 ; j ++ ) { if ( s . charAt ( j ) != s . charAt ( j + 1 ) ) { counter ++ ; } } System . out . println ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + counter ) ; } } }


package gcj2016 . qualif ; import java . io . FileNotFoundException ; import java . io . FileReader ; import java . io . PrintWriter ; import java . util . Scanner ; import java . util . logging . Level ; import java . util . logging . Logger ; public class ExoB { public static void main ( final String [ ] args ) { final String base = " / home / jfortin / workspace - gcj / Codejam2016 / q / ExoB / " ; final String input = base + " b1 . in " ; final String output = base + " b1 . out " ; try { final Scanner sc = new Scanner ( new FileReader ( input ) ) ; final PrintWriter pw = new PrintWriter ( output ) ; final int n = sc . nextInt ( ) ; sc . nextLine ( ) ; for ( int c = 0 ; c < n ; c ++ ) { System . out . println ( " Test ▁ case ▁ " + ( c + 1 ) + " . . . " ) ; pw . print ( " Case ▁ # " + ( c + 1 ) + " : ▁ " ) ; test ( sc , pw ) ; pw . println ( ) ; } pw . println ( ) ; pw . flush ( ) ; pw . close ( ) ; sc . close ( ) ; } catch ( final FileNotFoundException ex ) { Logger . getLogger ( ExoB . class . getName ( ) ) . log ( Level . SEVERE , null , ex ) ; } } private static void test ( final Scanner sc , final PrintWriter pw ) { String in = sc . next ( ) ; System . out . println ( in ) ; int i = count ( in , in . length ( ) , ' + ' ) ; pw . print ( i ) ; } private static int count ( final String in , final int length , final char c ) { if ( length == 0 ) { return 0 ; } if ( in . charAt ( length - 1 ) == c ) { return count ( in , length - 1 , c ) ; } else { return 1 + count ( in , length - 1 , c == ' + ' ? ' - ' : ' + ' ) ; } } }


import java . awt . * ; import java . awt . event . * ; import java . awt . geom . * ; import java . io . * ; import java . math . * ; import java . text . * ; import java . util . * ; import java . util . concurrent . * ; public class B { static BufferedReader br ; static StringTokenizer st ; static PrintWriter pw ; public static void main ( String [ ] args ) throws Exception { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; pw = new PrintWriter ( new BufferedWriter ( new FileWriter ( " b . out " ) ) ) ; final int MAX_CASES = readInt ( ) ; for ( int casenum = 1 ; casenum <= MAX_CASES ; casenum ++ ) { pw . printf ( " Case ▁ # % d : ▁ " , casenum ) ; String s = nextToken ( ) ; boolean [ ] good = new boolean [ s . length ( ) ] ; int ret = 0 ; boolean last = true ; for ( int i = 0 ; i < good . length ; i ++ ) { good [ i ] = s . charAt ( i ) == ' + ' ; if ( i > 0 && last != good [ i ] ) { ret ++ ; } last = good [ i ] ; } if ( ! last ) ret ++ ; pw . println ( ret ) ; } pw . close ( ) ; } public static int readInt ( ) { return Integer . parseInt ( nextToken ( ) ) ; } public static long readLong ( ) { return Long . parseLong ( nextToken ( ) ) ; } public static double readDouble ( ) { return Double . parseDouble ( nextToken ( ) ) ; } public static String nextToken ( ) { while ( st == null || ! st . hasMoreTokens ( ) ) { try { if ( ! br . ready ( ) ) { pw . close ( ) ; System . exit ( 0 ) ; } st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( IOException e ) { System . err . println ( e ) ; System . exit ( 1 ) ; } } return st . nextToken ( ) ; } public static String readLine ( ) { st = null ; try { return br . readLine ( ) ; } catch ( IOException e ) { System . err . println ( e ) ; System . exit ( 1 ) ; return null ; } } }


import java . io . * ; import java . util . * ; public class revengepancake { private static InputReader in ; private static PrintWriter out ; public static boolean SUBMIT = true ; public static final String NAME = " B - large " ; private static void main2 ( ) throws IOException { char [ ] s = in . next ( ) . toCharArray ( ) ; int n = s . length ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] != s [ i - 1 ] ) count ++ ; } if ( s [ n - 1 ] == ' - ' ) count ++ ; out . println ( count ) ; } public static void main ( String [ ] args ) throws IOException { if ( SUBMIT ) { in = new InputReader ( new FileInputStream ( new File ( NAME + " . in " ) ) ) ; out = new PrintWriter ( new BufferedWriter ( new FileWriter ( NAME + " . out " ) ) ) ; } else { in = new InputReader ( System . in ) ; out = new PrintWriter ( System . out , true ) ; } int numCases = in . nextInt ( ) ; for ( int test = 1 ; test <= numCases ; test ++ ) { out . print ( " Case ▁ # " + test + " : ▁ " ) ; main2 ( ) ; } out . close ( ) ; System . exit ( 0 ) ; } static class InputReader { public BufferedReader reader ; public StringTokenizer tokenizer ; public InputReader ( InputStream stream ) { reader = new BufferedReader ( new InputStreamReader ( stream ) , 32768 ) ; tokenizer = null ; } public String next ( ) { while ( tokenizer == null || ! tokenizer . hasMoreTokens ( ) ) { try { tokenizer = new StringTokenizer ( reader . readLine ( ) ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } } return tokenizer . nextToken ( ) ; } public int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } } }


import java . util . * ; class Lawnmower { public static void main ( String ... args ) { Scanner sc = new Scanner ( System . in ) ; int tests = sc . nextInt ( ) ; for ( int test = 1 ; test <= tests ; ++ test ) { int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; int [ ] [ ] lawn = new int [ n ] [ m ] ; int [ ] rowMaxes = new int [ n ] ; int [ ] colMaxes = new int [ m ] ; boolean possible = true ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { int a = sc . nextInt ( ) ; lawn [ i ] [ j ] = a ; rowMaxes [ i ] = Math . max ( a , rowMaxes [ i ] ) ; colMaxes [ j ] = Math . max ( a , colMaxes [ j ] ) ; if ( a > 100 ) { possible = false ; } } } for ( int i = 0 ; i < n & possible ; ++ i ) { for ( int j = 0 ; j < m & possible ; ++ j ) { if ( lawn [ i ] [ j ] < rowMaxes [ i ] && lawn [ i ] [ j ] < colMaxes [ j ] ) { possible = false ; } } } System . out . println ( " Case ▁ # " + test + " : ▁ " + ( possible ? " YES " : " NO " ) ) ; } } }

def setup ( infile ) : NEW_LINE INDENT return locals ( ) NEW_LINE DEDENT def reader ( testcase , infile , ** ignore ) : NEW_LINE INDENT P = map ( int , infile . next ( ) . split ( ) ) NEW_LINE S = [ map ( int , infile . next ( ) . split ( ) ) for i in range ( P [ 0 ] ) ] NEW_LINE return locals ( ) NEW_LINE DEDENT def solver ( infile , testcase , N = None , P = None , I = None , T = None , S = None , C = None , ** ignore ) : NEW_LINE INDENT import numpypy as np NEW_LINE S = np . array ( S ) NEW_LINE done = np . zeros ( P , dtype = int ) NEW_LINE for row in range ( P [ 0 ] ) : NEW_LINE INDENT m = S [ row ] . max ( ) NEW_LINE done [ row ] [ S [ row ] == m ] = 1 NEW_LINE DEDENT for col in range ( P [ 1 ] ) : NEW_LINE INDENT m = S [ : , col ] . max ( ) NEW_LINE done [ : , col ] [ S [ : , col ] == m ] = 1 NEW_LINE DEDENT res = ' YES ' if done . sum ( ) == P [ 0 ] * P [ 1 ] else ' NO ' NEW_LINE return ' Case ▁ # % s : ▁ % s \n ' % ( testcase , res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT import sys NEW_LINE T = int ( sys . stdin . next ( ) ) NEW_LINE common = setup ( sys . stdin ) NEW_LINE for t in xrange ( 1 , T + 1 ) : NEW_LINE INDENT sys . stdout . write ( solver ( ** reader ( t , ** common ) ) ) NEW_LINE DEDENT DEDENT


n = int ( raw_input ( ) ) NEW_LINE for v in range ( n ) : NEW_LINE INDENT board = [ ] NEW_LINE inpline = raw_input ( ) . split ( ' ▁ ' ) NEW_LINE m = int ( inpline [ 0 ] ) NEW_LINE n = int ( inpline [ 1 ] ) NEW_LINE [ board . append ( [ int ( x ) for x in raw_input ( ) . split ( ' ▁ ' ) ] ) for i in range ( m ) ] NEW_LINE bs = sorted ( [ ( i , j ) for i in range ( m ) for j in range ( n ) ] , key = lambda ( i , j ) : board [ i ] [ j ] ) NEW_LINE good = True NEW_LINE for ( i , j ) in bs : NEW_LINE INDENT if board [ i ] [ j ] == ' X ' : continue NEW_LINE if all ( board [ i2 ] [ j ] in [ board [ i ] [ j ] , ' X ' ] for i2 in range ( m ) ) : NEW_LINE INDENT for i2 in range ( m ) : board [ i2 ] [ j ] = ' X ' NEW_LINE DEDENT elif all ( board [ i ] [ j2 ] in [ board [ i ] [ j ] , ' X ' ] for j2 in range ( n ) ) : NEW_LINE INDENT for j2 in range ( n ) : board [ i ] [ j2 ] = ' X ' NEW_LINE DEDENT else : NEW_LINE INDENT good = False NEW_LINE break NEW_LINE DEDENT DEDENT print ' Case ▁ # % d : ▁ % s ' % ( v + 1 , ' YES ' if good else ' NO ' ) NEW_LINE DEDENT


import sys NEW_LINE import time NEW_LINE timeit = 1 NEW_LINE debugv = 0 NEW_LINE def main ( ) : NEW_LINE INDENT T = int ( sys . stdin . readline ( ) ) NEW_LINE for case in range ( 1 , T + 1 ) : NEW_LINE INDENT doCase ( case ) NEW_LINE DEDENT DEDENT def doCase ( case ) : NEW_LINE INDENT N , M = [ int ( n ) for n in sys . stdin . readline ( ) . split ( ) ] NEW_LINE lawn = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT row = [ int ( n ) for n in sys . stdin . readline ( ) . split ( ) ] NEW_LINE if len ( row ) != M : NEW_LINE INDENT raise Exception ( " incorrect ▁ line ▁ length " ) NEW_LINE DEDENT lawn . append ( row ) NEW_LINE DEDENT lawn_possible = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT row_possible = 1 NEW_LINE for k in range ( N ) : NEW_LINE INDENT if lawn [ k ] [ j ] > lawn [ i ] [ j ] : NEW_LINE INDENT row_possible = 0 NEW_LINE DEDENT DEDENT col_possible = 1 NEW_LINE for k in range ( M ) : NEW_LINE INDENT if lawn [ i ] [ k ] > lawn [ i ] [ j ] : NEW_LINE INDENT col_possible = 0 NEW_LINE DEDENT DEDENT if not ( row_possible or col_possible ) : NEW_LINE INDENT lawn_possible = 0 NEW_LINE DEDENT DEDENT DEDENT result = ' YES ' if lawn_possible else ' NO ' NEW_LINE debug ( " { } \n " . format ( result ) ) NEW_LINE sys . stdout . write ( " Case ▁ # { } : ▁ { } \n " . format ( case , result ) ) NEW_LINE DEDENT def debug ( m ) : NEW_LINE INDENT if debugv : NEW_LINE INDENT sys . stderr . write ( m ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT startTime = time . clock ( ) NEW_LINE main ( ) NEW_LINE if timeit : NEW_LINE INDENT sys . stderr . write ( " Completed ▁ in ▁ { } ▁ seconds . \n " . format ( time . clock ( ) - startTime ) ) NEW_LINE DEDENT DEDENT


import sys NEW_LINE from collections import defaultdict NEW_LINE def readline ( ) : NEW_LINE INDENT s = raw_input ( ) . strip ( ) NEW_LINE while s == " " : NEW_LINE INDENT s = raw_input ( ) . strip ( ) NEW_LINE DEDENT return s NEW_LINE DEDENT def readvals ( ) : NEW_LINE INDENT return map ( int , readline ( ) . split ( ) ) NEW_LINE DEDENT def solve ( ) : NEW_LINE INDENT r , c = readvals ( ) NEW_LINE grid = tuple ( readvals ( ) for i in range ( r ) ) NEW_LINE row_min = tuple ( map ( max , grid ) ) NEW_LINE col_min = tuple ( map ( max , zip ( * grid ) ) ) NEW_LINE for y in range ( r ) : NEW_LINE INDENT for x in range ( c ) : NEW_LINE INDENT if grid [ y ] [ x ] != min ( row_min [ y ] , col_min [ x ] ) : NEW_LINE INDENT return " NO " NEW_LINE DEDENT DEDENT DEDENT return " YES " NEW_LINE DEDENT N = int ( readline ( ) ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT sol = solve ( ) NEW_LINE print ( " Case ▁ # { 0 } : ▁ { 1 } " . format ( i + 1 , sol ) ) NEW_LINE DEDENT


filename = ' B - large . in ' NEW_LINE def getCol ( array , c ) : NEW_LINE INDENT return [ row [ c ] for row in array ] NEW_LINE DEDENT def mowable ( array ) : NEW_LINE INDENT for i , line in enumerate ( array ) : NEW_LINE INDENT for j , col in enumerate ( line ) : NEW_LINE INDENT if ( array [ i ] [ j ] < max ( array [ i ] ) ) and ( array [ i ] [ j ] < max ( getCol ( array , j ) ) ) : NEW_LINE INDENT return ' NO ' NEW_LINE DEDENT DEDENT DEDENT return ' YES ' NEW_LINE DEDENT FILE = open ( filename ) NEW_LINE T = int ( FILE . readline ( ) ) NEW_LINE for i in range ( 1 , T + 1 ) : NEW_LINE INDENT rawLine = FILE . readline ( ) . split ( ' ▁ ' ) NEW_LINE rows , cols = int ( rawLine [ 0 ] ) , int ( rawLine [ 1 ] ) NEW_LINE array = [ 0 ] * rows NEW_LINE for r in range ( 0 , rows ) : NEW_LINE INDENT array [ r ] = [ int ( x ) for x in FILE . readline ( ) . split ( ' ▁ ' ) ] NEW_LINE DEDENT print ( ' Case ▁ # ' + str ( i ) + ' : ▁ ' + mowable ( array ) ) NEW_LINE DEDENT


package lawnmower ; public class Lawnmower { public static String [ ] execute ( String [ ] input ) { String [ ] words = { " NO " , " YES " } ; String [ ] out ; String [ ] splitted ; int n , m ; int [ ] [ ] board ; int result ; int test = 1 ; int ntests ; ntests = Integer . parseInt ( input [ 0 ] ) ; out = new String [ ntests ] ; for ( int i = 1 ; test < 1 + ntests ; ) { splitted = input [ i ++ ] . split ( " \\ s " ) ; n = Integer . parseInt ( splitted [ 0 ] ) ; m = Integer . parseInt ( splitted [ 1 ] ) ; board = new int [ n ] [ m ] ; for ( int j = 0 ; j < n ; j ++ ) { splitted = input [ i ++ ] . split ( " \\ s " ) ; for ( int k = 0 ; k < m ; k ++ ) { board [ j ] [ k ] = Integer . parseInt ( splitted [ k ] ) ; } } result = isFeasible ( board , n , m ) ; out [ test - 1 ] = " Case ▁ # " + test + " : ▁ " + words [ result ] ; System . out . println ( out [ test - 1 ] ) ; test ++ ; } return out ; } private static int isFeasible ( int [ ] [ ] board , int n , int m ) { int [ ] maxrow = new int [ n ] ; int [ ] maxcol = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( board [ i ] [ j ] > maxrow [ i ] ) maxrow [ i ] = board [ i ] [ j ] ; if ( board [ i ] [ j ] > maxcol [ j ] ) maxcol [ j ] = board [ i ] [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( board [ i ] [ j ] < maxrow [ i ] ) if ( board [ i ] [ j ] < maxcol [ j ] ) return 0 ; } } return 1 ; } }


import java . io . FileInputStream ; import java . io . FileOutputStream ; import java . io . PrintWriter ; import java . util . Scanner ; public class Lawnmower { public static boolean check ( int [ ] [ ] map , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { boolean flag = true , flag2 = true ; for ( int k = 0 ; k < N ; k ++ ) { if ( map [ k ] [ j ] > map [ i ] [ j ] ) { flag = false ; break ; } } for ( int k = 0 ; k < M ; k ++ ) { if ( map [ i ] [ k ] > map [ i ] [ j ] ) { flag2 = false ; break ; } } if ( ! flag && ! flag2 ) return false ; } } return true ; } public static void main ( String args [ ] ) throws Exception { Scanner in = new Scanner ( new FileInputStream ( " B - large . in " ) ) ; PrintWriter writer = new PrintWriter ( new FileOutputStream ( " B - large . out " ) ) ; int T = in . nextInt ( ) ; for ( int i = 0 ; i < T ; i ++ ) { int N = in . nextInt ( ) ; int M = in . nextInt ( ) ; int [ ] [ ] map = new int [ N ] [ M ] ; for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < M ; k ++ ) { map [ j ] [ k ] = in . nextInt ( ) ; } } boolean result = check ( map , N , M ) ; writer . write ( " Case ▁ # " + ( i + 1 ) + " : ▁ " ) ; writer . println ( result ? " YES " : " NO " ) ; } in . close ( ) ; writer . close ( ) ; } }


import java . io . PrintWriter ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . List ; import java . util . Scanner ; public class ProblemB { static final String YES = " YES " ; static final String NO = " NO " ; public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; PrintWriter out = new PrintWriter ( System . out ) ; int T = in . nextInt ( ) ; for ( int cn = 1 ; cn <= T ; cn ++ ) { int N = in . nextInt ( ) ; int M = in . nextInt ( ) ; int [ ] [ ] a = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { a [ i ] [ j ] = in . nextInt ( ) ; } } out . println ( String . format ( " Case ▁ # % d : ▁ % s " , cn , ( solve ( a ) ? YES : NO ) ) ) ; } out . flush ( ) ; } private static boolean solve ( int [ ] [ ] board ) { int N = board . length ; int M = board [ 0 ] . length ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int a = board [ i ] [ j ] ; int rmax = 0 , cmax = 0 ; for ( int x = 0 ; x < M ; x ++ ) { if ( x != j ) { rmax = Math . max ( rmax , board [ i ] [ x ] ) ; } } for ( int y = 0 ; y < N ; y ++ ) { if ( y != i ) { cmax = Math . max ( cmax , board [ y ] [ j ] ) ; } } if ( rmax > a && cmax > a ) { return false ; } } } return true ; } public static void debug ( Object ... o ) { System . err . println ( Arrays . deepToString ( o ) ) ; } }


import java . io . BufferedReader ; import java . io . BufferedWriter ; import java . io . File ; import java . io . FileReader ; import java . io . FileWriter ; import java . util . StringTokenizer ; public class Lawnmower { public static void main ( String [ ] args ) throws Exception { BufferedReader br = new BufferedReader ( new FileReader ( new File ( " in " ) ) ) ; BufferedWriter bw = new BufferedWriter ( new FileWriter ( new File ( " out " ) ) ) ; StringTokenizer st ; int T = Integer . parseInt ( br . readLine ( ) ) ; for ( int cn = 1 ; cn <= T ; cn ++ ) { st = new StringTokenizer ( br . readLine ( ) ) ; int N = Integer . parseInt ( st . nextToken ( ) ) ; int M = Integer . parseInt ( st . nextToken ( ) ) ; int arr [ ] [ ] = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { st = new StringTokenizer ( br . readLine ( ) ) ; for ( int j = 0 ; j < M ; j ++ ) { arr [ i ] [ j ] = Integer . parseInt ( st . nextToken ( ) ) ; } } boolean okay = true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int h = arr [ i ] [ j ] ; boolean ok1 = true ; for ( int k = 0 ; k < N ; k ++ ) ok1 &= arr [ k ] [ j ] <= h ; boolean ok2 = true ; for ( int k = 0 ; k < M ; k ++ ) ok2 &= arr [ i ] [ k ] <= h ; if ( ! ( ok1 || ok2 ) ) okay = false ; } } if ( okay ) bw . append ( " Case ▁ # " + cn + " : ▁ YES \n " ) ; else bw . append ( " Case ▁ # " + cn + " : ▁ NO \n " ) ; } bw . flush ( ) ; } }


import java . io . File ; import java . io . FileNotFoundException ; import java . io . PrintWriter ; import java . util . * ; public class Diamond implements Runnable { final Scanner in ; final PrintWriter out ; public Diamond ( ) throws FileNotFoundException { out = new PrintWriter ( getClass ( ) . getName ( ) . toLowerCase ( ) + " . out " ) ; in = new Scanner ( new File ( getClass ( ) . getName ( ) . toLowerCase ( ) + " . in " ) ) ; } public static void main ( String [ ] args ) { try { new Diamond ( ) . run ( ) ; } catch ( FileNotFoundException e ) { e . printStackTrace ( ) ; } } public void run ( ) { int tests = in . nextInt ( ) ; in . nextLine ( ) ; for ( int testcase = 1 ; testcase <= tests ; testcase ++ ) { int n = in . nextInt ( ) ; List < List < Integer > > edges = new ArrayList < List < Integer > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { List < Integer > cur = new ArrayList < Integer > ( ) ; int mi = in . nextInt ( ) ; for ( int x = 0 ; x < mi ; x ++ ) { cur . add ( in . nextInt ( ) - 1 ) ; } edges . add ( cur ) ; } boolean path = false ; for ( int st = 0 ; st < n ; st ++ ) { Queue < Integer > q = new ArrayDeque < Integer > ( ) ; Set < Integer > v = new HashSet < Integer > ( ) ; v . add ( st ) ; for ( q . offer ( st ) ; ! q . isEmpty ( ) ; q . poll ( ) ) { int c = q . peek ( ) ; for ( int d : edges . get ( c ) ) { if ( v . contains ( d ) ) { path = true ; continue ; } v . add ( d ) ; q . offer ( d ) ; } } } out . println ( " Case ▁ # " + testcase + " : ▁ " + ( path ? " Yes " : " No " ) ) ; } out . close ( ) ; in . close ( ) ; } }

import operator NEW_LINE import itertools NEW_LINE import functools NEW_LINE import math NEW_LINE from collections import deque NEW_LINE def bfs ( i ) : NEW_LINE INDENT res = False NEW_LINE visited = [ 0 ] * ( N + 1 ) NEW_LINE q = deque ( [ node [ i ] ] ) NEW_LINE while len ( q ) : NEW_LINE INDENT n = q . popleft ( ) NEW_LINE if visited [ n [ 0 ] ] : NEW_LINE INDENT res = True NEW_LINE return res NEW_LINE DEDENT else : NEW_LINE INDENT visited [ n [ 0 ] ] = 1 NEW_LINE DEDENT for x in n [ 1 : ] : NEW_LINE INDENT q . append ( node [ x ] ) NEW_LINE DEDENT DEDENT DEDENT fn = open ( '1 . in ' ) NEW_LINE ofn = open ( '1 . out ' , ' w ' ) NEW_LINE TC = int ( fn . readline ( ) ) NEW_LINE for tc in range ( TC ) : NEW_LINE INDENT N = int ( fn . readline ( ) . strip ( ) ) NEW_LINE node = [ None ] * ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT r = map ( int , fn . readline ( ) . strip ( ) . split ( ) ) NEW_LINE node [ i ] = [ i ] + r [ 1 : ] NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if bfs ( i ) : NEW_LINE INDENT print >> ofn , ' Case ▁ # { } : ▁ { } ' . format ( tc + 1 , ' Yes ' ) NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print >> ofn , ' Case ▁ # { } : ▁ { } ' . format ( tc + 1 , ' No ' ) NEW_LINE DEDENT DEDENT


import string NEW_LINE def FindAncestorsDiamond ( i , Parents , Ancestors ) : NEW_LINE INDENT PofI = Parents [ i ] NEW_LINE anI = PofI NEW_LINE for p in PofI : NEW_LINE INDENT if p not in Ancestors : NEW_LINE INDENT if FindAncestorsDiamond ( p , Parents , Ancestors ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT if len ( anI & Ancestors [ p ] ) > 0 : NEW_LINE INDENT return True NEW_LINE DEDENT anI = anI | Ancestors [ p ] NEW_LINE DEDENT Ancestors [ i ] = anI NEW_LINE return False NEW_LINE DEDENT def A ( inpfile ) : NEW_LINE INDENT fin = open ( inpfile , ' r ' ) NEW_LINE fout = open ( inpfile + ' . out ' , ' w ' ) NEW_LINE CNT = int ( fin . readline ( ) ) NEW_LINE for iCNT in xrange ( CNT ) : NEW_LINE INDENT Parents = [ ] NEW_LINE N = int ( fin . readline ( ) ) NEW_LINE for iC in xrange ( 1 , N + 1 ) : NEW_LINE INDENT Parents . append ( set ( map ( lambda x : int ( x ) - 1 , fin . readline ( ) . split ( ) [ 1 : ] ) ) ) NEW_LINE DEDENT Ancestors = { } NEW_LINE Answer = False NEW_LINE for iC in xrange ( N ) : NEW_LINE INDENT if FindAncestorsDiamond ( iC , Parents , Ancestors ) : NEW_LINE INDENT Answer = True NEW_LINE break NEW_LINE DEDENT DEDENT text = ' Case ▁ # ' + str ( iCNT + 1 ) + ' : ▁ ' + ( Answer and ' Yes ' or ' No ' ) NEW_LINE print text NEW_LINE fout . write ( text + ' \n ' ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A ( ' . . \\test\\A - large . in ' ) ; NEW_LINE DEDENT


import itertools NEW_LINE class DiamondFound ( Exception ) : NEW_LINE INDENT pass NEW_LINE DEDENT def find_ancestors ( class_num , inheritance , ancestors ) : NEW_LINE INDENT if ancestors [ class_num ] : NEW_LINE INDENT return NEW_LINE DEDENT direct_fathers = inheritance [ class_num ] NEW_LINE ancestors [ class_num ] . update ( direct_fathers ) NEW_LINE for direct_father in direct_fathers : NEW_LINE INDENT find_ancestors ( direct_father , inheritance , ancestors ) NEW_LINE ancestors_of_direct_father = ancestors [ direct_father ] NEW_LINE before = len ( ancestors [ class_num ] ) NEW_LINE ancestors [ class_num ] . update ( ancestors_of_direct_father ) NEW_LINE after = len ( ancestors [ class_num ] ) NEW_LINE if after - before != len ( ancestors_of_direct_father ) : NEW_LINE INDENT raise DiamondFound ( ) NEW_LINE DEDENT DEDENT DEDENT def solve ( num_of_classes , inheritance ) : NEW_LINE INDENT ancestors = dict ( ( i , set ( ) ) for i in xrange ( 1 , num_of_classes + 1 ) ) NEW_LINE for class_num in inheritance : NEW_LINE INDENT try : NEW_LINE INDENT find_ancestors ( class_num , inheritance , ancestors ) NEW_LINE DEDENT except DiamondFound : NEW_LINE INDENT return ' Yes ' NEW_LINE DEDENT DEDENT return ' No ' NEW_LINE DEDENT def process_files ( in_file , out_file ) : NEW_LINE INDENT num_of_test_cases = int ( in_file . next ( ) . strip ( ) ) NEW_LINE for test_number in xrange ( num_of_test_cases ) : NEW_LINE INDENT num_of_classes = int ( in_file . next ( ) . strip ( ) ) NEW_LINE inheritance = { } NEW_LINE for class_num in xrange ( 1 , num_of_classes + 1 ) : NEW_LINE INDENT params = in_file . next ( ) . strip ( ) . split ( ) NEW_LINE assert len ( params ) - 1 == int ( params [ 0 ] ) NEW_LINE inheritance [ class_num ] = [ int ( i ) for i in params [ 1 : ] ] NEW_LINE DEDENT result = solve ( num_of_classes , inheritance ) NEW_LINE out_file . write ( ' Case ▁ # % d : ▁ % s \n ' % ( test_number + 1 , result ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT with open ( ' A - large . in ' , ' rb ' ) as in_file : NEW_LINE INDENT with open ( ' A - large . out ' , ' wb ' ) as out_file : NEW_LINE INDENT process_files ( in_file , out_file ) NEW_LINE DEDENT DEDENT DEDENT


import sys NEW_LINE sys . setrecursionlimit ( 10000 ) NEW_LINE def get ( n , graph ) : NEW_LINE INDENT visited = [ False ] * n NEW_LINE order = [ ] NEW_LINE def dfs ( now ) : NEW_LINE INDENT if visited [ now ] : return NEW_LINE visited [ now ] = True NEW_LINE for x in graph [ now ] : NEW_LINE INDENT dfs ( x ) NEW_LINE DEDENT order . append ( now ) NEW_LINE DEDENT for x in xrange ( n ) : NEW_LINE INDENT dfs ( x ) NEW_LINE DEDENT def check ( now ) : NEW_LINE INDENT checked [ now ] = True NEW_LINE if visited [ now ] : return True NEW_LINE visited [ now ] = True NEW_LINE for x in graph [ now ] : NEW_LINE INDENT if check ( x ) : return True NEW_LINE DEDENT return False NEW_LINE DEDENT checked = [ False ] * n NEW_LINE for x in reversed ( order ) : NEW_LINE INDENT visited = [ False ] * n NEW_LINE if not checked [ x ] and check ( x ) : return ' Yes ' NEW_LINE DEDENT return ' No ' NEW_LINE DEDENT t = input ( ) NEW_LINE for x in xrange ( t ) : NEW_LINE INDENT n = input ( ) NEW_LINE graph = [ [ ] for i in xrange ( n ) ] NEW_LINE for y in xrange ( n ) : NEW_LINE INDENT nums = map ( int , raw_input ( ) . strip ( ) . split ( ) ) NEW_LINE for z in nums [ 1 : ] : NEW_LINE INDENT graph [ y ] . append ( z - 1 ) NEW_LINE DEDENT DEDENT print ' Case ▁ # % d : ▁ % s ' % ( x + 1 , get ( n , graph ) ) NEW_LINE DEDENT


def iterative_dfs ( graph , start , path = [ ] ) : NEW_LINE INDENT q = [ start ] NEW_LINE while q : NEW_LINE INDENT v = q . pop ( 0 ) NEW_LINE if v not in path : NEW_LINE INDENT path = path + [ v ] NEW_LINE q = graph [ v ] + q NEW_LINE DEDENT DEDENT return path NEW_LINE DEDENT def iterative_bfs ( graph , start , path = [ ] ) : NEW_LINE INDENT q = [ start ] NEW_LINE while q : NEW_LINE INDENT v = q . pop ( 0 ) NEW_LINE if not v in path : NEW_LINE INDENT path = path + [ v ] NEW_LINE q = q + graph [ v ] NEW_LINE DEDENT DEDENT return path NEW_LINE DEDENT T = int ( raw_input ( ) ) NEW_LINE for t in range ( 1 , T + 1 ) : NEW_LINE INDENT N = int ( raw_input ( ) ) NEW_LINE G = dict ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT G [ i ] = [ ] NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT line = map ( int , raw_input ( ) . split ( ) ) [ 1 : ] NEW_LINE for j in line : NEW_LINE INDENT G [ i ] . append ( j ) NEW_LINE DEDENT DEDENT parents = dict ( ) NEW_LINE for i in G : NEW_LINE INDENT parents [ i ] = set ( iterative_bfs ( G , i , [ ] ) ) NEW_LINE DEDENT diamond = False NEW_LINE for node in G : NEW_LINE INDENT if diamond : NEW_LINE INDENT break NEW_LINE DEDENT visited = set ( ) NEW_LINE for par in G [ node ] : NEW_LINE INDENT if diamond : NEW_LINE INDENT break NEW_LINE DEDENT for x in parents [ par ] : NEW_LINE INDENT if x in visited : NEW_LINE INDENT diamond = True NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT visited . add ( x ) NEW_LINE DEDENT DEDENT DEDENT DEDENT print " Case ▁ # { 0 } : ▁ { 1 } " . format ( t , " Yes " if diamond else " No " ) NEW_LINE DEDENT


import java . io . BufferedWriter ; import java . io . File ; import java . io . FileWriter ; import java . util . Scanner ; import java . util . logging . Level ; import java . util . logging . Logger ; public class R1CC { static boolean [ ] g ; static int [ ] [ ] p ; static int [ ] d ; public static void main ( String [ ] args ) { try { Scanner sc = new Scanner ( new File ( " A - large . in " ) ) ; BufferedWriter bw = new BufferedWriter ( new FileWriter ( new File ( " output . out " ) ) ) ; int t = sc . nextInt ( ) ; X : for ( int i = 0 ; i < t ; i ++ ) { bw . write ( " Case ▁ # " + String . valueOf ( i + 1 ) + " : ▁ " ) ; int r = 0 ; int n = sc . nextInt ( ) ; d = new int [ n ] ; p = new int [ n ] [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { d [ j ] = sc . nextInt ( ) ; for ( int k = 0 ; k < d [ j ] ; k ++ ) p [ j ] [ k ] = sc . nextInt ( ) - 1 ; } for ( int j = 0 ; j < n ; j ++ ) { g = new boolean [ n ] ; if ( traverse ( j ) ) { bw . write ( " Yes " ) ; bw . newLine ( ) ; bw . flush ( ) ; continue X ; } } bw . write ( " No " ) ; bw . newLine ( ) ; bw . flush ( ) ; } bw . close ( ) ; } catch ( Exception ex ) { Logger . getLogger ( QA . class . getName ( ) ) . log ( Level . SEVERE , null , ex ) ; } } static boolean traverse ( int h ) { for ( int i = 0 ; i < d [ h ] ; i ++ ) { if ( g [ p [ h ] [ i ] ] ) return true ; else g [ p [ h ] [ i ] ] = true ; if ( traverse ( p [ h ] [ i ] ) ) return true ; } return false ; } }


package codejam . round1c_2012 ; import java . io . File ; import java . io . PrintStream ; import java . util . ArrayDeque ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . Deque ; import java . util . HashSet ; import java . util . List ; import java . util . Scanner ; import java . util . Set ; public class MainA { static char [ ] mapping = new char [ 256 ] ; static List < Set < Integer > > defs ; public static void main ( String [ ] args ) throws Exception { String f = " / home / floris / dev / java / Test / src / codejam / round1c _ 2012 / A - large . in " ; Scanner sc = new Scanner ( new File ( f ) ) ; System . setOut ( new PrintStream ( new File ( f + " . out " ) ) ) ; int T = sc . nextInt ( ) ; for ( int i = 1 ; T -- > 0 ; i ++ ) { defs = new ArrayList < Set < Integer > > ( ) ; int classCount = sc . nextInt ( ) ; for ( int j = 0 ; j < classCount ; j ++ ) { Set < Integer > cur = new HashSet < Integer > ( ) ; int ic = sc . nextInt ( ) ; for ( int k = 0 ; k < ic ; k ++ ) cur . add ( sc . nextInt ( ) - 1 ) ; defs . add ( cur ) ; } System . out . printf ( " Case ▁ # % d : ▁ % s % n " , i , solve ( ) ) ; } } private static String solve ( ) { for ( int i = 0 ; i < defs . size ( ) ; i ++ ) { List < Integer > contained = new ArrayList < Integer > ( ) ; Deque < Integer > d = new ArrayDeque ( ) ; d . addAll ( defs . get ( i ) ) ; contained . addAll ( defs . get ( i ) ) ; contained . add ( i ) ; while ( ! d . isEmpty ( ) ) { int v = d . remove ( ) ; for ( int k : defs . get ( v ) ) { if ( contained . contains ( k ) ) return " Yes " ; d . add ( k ) ; contained . add ( k ) ; } } } return " No " ; } }


import java . io . * ; import java . util . * ; public class A { public static void main ( String args [ ] ) throws IOException { BufferedReader in = new BufferedReader ( new FileReader ( " A - small - attempt0 . in " ) ) ; PrintWriter out = new PrintWriter ( new FileWriter ( " A - small . out " ) ) ; int T = Integer . parseInt ( in . readLine ( ) ) ; for ( int i = 0 ; i < T ; i ++ ) { boolean adj [ ] [ ] = new boolean [ 1005 ] [ 1005 ] ; int N ; String s = in . readLine ( ) ; StringTokenizer st = new StringTokenizer ( s ) ; N = Integer . parseInt ( st . nextToken ( ) ) ; for ( int r = 0 ; r < N ; r ++ ) { s = in . readLine ( ) ; st = new StringTokenizer ( s ) ; int M = Integer . parseInt ( st . nextToken ( ) ) ; for ( int c = 0 ; c < M ; c ++ ) { int d = Integer . parseInt ( st . nextToken ( ) ) - 1 ; adj [ r ] [ d ] = true ; } } boolean ans = false ; for ( int n = 0 ; n < N ; n ++ ) { LinkedList < Integer > q = new LinkedList < Integer > ( ) ; q . add ( n ) ; boolean v [ ] = new boolean [ N ] ; while ( ! q . isEmpty ( ) ) { int x = q . pollFirst ( ) ; if ( v [ x ] ) { ans = true ; break ; } v [ x ] = true ; for ( int m = 0 ; m < N ; m ++ ) { if ( adj [ x ] [ m ] ) { q . addFirst ( m ) ; } } } } out . println ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + ( ans ? " Yes " : " No " ) ) ; } out . close ( ) ; } }


import java . util . ArrayList ; import java . util . HashSet ; import java . util . LinkedList ; import java . util . List ; import java . util . Scanner ; import java . util . Set ; public class Q1 { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int numTests = s . nextInt ( ) ; for ( int test = 0 ; test < numTests ; test ++ ) { System . out . println ( " Case ▁ # " + ( test + 1 ) + " : ▁ " + getResult ( s ) ) ; } } private static String getResult ( Scanner s ) { int numNodes = s . nextInt ( ) ; List < GraphNode > nodes = new ArrayList < GraphNode > ( numNodes ) ; for ( int i = 0 ; i < numNodes ; i ++ ) { nodes . add ( new GraphNode ( ) ) ; } for ( int i = 0 ; i < numNodes ; i ++ ) { int numChildren = s . nextInt ( ) ; for ( int j = 0 ; j < numChildren ; j ++ ) { int child = s . nextInt ( ) ; nodes . get ( child - 1 ) . childNodes . add ( nodes . get ( i ) ) ; } } for ( GraphNode node : nodes ) { if ( node . childNodes . size ( ) > 1 && node . hasDiamond ( ) ) { return " Yes " ; } } return " No " ; } public static class GraphNode { public List < GraphNode > childNodes = new LinkedList < GraphNode > ( ) ; public boolean hasDiamond ( ) { List < GraphNode > foundSoFar = new LinkedList < GraphNode > ( ) ; Set < GraphNode > fastLookup = new HashSet < GraphNode > ( ) ; foundSoFar . add ( this ) ; fastLookup . add ( this ) ; while ( foundSoFar . size ( ) > 0 ) { GraphNode node = foundSoFar . remove ( 0 ) ; for ( GraphNode child : node . childNodes ) { if ( fastLookup . contains ( child ) ) return true ; foundSoFar . add ( child ) ; fastLookup . add ( child ) ; } } return false ; } } }


import java . util . Scanner ; public class iCode2012OutofGas { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; for ( int index = 1 ; index <= T ; index ++ ) { System . out . println ( " Case ▁ # " + index + " : " ) ; double D = in . nextDouble ( ) ; int N = in . nextInt ( ) ; int A = in . nextInt ( ) ; double [ ] time = new double [ N + 1 ] ; double [ ] pos = new double [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { time [ i ] = in . nextDouble ( ) ; pos [ i ] = in . nextDouble ( ) ; } for ( int acc = 0 ; acc < A ; acc ++ ) { double a = in . nextDouble ( ) ; double startmovingat = 0 ; for ( int i = 1 ; i < N ; i ++ ) { startmovingat = Math . max ( startmovingat , time [ i ] - Math . sqrt ( 2 * pos [ i ] / a ) ) ; } double effectivetime = time [ N - 1 ] + ( time [ N ] - time [ N - 1 ] ) / ( pos [ N ] - pos [ N - 1 ] ) * ( D - pos [ N - 1 ] ) ; startmovingat = Math . max ( startmovingat , effectivetime - Math . sqrt ( 2 * D / a ) ) ; System . out . println ( Math . max ( effectivetime , startmovingat + Math . sqrt ( 2 * D / a ) ) ) ; } } } }

import os , sys , math NEW_LINE inFile = None NEW_LINE outFile = None NEW_LINE def printAnswer ( case , answer ) : NEW_LINE INDENT outFile . write ( ' Case ▁ # { 0 } : ▁ { 1 } \n ' . format ( case + 1 , answer ) ) NEW_LINE DEDENT def solveSimple ( tc , a , d ) : NEW_LINE INDENT t = math . sqrt ( 2.0 * d / a ) NEW_LINE if t < tc : NEW_LINE INDENT t = tc NEW_LINE DEDENT outFile . write ( ' { 0 } \n ' . format ( t ) ) NEW_LINE DEDENT def solveCase ( caseNo ) : NEW_LINE INDENT ( d , n , a ) = inFile . readline ( ) . strip ( ) . split ( ) NEW_LINE d = float ( d ) NEW_LINE n = int ( n ) NEW_LINE a = int ( a ) NEW_LINE printAnswer ( caseNo , ' ' ) NEW_LINE xc = d + 100.0 NEW_LINE tc = 0 NEW_LINE if n == 1 : NEW_LINE INDENT ( t0 , x0 ) = map ( float , inFile . readline ( ) . strip ( ) . split ( ) ) NEW_LINE if x0 > d + ( 0.00000001 ) : NEW_LINE INDENT tc = 0 NEW_LINE DEDENT else : NEW_LINE INDENT tc = t0 NEW_LINE print tc NEW_LINE DEDENT DEDENT if n == 2 : NEW_LINE INDENT ( t0 , x0 ) = map ( float , inFile . readline ( ) . strip ( ) . split ( ) ) NEW_LINE ( t1 , x1 ) = map ( float , inFile . readline ( ) . strip ( ) . split ( ) ) NEW_LINE vc = ( x1 - x0 ) / ( t1 - t0 ) NEW_LINE if x1 > d : NEW_LINE INDENT tc = t0 + ( d - x0 ) / vc NEW_LINE DEDENT else : NEW_LINE INDENT tc = t1 NEW_LINE DEDENT DEDENT av = map ( float , inFile . readline ( ) . strip ( ) . split ( ) ) NEW_LINE for a in av : NEW_LINE INDENT solveSimple ( tc , a , d ) NEW_LINE DEDENT DEDENT def main ( ) : NEW_LINE INDENT cases = int ( inFile . readline ( ) . strip ( ) ) NEW_LINE for case in range ( cases ) : NEW_LINE INDENT solveCase ( case ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT inFile = open ( ' in . txt ' , ' rt ' ) NEW_LINE outFile = open ( ' out . txt ' , ' wt ' ) NEW_LINE main ( ) NEW_LINE DEDENT


from math import * NEW_LINE T = int ( raw_input ( ) ) NEW_LINE for tInput in xrange ( 1 , T + 1 ) : NEW_LINE INDENT line = raw_input ( ) . split ( ) NEW_LINE D = float ( line [ 0 ] ) NEW_LINE N , A = int ( line [ 1 ] ) , int ( line [ 2 ] ) NEW_LINE ts = [ 0 ] * N NEW_LINE xs = [ 0 ] * N NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT line = raw_input ( ) . split ( ) NEW_LINE ts [ i ] , xs [ i ] = float ( line [ 0 ] ) , float ( line [ 1 ] ) NEW_LINE DEDENT accelerations = map ( float , raw_input ( ) . split ( ) ) NEW_LINE print " Case ▁ # % d : " % tInput NEW_LINE c = xs [ 0 ] NEW_LINE if N == 1 : v0 = 0 NEW_LINE else : v0 = ( xs [ 1 ] - xs [ 0 ] ) / ( ts [ 1 ] - ts [ 0 ] ) NEW_LINE if v0 == 0 : NEW_LINE INDENT for a in accelerations : NEW_LINE INDENT print sqrt ( 2 * D / a ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT t2 = ( D - c ) / v0 ; NEW_LINE for a in accelerations : NEW_LINE INDENT t2star = ( v0 + sqrt ( v0 * v0 + 2 * a * c ) ) / a NEW_LINE if a < ( 2 * t2 * v0 + 2 * c ) / ( t2 * t2 ) or v0 > a * t2 : print sqrt ( 2 * D / a ) NEW_LINE else : print t2 NEW_LINE DEDENT DEDENT DEDENT


inp = open ( ' . / B - large . in ' , ' r ' ) NEW_LINE outp = open ( ' . / B . out ' , ' w ' ) NEW_LINE T = int ( inp . readline ( ) ) NEW_LINE def solve ( i ) : NEW_LINE INDENT I = inp . readline ( ) . replace ( ' \n ' , ' ' ) . split ( ' ▁ ' ) NEW_LINE D = float ( I [ 0 ] ) NEW_LINE N = int ( I [ 1 ] ) NEW_LINE A = int ( I [ 2 ] ) NEW_LINE Ns = [ ] NEW_LINE for n in range ( N ) : NEW_LINE INDENT Ns . append ( [ float ( x ) for x in inp . readline ( ) . replace ( ' \n ' , ' ' ) . split ( ' ▁ ' ) ] ) NEW_LINE if Ns [ n ] [ 1 ] > D : NEW_LINE INDENT if n != 0 : Ns [ n ] [ 0 ] = Ns [ n - 1 ] [ 0 ] + ( D - Ns [ n - 1 ] [ 1 ] ) / ( Ns [ n ] [ 1 ] - Ns [ n - 1 ] [ 1 ] ) * ( Ns [ n ] [ 0 ] - Ns [ n - 1 ] [ 0 ] ) NEW_LINE Ns [ n ] [ 1 ] = D NEW_LINE DEDENT DEDENT As = [ float ( x ) for x in inp . readline ( ) . replace ( ' \n ' , ' ' ) . split ( ' ▁ ' ) ] NEW_LINE Mx = [ 0 ] * A NEW_LINE for n in range ( N ) : NEW_LINE INDENT for a in range ( A ) : NEW_LINE INDENT Mx [ a ] = max ( Mx [ a ] , Ns [ n ] [ 0 ] - ( 2 * Ns [ n ] [ 1 ] / As [ a ] ) ** 0.5 ) NEW_LINE DEDENT DEDENT outp . write ( ' Case ▁ # ' + str ( i ) + ' : \n ' ) NEW_LINE for a in range ( A ) : NEW_LINE INDENT x = ( 2 * D / As [ a ] ) ** 0.5 + Mx [ a ] NEW_LINE outp . write ( str ( x ) + ' \n ' ) NEW_LINE DEDENT DEDENT for k in range ( T ) : NEW_LINE INDENT solve ( k + 1 ) NEW_LINE DEDENT inp . close ( ) NEW_LINE outp . close ( ) NEW_LINE


import sys NEW_LINE from fractions import Fraction NEW_LINE from math import sqrt NEW_LINE line = sys . stdin . readline ( ) NEW_LINE fields = line . split ( ) NEW_LINE assert len ( fields ) == 1 NEW_LINE ntc = int ( fields [ 0 ] ) NEW_LINE def solve ( d , a , other_car ) : NEW_LINE INDENT wait_time = Fraction ( 0 ) NEW_LINE first = True NEW_LINE for time , distance in other_car : NEW_LINE INDENT if distance > d : NEW_LINE INDENT if first : NEW_LINE INDENT break NEW_LINE DEDENT time = last_time + ( time - last_time ) * ( d - last_distance ) / ( distance - last_distance ) NEW_LINE distance = d NEW_LINE DEDENT first = False NEW_LINE arrival_time = sqrt ( 2 * distance / a ) NEW_LINE if arrival_time < time : NEW_LINE INDENT cur_wait_time = time - arrival_time NEW_LINE DEDENT else : NEW_LINE INDENT cur_wait_time = Fraction ( 0 ) NEW_LINE DEDENT if cur_wait_time > wait_time : NEW_LINE INDENT wait_time = cur_wait_time NEW_LINE DEDENT last_time , last_distance = time , distance NEW_LINE DEDENT arrival_time = sqrt ( 2 * d / a ) NEW_LINE return wait_time + arrival_time NEW_LINE DEDENT for tc in range ( 1 , ntc + 1 ) : NEW_LINE INDENT line = sys . stdin . readline ( ) NEW_LINE fields = line . split ( ) NEW_LINE assert len ( fields ) == 3 NEW_LINE d = Fraction ( fields [ 0 ] ) NEW_LINE n = int ( fields [ 1 ] ) NEW_LINE a = int ( fields [ 2 ] ) NEW_LINE other_car = [ ] NEW_LINE for _ in range ( n ) : NEW_LINE INDENT line = sys . stdin . readline ( ) NEW_LINE fields = line . split ( ) NEW_LINE assert len ( fields ) == 2 NEW_LINE time = Fraction ( fields [ 0 ] ) NEW_LINE distance = Fraction ( fields [ 1 ] ) NEW_LINE other_car . append ( ( time , distance ) ) NEW_LINE DEDENT line = sys . stdin . readline ( ) NEW_LINE fields = line . split ( ) NEW_LINE assert len ( fields ) == a NEW_LINE print ( ' Case ▁ # { 0 } : ' . format ( tc ) ) NEW_LINE for i in range ( a ) : NEW_LINE INDENT accel = Fraction ( fields [ i ] ) NEW_LINE ans = solve ( d , accel , other_car ) NEW_LINE print ( ans ) NEW_LINE DEDENT DEDENT


import sys NEW_LINE import re NEW_LINE import math NEW_LINE import fractions NEW_LINE f = open ( sys . argv [ 1 ] , ' r ' ) NEW_LINE num = int ( f . readline ( ) ) NEW_LINE for z in range ( num ) : NEW_LINE INDENT d , n , m = f . readline ( ) . split ( ) NEW_LINE d = float ( d ) NEW_LINE n = int ( n ) NEW_LINE m = int ( m ) NEW_LINE tx = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT tx . append ( [ float ( x ) for x in f . readline ( ) . split ( ) ] ) NEW_LINE DEDENT tx . sort ( ) NEW_LINE a = [ float ( x ) for x in f . readline ( ) . split ( ) ] NEW_LINE if len ( tx ) > 1 : NEW_LINE INDENT tf = tx [ 0 ] [ 0 ] + ( d - tx [ 0 ] [ 1 ] ) * ( tx [ 1 ] [ 0 ] - tx [ 0 ] [ 0 ] ) / ( tx [ 1 ] [ 1 ] - tx [ 0 ] [ 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT tf = 0 NEW_LINE DEDENT print ' Case ▁ # { } : ' . format ( z + 1 ) NEW_LINE for x in a : NEW_LINE INDENT print max ( math . sqrt ( 2 * d / x ) , tf ) NEW_LINE DEDENT sys . stdout . flush ( ) NEW_LINE DEDENT


import java . util . Scanner ; public class Gas { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int T = sc . nextInt ( ) ; for ( int C = 1 ; C <= T ; C ++ ) { System . out . printf ( " Case ▁ # % d : \n " , C ) ; double D = sc . nextDouble ( ) ; int N = sc . nextInt ( ) ; int A = sc . nextInt ( ) ; if ( N == 1 ) { sc . nextDouble ( ) ; sc . nextDouble ( ) ; for ( int i = 0 ; i < A ; i ++ ) { double a = sc . nextDouble ( ) ; System . out . println ( Math . sqrt ( 2 * D / a ) ) ; } continue ; } double t1 = sc . nextDouble ( ) ; double d1 = sc . nextDouble ( ) ; double t2 = sc . nextDouble ( ) ; double d2 = sc . nextDouble ( ) ; double v = ( d2 - d1 ) / t2 ; for ( int i = 0 ; i < A ; i ++ ) { double a = sc . nextDouble ( ) ; double t_meet = ( v + Math . sqrt ( v * v + 2 * a * d1 ) ) / a ; double d_meet = d1 + v * t_meet ; if ( d_meet >= D ) { System . out . println ( Math . sqrt ( 2 * D / a ) ) ; } else { System . out . println ( t_meet + ( D - d_meet ) / v ) ; } } } } }


package round1c ; import java . util . Locale ; import java . util . Scanner ; public class B { public static void main ( String [ ] args ) { Locale . setDefault ( Locale . ENGLISH ) ; Scanner sc = new Scanner ( System . in ) ; int t = sc . nextInt ( ) ; for ( int i = 0 ; i < t ; i ++ ) { double d = sc . nextDouble ( ) ; int n = sc . nextInt ( ) ; int a = sc . nextInt ( ) ; double times [ ] = new double [ n ] ; double positions [ ] = new double [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { times [ j ] = sc . nextDouble ( ) ; positions [ j ] = sc . nextDouble ( ) ; } System . out . println ( " Case ▁ # " + ( i + 1 ) + " : " ) ; for ( int j = 0 ; j < a ; j ++ ) { double acc = sc . nextDouble ( ) ; double otherTime = n == 1 ? 0 : ( d - positions [ 0 ] ) / ( positions [ 1 ] - positions [ 0 ] ) * ( times [ 1 ] - times [ 0 ] ) + times [ 0 ] ; double myTime = Math . sqrt ( d * 2 / acc ) ; System . out . println ( " " + Math . max ( otherTime , myTime ) ) ; } } } }


import java . util . * ; import java . io . * ; public class B { public static void main ( String [ ] args ) throws Exception { Scanner in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; double D ; int N , A ; for ( int caseNo = 1 ; caseNo <= T ; caseNo ++ ) { D = in . nextDouble ( ) ; N = in . nextInt ( ) ; A = in . nextInt ( ) ; double anotherVelocity ; double [ ] t = new double [ N ] ; double [ ] x = new double [ N ] ; double minTime = 0 ; for ( int i = 0 ; i < N ; i ++ ) { t [ i ] = in . nextDouble ( ) ; x [ i ] = in . nextDouble ( ) ; } if ( N == 2 ) { anotherVelocity = ( x [ 1 ] - x [ 0 ] ) / ( t [ 1 ] - t [ 0 ] ) ; minTime = ( D - x [ 0 ] ) / anotherVelocity + t [ 0 ] ; } System . out . println ( " Case ▁ # " + caseNo + " : " ) ; for ( int i = 0 ; i < A ; i ++ ) { double a = in . nextDouble ( ) ; if ( N == 1 ) { System . out . printf ( " % .7f \n " , Math . sqrt ( 2 * D / a ) ) ; } else { System . out . printf ( " % .7f \n " , Math . max ( Math . sqrt ( 2 * D / a ) , minTime ) ) ; } } } } }


package round2 ; import java . util . Scanner ; public class A { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int cases = sc . nextInt ( ) ; for ( int caze = 1 ; caze <= cases ; caze ++ ) { int N = sc . nextInt ( ) ; int P = sc . nextInt ( ) ; int [ ] cant = new int [ P ] ; for ( int i = 0 ; i < N ; i ++ ) { cant [ sc . nextInt ( ) % P ] ++ ; } int ans = cant [ 0 ] ; if ( P == 2 ) { ans += ( cant [ 1 ] + 1 ) / 2 ; } else if ( P == 3 ) { ans += Math . min ( cant [ 1 ] , cant [ 2 ] ) ; int left = Math . max ( cant [ 1 ] , cant [ 2 ] ) - Math . min ( cant [ 1 ] , cant [ 2 ] ) ; ans += ( left + 2 ) / 3 ; } else { ans += Math . min ( cant [ 1 ] , cant [ 3 ] ) ; int left = Math . max ( cant [ 1 ] , cant [ 3 ] ) - Math . min ( cant [ 1 ] , cant [ 3 ] ) ; ans += cant [ 2 ] / 2 ; boolean extra2 = cant [ 2 ] % 2 == 1 ; if ( left >= 2 && extra2 ) { ans ++ ; left -= 2 ; extra2 = false ; } if ( extra2 ) left ++ ; ans += ( left + 3 ) / 4 ; } System . out . println ( " Case ▁ # " + caze + " : ▁ " + ans ) ; } } }

import numpy as np NEW_LINE inname = " input . txt " NEW_LINE outname = " output . txt " NEW_LINE with open ( inname , ' r ' ) as f : NEW_LINE INDENT cases = int ( f . readline ( ) ) NEW_LINE for tc in range ( 1 , cases + 1 ) : NEW_LINE INDENT line = f . readline ( ) . strip ( ) . split ( ' ▁ ' ) NEW_LINE N = int ( line [ 0 ] ) NEW_LINE P = int ( line [ 1 ] ) NEW_LINE Pi = [ ] NEW_LINE R = [ 0 ] * P NEW_LINE line = f . readline ( ) . strip ( ) . split ( ' ▁ ' ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT Pi . append ( int ( line [ i ] ) ) NEW_LINE R [ Pi [ i ] % P ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE if P == 2 : NEW_LINE INDENT ans = R [ 0 ] + ( R [ 1 ] + 1 ) // 2 NEW_LINE DEDENT elif P == 3 : NEW_LINE INDENT ans = R [ 0 ] NEW_LINE if R [ 1 ] < R [ 2 ] : NEW_LINE INDENT a = R [ 1 ] NEW_LINE b = R [ 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT a = R [ 2 ] NEW_LINE b = R [ 1 ] NEW_LINE DEDENT ans += a NEW_LINE b -= a NEW_LINE ans += ( b + 2 ) // 3 NEW_LINE DEDENT else : NEW_LINE INDENT ans = R [ 0 ] NEW_LINE if R [ 1 ] < R [ 3 ] : NEW_LINE INDENT a = R [ 1 ] NEW_LINE b = R [ 3 ] NEW_LINE DEDENT else : NEW_LINE INDENT a = R [ 3 ] NEW_LINE b = R [ 1 ] NEW_LINE DEDENT c = R [ 2 ] NEW_LINE ans += c // 2 NEW_LINE c %= 2 NEW_LINE ans += a NEW_LINE b -= a NEW_LINE if c == 1 and b >= 2 : NEW_LINE INDENT c -= 1 NEW_LINE b -= 2 NEW_LINE ans += 1 NEW_LINE DEDENT ans += b // 4 NEW_LINE b %= 4 NEW_LINE if c + b > 0 : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT print ( " Case ▁ # % d : ▁ % d " % ( tc , ans ) ) NEW_LINE DEDENT DEDENT


def maximizeFreshChocolate ( N , P , groups ) : NEW_LINE INDENT remainingGroups = groups NEW_LINE result = 0 NEW_LINE while len ( remainingGroups ) > 0 : NEW_LINE INDENT optimalSets = { } NEW_LINE optimalCounts = { } NEW_LINE for index in xrange ( len ( remainingGroups ) ) : NEW_LINE INDENT tempOptimalSets = { } NEW_LINE tempOptimalCounts = { } NEW_LINE for remainder in optimalCounts : NEW_LINE INDENT possibleRemainder = ( remainder + remainingGroups [ index ] ) % P NEW_LINE if optimalCounts [ remainder ] + 1 < tempOptimalCounts . get ( possibleRemainder , 10 ** 18 ) : NEW_LINE INDENT tempOptimalSets [ possibleRemainder ] = optimalSets [ remainder ] + [ index ] NEW_LINE tempOptimalCounts [ possibleRemainder ] = optimalCounts [ remainder ] + 1 NEW_LINE DEDENT DEDENT tempOptimalSets [ remainingGroups [ index ] % P ] = [ index ] NEW_LINE tempOptimalCounts [ remainingGroups [ index ] % P ] = 1 NEW_LINE for remainder in tempOptimalCounts : NEW_LINE INDENT if tempOptimalCounts [ remainder ] < optimalCounts . get ( remainder , 10 ** 18 ) : NEW_LINE INDENT optimalSets [ remainder ] = tempOptimalSets [ remainder ] NEW_LINE optimalCounts [ remainder ] = tempOptimalCounts [ remainder ] NEW_LINE DEDENT DEDENT DEDENT if 0 in optimalCounts : NEW_LINE INDENT result += 1 NEW_LINE remainingGroups = [ remainingGroups [ i ] for i in xrange ( len ( remainingGroups ) ) if i not in optimalSets [ 0 ] ] NEW_LINE DEDENT else : NEW_LINE INDENT return result + 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT with open ( ' . . / inputs / A - large . in ' ) as infile : NEW_LINE INDENT with open ( ' . . / outputs / A - large . out ' , ' wb ' ) as outfile : NEW_LINE INDENT cases = int ( infile . readline ( ) ) NEW_LINE for i in xrange ( cases ) : NEW_LINE INDENT [ N , P ] = map ( int , infile . readline ( ) . split ( ' ▁ ' ) ) NEW_LINE groups = map ( int , infile . readline ( ) . split ( ' ▁ ' ) ) NEW_LINE outfile . write ( ' Case ▁ # ' + str ( i + 1 ) + ' : ▁ ' ) NEW_LINE outfile . write ( str ( maximizeFreshChocolate ( N , P , groups ) ) ) NEW_LINE outfile . write ( ' \n ' ) NEW_LINE DEDENT DEDENT DEDENT


inputF = open ( ' A - large . in ' , ' r ' ) NEW_LINE output = open ( ' A - large . out ' , ' w ' ) NEW_LINE numCases = int ( inputF . readline ( ) ) NEW_LINE def getFreshGroups ( groups , p ) : NEW_LINE INDENT count = 0 NEW_LINE groups = [ i % p for i in groups ] NEW_LINE count += groups . count ( 0 ) NEW_LINE groups = [ g for g in groups if g != 0 ] NEW_LINE if p == 2 and len ( groups ) > 0 : NEW_LINE INDENT count += ( len ( groups ) + 1 ) / 2 NEW_LINE DEDENT elif p == 3 and len ( groups ) > 0 : NEW_LINE INDENT minMod = min ( groups . count ( 1 ) , groups . count ( 2 ) ) NEW_LINE count += minMod NEW_LINE remaining = max ( groups . count ( 1 ) , groups . count ( 2 ) ) - min ( groups . count ( 1 ) , groups . count ( 2 ) ) NEW_LINE count += ( remaining + 2 ) / 3 NEW_LINE DEDENT elif p == 4 and len ( groups ) > 0 : NEW_LINE INDENT count += ( groups . count ( 2 ) ) / 2 NEW_LINE extraTwo = ( ( groups . count ( 2 ) % 2 ) == 1 ) NEW_LINE minMod = min ( groups . count ( 1 ) , groups . count ( 3 ) ) NEW_LINE count += minMod NEW_LINE remaining = max ( groups . count ( 1 ) , groups . count ( 3 ) ) - min ( groups . count ( 1 ) , groups . count ( 3 ) ) NEW_LINE if extraTwo : NEW_LINE INDENT count += 1 + ( remaining + 1 ) / 4 NEW_LINE DEDENT else : NEW_LINE INDENT count += ( remaining + 3 ) / 4 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT for i in range ( numCases ) : NEW_LINE INDENT n , p = inputF . readline ( ) . split ( ) NEW_LINE groups = [ int ( j ) for j in inputF . readline ( ) . split ( ) ] NEW_LINE numFresh = getFreshGroups ( groups , int ( p ) ) NEW_LINE output . write ( ' Case ▁ # ' + str ( i + 1 ) + ' : ▁ ' + str ( numFresh ) + ' \n ' ) NEW_LINE DEDENT inputF . close ( ) NEW_LINE output . close ( ) NEW_LINE


from itertools import * NEW_LINE from collections import * NEW_LINE from functools import * NEW_LINE from operator import floordiv , mul , sub NEW_LINE strategies = { 2 : [ ( 1 , ( 1 , 0 ) ) , ( 1 , ( 0 , 2 ) ) ] , 3 : [ ( 1 , ( 1 , 0 , 0 ) ) , ( 1 , ( 0 , 1 , 1 ) ) , ( 1 , ( 0 , 3 , 0 ) ) , ( 1 , ( 0 , 0 , 3 ) ) ] , 4 : [ ( 1 , ( 1 , 0 , 0 , 0 ) ) , ( 1 , ( 0 , 1 , 0 , 1 ) ) , ( 1 , ( 0 , 0 , 2 , 0 ) ) , ( 1 , ( 0 , 2 , 1 , 0 ) ) , ( 1 , ( 0 , 0 , 1 , 2 ) ) , ( 1 , ( 0 , 4 , 0 , 0 ) ) , ( 1 , ( 0 , 0 , 0 , 4 ) ) , ] } NEW_LINE T = int ( input ( ) . strip ( ) ) NEW_LINE for t in range ( T ) : NEW_LINE INDENT print ( " Case ▁ # { } : ▁ " . format ( t + 1 ) , end = " " ) NEW_LINE ngroups , packsize = map ( int , input ( ) . strip ( ) . split ( " ▁ " ) ) NEW_LINE groups = map ( int , input ( ) . strip ( ) . split ( " ▁ " ) ) NEW_LINE strategy = strategies [ packsize ] NEW_LINE modgroups = list ( repeat ( 0 , packsize ) ) NEW_LINE for i in groups : NEW_LINE INDENT modgroups [ i % packsize ] += 1 NEW_LINE DEDENT score = 0 NEW_LINE for value , operation in strategy : NEW_LINE INDENT repetitions = min ( map ( lambda a , b : a // b if b != 0 else float ( ' inf ' ) , modgroups , operation ) ) NEW_LINE modgroups = list ( map ( sub , modgroups , map ( mul , operation , repeat ( repetitions ) ) ) ) NEW_LINE score += value * repetitions NEW_LINE DEDENT if sum ( modgroups ) > 0 : NEW_LINE INDENT score += 1 NEW_LINE DEDENT print ( score ) NEW_LINE DEDENT


from math import ceil , floor NEW_LINE numInputs = int ( input ( ) ) NEW_LINE for i in range ( numInputs ) : NEW_LINE INDENT N , P = [ int ( num ) for num in input ( ) . split ( " ▁ " ) ] NEW_LINE G = [ int ( num ) for num in input ( ) . split ( " ▁ " ) ] NEW_LINE numsMod = { } NEW_LINE for j in range ( P ) : NEW_LINE INDENT numsMod [ j ] = 0 NEW_LINE DEDENT for g in G : NEW_LINE INDENT numsMod [ g % P ] += 1 NEW_LINE DEDENT if P == 2 : NEW_LINE INDENT print ( " Case ▁ # " + str ( i + 1 ) + " : ▁ " + str ( numsMod [ 0 ] + ceil ( numsMod [ 1 ] / 2.0 ) ) ) NEW_LINE continue NEW_LINE DEDENT if P == 3 : NEW_LINE INDENT smaller = min ( numsMod [ 1 ] , numsMod [ 2 ] ) NEW_LINE larger = max ( numsMod [ 1 ] , numsMod [ 2 ] ) NEW_LINE diff = larger - smaller NEW_LINE print ( " Case ▁ # " + str ( i + 1 ) + " : ▁ " + str ( numsMod [ 0 ] + smaller + ceil ( diff / 3.0 ) ) ) NEW_LINE continue NEW_LINE DEDENT total = numsMod [ 0 ] NEW_LINE total += floor ( numsMod [ 2 ] / 2.0 ) NEW_LINE smaller = min ( numsMod [ 1 ] , numsMod [ 3 ] ) NEW_LINE larger = max ( numsMod [ 1 ] , numsMod [ 3 ] ) NEW_LINE diff = larger - smaller NEW_LINE total += smaller NEW_LINE if numsMod [ 2 ] % 2 == 1 : NEW_LINE INDENT total += 1 NEW_LINE if diff > 2 : NEW_LINE INDENT total += ceil ( ( diff - 2 ) / 4.0 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT total += ceil ( diff / 4.0 ) NEW_LINE DEDENT print ( " Case ▁ # " + str ( i + 1 ) + " : ▁ " + str ( total ) ) NEW_LINE DEDENT


import java . util . * ; class A { public static void main ( String [ ] arg ) { Scanner sc = new Scanner ( System . in ) ; int TT = sc . nextInt ( ) ; for ( int ii = 1 ; ii <= TT ; ++ ii ) { int N = sc . nextInt ( ) ; int P = sc . nextInt ( ) ; int [ ] M = new int [ 5 ] ; for ( int i = 0 ; i < N ; ++ i ) { int G = sc . nextInt ( ) ; M [ G % P ] ++ ; } int ans = 1 + M [ 0 ] ; if ( P == 2 ) { ans += M [ 1 ] / 2 ; if ( M [ 1 ] % 2 == 0 ) ans -- ; } else if ( P == 3 ) { int min = Math . min ( M [ 1 ] , M [ 2 ] ) ; int left = Math . max ( M [ 1 ] , M [ 2 ] ) - min ; if ( left % 3 == 0 ) ans -- ; left /= 3 ; ans += min + left ; } else if ( P == 4 ) { while ( M [ 2 ] >= 2 ) { ans ++ ; M [ 2 ] -= 2 ; } while ( M [ 1 ] > 0 && M [ 3 ] > 0 ) { ans ++ ; M [ 1 ] -- ; M [ 3 ] -- ; } if ( M [ 2 ] == 1 ) { if ( M [ 1 ] % 4 >= 2 ) { ans ++ ; M [ 1 ] -= 2 ; M [ 2 ] -- ; } else if ( M [ 3 ] % 4 >= 2 ) { ans ++ ; M [ 3 ] -= 2 ; M [ 2 ] -- ; } } int threes = M [ 3 ] / 4 ; int ones = M [ 1 ] / 4 ; ans += threes + ones ; if ( M [ 1 ] % 4 == 0 && M [ 3 ] % 4 == 0 && M [ 2 ] % 2 == 0 ) { ans -- ; } } System . out . printf ( " Case ▁ # % d : ▁ % d \n " , ii , ans ) ; } } }


import java . util . * ; public class Main { public static void main ( String args [ ] ) { ( new Main ( ) ) . solve ( ) ; } void solve ( ) { Scanner cin = new Scanner ( System . in ) ; int T = cin . nextInt ( ) ; for ( int C = 1 ; C <= T ; ++ C ) { int N = cin . nextInt ( ) ; int P = cin . nextInt ( ) ; int count [ ] = new int [ P ] ; for ( int i = 0 ; i < N ; ++ i ) { ++ count [ cin . nextInt ( ) % P ] ; } int ret = count [ 0 ] ; count [ 0 ] = 0 ; if ( P == 2 ) { ret += ( count [ 1 ] + 1 ) / 2 ; } else if ( P == 3 ) { int min = Math . min ( count [ 1 ] , count [ 2 ] ) ; ret += min ; count [ 1 ] -= min ; count [ 2 ] -= min ; ret += ( count [ 1 ] + 2 ) / 3 ; ret += ( count [ 2 ] + 2 ) / 3 ; } else if ( P == 4 ) { { int two = Math . min ( count [ 1 ] , count [ 3 ] ) ; ret += two ; count [ 1 ] -= two ; count [ 3 ] -= two ; } { int two = count [ 2 ] / 2 ; ret += two ; count [ 2 ] -= two * 2 ; } { int three = Math . min ( count [ 2 ] , Math . max ( count [ 1 ] / 2 , count [ 3 ] / 2 ) ) ; ret += three ; count [ 2 ] -= three ; if ( count [ 1 ] > 0 ) { count [ 1 ] -= three * 2 ; } if ( count [ 3 ] > 0 ) { count [ 3 ] -= three * 2 ; } } { if ( count [ 1 ] > 0 || count [ 2 ] > 0 || count [ 3 ] > 0 ) { ++ ret ; } } } System . out . println ( " Case ▁ # " + C + " : ▁ " + ret ) ; } cin . close ( ) ; } }


import java . io . File ; import java . util . Arrays ; import java . util . Scanner ; public class C { public static void main ( String [ ] args ) throws Exception { int [ ] arr = new int [ 1 << 22 ] ; long [ ] cost = new long [ 1 << 20 ] ; for ( int i = 1 ; i < ( 1 << 20 ) ; i ++ ) { cost [ i ] = cost [ i - 1 ] + i * i ; } Scanner s = new Scanner ( new File ( " C . in " ) ) ; int T = s . nextInt ( ) ; for ( int tc = 1 ; tc <= T ; tc ++ ) { int C = s . nextInt ( ) ; Arrays . fill ( arr , 0 ) ; for ( int i = 0 ; i < C ; i ++ ) arr [ s . nextInt ( ) + 2000000 ] = s . nextInt ( ) ; long ans = 0 ; boolean ok = false ; while ( ! ok ) { ok = true ; for ( int i = 0 ; i < 4000000 ; i ++ ) { if ( arr [ i ] > 1 ) { ok = false ; int j = i ; while ( arr [ j ] >= arr [ i ] ) j ++ ; j -- ; if ( ( j - i ) % 2 == 1 ) j -- ; int v = arr [ i ] ; int val = v * ( j - i + 1 ) ; int loc = ( i + j ) / 2 ; ans -= v * cost [ ( j - i + 1 ) / 2 ] ; ans += cost [ val / 2 ] ; for ( int k = i ; k <= j ; k ++ ) arr [ k ] -= v ; for ( int shift = 0 ; shift <= val / 2 ; shift ++ ) { if ( shift == 0 && val % 2 == 1 ) arr [ loc ] ++ ; if ( shift > 0 ) { arr [ loc + shift ] ++ ; arr [ loc - shift ] ++ ; } } } } } System . out . println ( " Case ▁ # " + tc + " : ▁ " + ans ) ; } } }

import sys NEW_LINE f = open ( sys . argv [ 1 ] ) NEW_LINE casenum = int ( f . readline ( ) ) NEW_LINE for casei in xrange ( casenum ) : NEW_LINE INDENT res = 0 NEW_LINE C = int ( f . readline ( ) ) NEW_LINE corners = { } NEW_LINE for z in xrange ( C ) : NEW_LINE INDENT _ = f . readline ( ) . splitlines ( ) [ 0 ] . split ( ) NEW_LINE x , n = ( int ( x ) for x in _ ) NEW_LINE corners [ x ] = n NEW_LINE DEDENT while corners . values ( ) . count ( 1 ) != len ( corners ) : NEW_LINE INDENT needmoves = [ k for k , v in corners . iteritems ( ) if v > 1 ] NEW_LINE for x in needmoves : NEW_LINE INDENT n = corners [ x ] NEW_LINE if n == 2 : NEW_LINE INDENT del corners [ x ] NEW_LINE DEDENT else : NEW_LINE INDENT corners [ x ] = n - 2 NEW_LINE DEDENT corners [ x - 1 ] = corners . get ( x - 1 , 0 ) + 1 NEW_LINE corners [ x + 1 ] = corners . get ( x + 1 , 0 ) + 1 NEW_LINE res += 1 NEW_LINE DEDENT DEDENT print ' Case ▁ # % d : ▁ % s ' % ( casei + 1 , res ) NEW_LINE DEDENT


infile = open ( " C - small . in " , " r " ) NEW_LINE T = int ( infile . readline ( ) . strip ( ) ) NEW_LINE for t in range ( T ) : NEW_LINE INDENT C = int ( infile . readline ( ) . strip ( ) ) NEW_LINE corners = { } NEW_LINE needs_moves = False NEW_LINE for c in range ( C ) : NEW_LINE INDENT line = infile . readline ( ) . strip ( ) NEW_LINE P = int ( line . split ( ' ▁ ' ) [ 0 ] ) NEW_LINE V = int ( line . split ( ' ▁ ' ) [ 1 ] ) NEW_LINE corners [ P ] = V NEW_LINE if ( V > 1 ) : NEW_LINE INDENT needs_moves = True NEW_LINE DEDENT DEDENT total_moves = 0 NEW_LINE while needs_moves : NEW_LINE INDENT newcorners = { } NEW_LINE needs_moves = False NEW_LINE for key , value in sorted ( corners . items ( ) ) : NEW_LINE INDENT if value == 1 : NEW_LINE INDENT if not newcorners . has_key ( key ) : NEW_LINE INDENT newcorners [ key ] = 0 NEW_LINE DEDENT newcorners [ key ] += 1 NEW_LINE DEDENT elif value > 1 : NEW_LINE INDENT needs_moves = True NEW_LINE moves = int ( value / 2 ) NEW_LINE total_moves += moves NEW_LINE if not newcorners . has_key ( key - 1 ) : NEW_LINE INDENT newcorners [ key - 1 ] = 0 NEW_LINE DEDENT if newcorners . has_key ( key + 1 ) : NEW_LINE INDENT assert False NEW_LINE DEDENT newcorners [ key - 1 ] += moves NEW_LINE newcorners [ key + 1 ] = moves NEW_LINE if value > moves * 2 : NEW_LINE INDENT if not newcorners . has_key ( key ) : NEW_LINE INDENT newcorners [ key ] = 0 NEW_LINE DEDENT newcorners [ key ] += 1 NEW_LINE DEDENT DEDENT DEDENT corners = newcorners NEW_LINE DEDENT print " Case ▁ # " + str ( t + 1 ) + " : ▁ " + str ( total_moves ) NEW_LINE DEDENT


for casenum in xrange ( 1 , 1 + int ( raw_input ( ) ) ) : NEW_LINE INDENT C = int ( raw_input ( ) ) NEW_LINE vs = [ [ int ( z ) for z in raw_input ( ) . split ( ) ] for j in xrange ( C ) ] NEW_LINE ve = { } NEW_LINE for v in vs : NEW_LINE INDENT ve [ v [ 0 ] ] = v [ 1 ] NEW_LINE DEDENT v = ve NEW_LINE switches = 0 NEW_LINE v1 = [ z for z in ve . keys ( ) if v [ z ] > 1 ] NEW_LINE while ( len ( v1 ) > 0 ) : NEW_LINE INDENT for loc in v1 : NEW_LINE INDENT if not ( v . has_key ( loc - 1 ) ) : NEW_LINE INDENT v [ loc - 1 ] = 0 NEW_LINE DEDENT if not ( v . has_key ( loc + 1 ) ) : NEW_LINE INDENT v [ loc + 1 ] = 0 NEW_LINE DEDENT v [ loc - 1 ] += v [ loc ] >> 1 NEW_LINE v [ loc + 1 ] += v [ loc ] >> 1 NEW_LINE switches += v [ loc ] >> 1 NEW_LINE v [ loc ] &= 1 NEW_LINE DEDENT v1 = [ z for z in ve . keys ( ) if v [ z ] > 1 ] NEW_LINE DEDENT print " Case ▁ # % d : ▁ % d " % ( casenum , switches ) NEW_LINE DEDENT


from collections import defaultdict NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT for case in xrange ( input ( ) ) : NEW_LINE INDENT cnt = defaultdict ( int ) NEW_LINE C = int ( raw_input ( ) ) NEW_LINE for _ in xrange ( C ) : NEW_LINE INDENT p , v = [ int ( s ) for s in raw_input ( ) . split ( ) ] NEW_LINE cnt [ p ] = v NEW_LINE DEDENT res = 0 NEW_LINE while True : NEW_LINE INDENT found = None NEW_LINE for p in cnt : NEW_LINE INDENT if cnt [ p ] > 1 : NEW_LINE INDENT found = p NEW_LINE break NEW_LINE DEDENT DEDENT if found is None : NEW_LINE INDENT break NEW_LINE DEDENT cnt [ p ] -= 2 NEW_LINE if cnt [ p ] == 0 : NEW_LINE INDENT del cnt [ p ] NEW_LINE DEDENT cnt [ p - 1 ] += 1 NEW_LINE cnt [ p + 1 ] += 1 NEW_LINE res += 1 NEW_LINE DEDENT print ' Case ▁ # % d : ▁ % d ' % ( case + 1 , res ) NEW_LINE DEDENT DEDENT


import sys NEW_LINE import psyco NEW_LINE psyco . full ( ) NEW_LINE def dbg ( a ) : sys . stderr . write ( str ( a ) ) NEW_LINE def readint ( ) : return int ( raw_input ( ) ) NEW_LINE def readfloat ( ) : return float ( raw_input ( ) ) NEW_LINE def readarray ( foo ) : return [ foo ( x ) for x in raw_input ( ) . split ( ) ] NEW_LINE def run_test ( test ) : NEW_LINE INDENT C = readint ( ) NEW_LINE a = dict ( [ readarray ( int ) for i in xrange ( C ) ] ) NEW_LINE done = False NEW_LINE res = 0 NEW_LINE while not done : NEW_LINE INDENT done = True NEW_LINE a2 = { } NEW_LINE for k in a . keys ( ) : NEW_LINE INDENT v = a [ k ] NEW_LINE if v == 1 : NEW_LINE INDENT a2 [ k ] = ( a2 [ k ] + 1 ) if ( k in a2 ) else 1 NEW_LINE continue NEW_LINE DEDENT done = False NEW_LINE a2 [ k - 1 ] = ( a2 [ k - 1 ] + v / 2 ) if ( k - 1 in a2 ) else v / 2 NEW_LINE a2 [ k + 1 ] = ( a2 [ k + 1 ] + v / 2 ) if ( k + 1 in a2 ) else v / 2 NEW_LINE res += v / 2 NEW_LINE if v % 2 == 1 : NEW_LINE INDENT a2 [ k ] = ( a2 [ k ] + 1 ) if ( k in a2 ) else 1 NEW_LINE DEDENT DEDENT a = a2 NEW_LINE DEDENT return res NEW_LINE DEDENT for test in range ( readint ( ) ) : NEW_LINE INDENT dbg ( " Test ▁ % d \n " % ( test + 1 ) ) NEW_LINE print " Case ▁ # % d : ▁ % d " % ( test + 1 , run_test ( test ) ) NEW_LINE DEDENT


import java . util . * ; import java . io . * ; import java . math . * ; public class Sol_slow { public void doMain ( ) throws Exception { Scanner sc = new Scanner ( new FileReader ( " input . txt " ) ) ; PrintWriter pw = new PrintWriter ( new FileWriter ( " output . txt " ) ) ; int caseCnt = sc . nextInt ( ) ; long time = System . currentTimeMillis ( ) ; for ( int caseNum = 1 ; caseNum <= caseCnt ; caseNum ++ ) { pw . print ( " Case ▁ # " + caseNum + " : ▁ " ) ; int C = sc . nextInt ( ) ; int [ ] A = new int [ C ] ; int [ ] B = new int [ C ] ; int N = 0 ; for ( int i = 0 ; i < C ; i ++ ) { A [ i ] = sc . nextInt ( ) ; B [ i ] = sc . nextInt ( ) ; N += B [ i ] ; } int [ ] x = new int [ N ] ; int pos = 0 ; for ( int i = 0 ; i < C ; i ++ ) for ( int j = 0 ; j < B [ i ] ; j ++ ) x [ pos ++ ] = A [ i ] ; int cnt = 0 ; while ( true ) { boolean find = false ; Arrays . sort ( x ) ; for ( int i = 0 ; i + 1 < N ; i ++ ) if ( x [ i ] == x [ i + 1 ] ) { x [ i ] -- ; x [ i + 1 ] ++ ; find = true ; cnt ++ ; break ; } if ( ! find ) break ; } pw . println ( cnt ) ; System . out . println ( " Finished ▁ testcase ▁ " + caseNum + " , ▁ time ▁ = ▁ " + ( System . currentTimeMillis ( ) - time ) ) ; } pw . flush ( ) ; pw . close ( ) ; sc . close ( ) ; } public static void main ( String [ ] args ) throws Exception { ( new Sol_slow ( ) ) . doMain ( ) ; } }


import java . io . File ; import java . io . FileNotFoundException ; import java . io . PrintWriter ; import java . util . Scanner ; import java . util . TreeSet ; public class C { public void solve ( ) throws FileNotFoundException { Scanner in = new Scanner ( new File ( " C - small - attempt1 . in " ) ) ; PrintWriter out = new PrintWriter ( " C - small - attempt1 . out " ) ; int testN = in . nextInt ( ) ; for ( int test = 1 ; test <= testN ; test ++ ) { out . print ( " Case ▁ # " + test + " : ▁ " ) ; int c = in . nextInt ( ) ; int [ ] a = new int [ 4000000 ] ; TreeSet < Integer > pos = new TreeSet < Integer > ( ) ; for ( int i = 0 ; i < c ; i ++ ) { int p = in . nextInt ( ) + 2000000 ; int v = in . nextInt ( ) ; a [ p ] += v ; if ( v > 1 ) { pos . add ( p ) ; } } int r = 0 ; while ( pos . size ( ) != 0 ) { int q = pos . pollFirst ( ) ; a [ q ] -= 2 ; a [ q - 1 ] ++ ; if ( a [ q - 1 ] == 2 ) { pos . add ( q - 1 ) ; } a [ q + 1 ] ++ ; if ( a [ q + 1 ] == 2 ) { pos . add ( q + 1 ) ; } if ( a [ q ] >= 2 ) { pos . add ( q ) ; } r ++ ; } out . println ( r ) ; } in . close ( ) ; out . close ( ) ; } public static void main ( String [ ] args ) throws FileNotFoundException { new C ( ) . solve ( ) ; } }


package y2010 . round3 . c ; import java . util . Scanner ; public class C { Scanner in ; public C ( ) { int CASE = 1 ; String [ ] name = { " sample " , " small " , " large " } ; System . setIn ( this . getClass ( ) . getResourceAsStream ( name [ CASE ] + " . in " ) ) ; in = new Scanner ( System . in ) ; int _t = in . nextInt ( ) ; for ( int _l = 1 ; _l <= _t ; _l ++ ) { int [ ] x = new int [ 3000001 ] ; int min = 3000001 ; int c = in . nextInt ( ) ; for ( int i = 0 ; i < c ; i ++ ) { int p = in . nextInt ( ) + 1500000 ; int v = in . nextInt ( ) ; x [ p ] = v ; min = Math . min ( min , p ) ; } int times = 0 ; while ( true ) { boolean finish = true ; for ( int i = min ; i < x . length ; i ++ ) { if ( x [ i ] > 1 ) { finish = false ; x [ i - 1 ] += 1 ; x [ i + 1 ] += 1 ; x [ i ] -= 2 ; times ++ ; if ( x [ i - 1 ] > 1 ) min = i - 1 ; break ; } } if ( finish ) break ; } String ans = " " + times ; System . out . printf ( " Case ▁ # % d : ▁ % s \n " , _l , ans ) ; } } public static void main ( String [ ] args ) { new C ( ) ; } }


import java . io . File ; import java . io . IOException ; import java . io . PrintWriter ; import java . util . Scanner ; public class Number { private static boolean run ( final char [ ] s , final int [ ] d1 , final int [ ] d , final int k ) { if ( k == s . length ) { for ( int i = 1 ; i < 10 ; ++ i ) { if ( d1 [ i ] != d [ i ] ) { return false ; } } return true ; } while ( true ) { char c = s [ k ] ; if ( c > '9' ) { s [ k ] = '0' ; return false ; } int x = c - '0' ; if ( d1 [ x ] < d [ x ] ) { ++ d1 [ x ] ; if ( run ( s , d1 , d , k + 1 ) ) { return true ; } -- d1 [ x ] ; } ++ s [ k ] ; } } public static void main ( final String ... args ) throws IOException { final String fname = " B - large " ; final Scanner sc = new Scanner ( new File ( fname + " . in " ) ) ; final PrintWriter pw = new PrintWriter ( fname + " . out " ) ; final int t = sc . nextInt ( ) ; sc . nextLine ( ) ; for ( int i = 0 ; i < t ; ++ i ) { final char [ ] s = ( '0' + sc . nextLine ( ) ) . toCharArray ( ) ; final int [ ] d = new int [ 10 ] ; for ( char c : s ) { d [ c - '0' ] ++ ; } ++ s [ s . length - 1 ] ; final int [ ] d1 = new int [ 10 ] ; if ( ! run ( s , d1 , d , 0 ) ) { throw new RuntimeException ( ) ; } final String r = String . valueOf ( s ) ; pw . println ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + ( s [ 0 ] == '0' ? r . substring ( 1 ) : r ) ) ; } pw . close ( ) ; } }

import sys NEW_LINE def m ( tab , i ) : NEW_LINE INDENT m = '9' NEW_LINE for x in tab : NEW_LINE INDENT if x > i : NEW_LINE INDENT m = min ( m , x ) NEW_LINE DEDENT DEDENT return m NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT def next_line ( ) : NEW_LINE INDENT return sys . stdin . readline ( ) NEW_LINE DEDENT n = int ( next_line ( ) ) NEW_LINE for nr in range ( 1 , n + 1 ) : NEW_LINE INDENT line = next_line ( ) NEW_LINE t = [ '0' ] + list ( line ) [ : - 1 ] NEW_LINE rev = t [ : : - 1 ] NEW_LINE prev = rev [ 0 ] NEW_LINE for pos , i in enumerate ( rev [ 1 : ] ) : NEW_LINE INDENT p = pos + 2 NEW_LINE if prev > i : NEW_LINE INDENT subtab = rev [ : p ] NEW_LINE x = m ( subtab , i ) NEW_LINE subtab . remove ( x ) NEW_LINE ret = sorted ( subtab , reverse = True ) + [ x ] + rev [ p : ] NEW_LINE break NEW_LINE DEDENT prev = i NEW_LINE DEDENT ret = " " . join ( ret [ : : - 1 ] ) NEW_LINE print " Case ▁ # % d : ▁ % d " % ( nr , int ( ret ) ) NEW_LINE DEDENT DEDENT main ( ) NEW_LINE


from skynet . math . numbers import digits NEW_LINE def dijkstra_next_perm ( iterable ) : NEW_LINE INDENT iterable = list ( iterable ) NEW_LINE N = len ( iterable ) NEW_LINE i = len ( iterable ) - 1 NEW_LINE while iterable [ i - 1 ] >= iterable [ i ] : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT j = N NEW_LINE while iterable [ j - 1 ] <= iterable [ i - 1 ] : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT iterable [ i - 1 ] , iterable [ j - 1 ] = iterable [ j - 1 ] , iterable [ i - 1 ] NEW_LINE i += 1 NEW_LINE j = N NEW_LINE while i < j : NEW_LINE INDENT iterable [ i - 1 ] , iterable [ j - 1 ] = iterable [ j - 1 ] , iterable [ i - 1 ] NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return iterable NEW_LINE DEDENT def next_number ( n ) : NEW_LINE INDENT p = str ( n ) NEW_LINE try : NEW_LINE INDENT m = int ( ' ' . join ( dijkstra_next_perm ( p ) ) ) NEW_LINE DEDENT except IndexError : NEW_LINE INDENT return int ( ' ' . join ( dijkstra_next_perm ( p + '0' ) ) ) NEW_LINE DEDENT if m <= n : NEW_LINE INDENT return int ( ' ' . join ( dijkstra_next_perm ( p + '0' ) ) ) NEW_LINE DEDENT return m NEW_LINE DEDENT with open ( ' B - large . in ' ) as file : NEW_LINE INDENT t = int ( file . readline ( ) ) NEW_LINE for i in range ( t ) : NEW_LINE INDENT n = int ( file . readline ( ) ) NEW_LINE print ( " Case ▁ # { } : ▁ { } " . format ( i + 1 , next_number ( n ) ) ) NEW_LINE DEDENT DEDENT