id
stringlengths 2
6
| java
stringlengths 48
5.92k
| python
stringlengths 33
11.1k
|
---|---|---|
T0 | import java . io . * ; import java . util . * ; import static java . lang . System . in ; class Main { static long mod = 1000000000 + 7 ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = sc . nextInt ( ) ; long ans = help ( a ) ; System . out . println ( ans ) ; } static long help ( int [ ] a ) { if ( a [ 0 ] != 0 ) return 0 ; int maxDist = 0 ; for ( int w : a ) maxDist = Math . max ( maxDist , w ) ; int [ ] rec = new int [ maxDist + 1 ] ; for ( int w : a ) rec [ w ] ++ ; if ( rec [ 0 ] != 1 ) return 0 ; for ( int i = 1 ; i <= maxDist ; i ++ ) { if ( rec [ i ] < 1 ) return 0 ; } long [ ] dp = new long [ maxDist + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= maxDist ; i ++ ) { long base = power ( 2 , rec [ i - 1 ] ) - 1 ; dp [ i ] = power ( base , rec [ i ] ) * dp [ i - 1 ] % mod ; long sameLayer = ( ( long ) rec [ i ] ) * ( ( long ) rec [ i ] - 1 ) / 2 ; dp [ i ] = dp [ i ] * power ( 2 , sameLayer ) % mod ; } return dp [ maxDist ] ; } static long power ( long base , long p ) { long ans = 1 ; while ( p > 0 ) { if ( p % 2 == 1 ) ans = ans * base % mod ; base = base * base % mod ; p /= 2 ; } return ans ; } }
| def main ( ) : NEW_LINE INDENT R = 10 ** 9 + 7 NEW_LINE N = int ( input ( ) ) NEW_LINE a = list ( map ( int , input ( ) . split ( " ▁ " ) ) ) NEW_LINE if a [ 0 ] != 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT amax = max ( a ) NEW_LINE h = [ 0 ] * ( amax + 1 ) NEW_LINE for i in a : NEW_LINE INDENT h [ i ] += 1 NEW_LINE DEDENT if h [ 0 ] != 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in h : NEW_LINE INDENT if i == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT ans = 1 NEW_LINE b = 1 NEW_LINE for i in h [ 1 : ] : NEW_LINE INDENT if i > 1 : NEW_LINE INDENT ans *= pow ( 2 , i * ( i - 1 ) // 2 , R ) NEW_LINE ans %= R NEW_LINE DEDENT ans *= pow ( pow ( 2 , b , R ) - 1 , i , R ) NEW_LINE ans %= R NEW_LINE b = i NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( main ( ) ) NEW_LINE DEDENT
|
T1 | import java . io . * ; import java . util . * ; import static java . lang . System . in ; class Main { static long mod = 1000000000 + 7 ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = sc . nextInt ( ) ; long ans = help ( a ) ; System . out . println ( ans ) ; } static long help ( int [ ] a ) { if ( a [ 0 ] != 0 ) return 0 ; int maxDist = 0 ; for ( int w : a ) maxDist = Math . max ( maxDist , w ) ; int [ ] rec = new int [ maxDist + 1 ] ; for ( int w : a ) rec [ w ] ++ ; if ( rec [ 0 ] != 1 ) return 0 ; for ( int i = 1 ; i <= maxDist ; i ++ ) { if ( rec [ i ] < 1 ) return 0 ; } long [ ] dp = new long [ maxDist + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= maxDist ; i ++ ) { long base = power ( 2 , rec [ i - 1 ] ) - 1 ; dp [ i ] = power ( base , rec [ i ] ) * dp [ i - 1 ] % mod ; long sameLayer = ( ( long ) rec [ i ] ) * ( ( long ) rec [ i ] - 1 ) / 2 ; dp [ i ] = dp [ i ] * power ( 2 , sameLayer ) % mod ; } return dp [ maxDist ] ; } static long power ( long base , long p ) { long ans = 1 ; while ( p > 0 ) { if ( p % 2 == 1 ) ans = ans * base % mod ; base = base * base % mod ; p /= 2 ; } return ans ; } }
| def powmod ( a , b , P ) : NEW_LINE INDENT if b == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT stk = [ ] NEW_LINE while b > 1 : NEW_LINE INDENT stk . append ( b % 2 ) NEW_LINE b //= 2 NEW_LINE DEDENT c = a % P NEW_LINE while len ( stk ) > 0 : NEW_LINE INDENT t = stk . pop ( ) NEW_LINE c = ( c * c ) % P NEW_LINE if t == 1 : NEW_LINE INDENT c = ( c * a ) % P NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT N = int ( input ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE P = 10 ** 9 + 7 NEW_LINE def count ( A ) : NEW_LINE INDENT S = [ 0 for _ in range ( max ( A ) + 1 ) ] NEW_LINE for a in A : NEW_LINE INDENT S [ a ] += 1 NEW_LINE DEDENT if A [ 0 ] != 0 or S [ 0 ] != 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT cnt = 1 NEW_LINE for i in range ( 1 , len ( S ) ) : NEW_LINE INDENT if S [ i ] == 0 : return 0 NEW_LINE path1 = powmod ( ( powmod ( 2 , S [ i - 1 ] , P ) - 1 ) % P , S [ i ] , P ) NEW_LINE path2 = powmod ( 2 , S [ i ] * ( S [ i ] - 1 ) // 2 , P ) % P NEW_LINE cnt = cnt * path1 * path2 % P NEW_LINE DEDENT return cnt NEW_LINE DEDENT print ( count ( A ) ) NEW_LINE
|
T2 | import java . io . * ; import java . util . * ; import static java . lang . System . in ; class Main { static long mod = 1000000000 + 7 ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = sc . nextInt ( ) ; long ans = help ( a ) ; System . out . println ( ans ) ; } static long help ( int [ ] a ) { if ( a [ 0 ] != 0 ) return 0 ; int maxDist = 0 ; for ( int w : a ) maxDist = Math . max ( maxDist , w ) ; int [ ] rec = new int [ maxDist + 1 ] ; for ( int w : a ) rec [ w ] ++ ; if ( rec [ 0 ] != 1 ) return 0 ; for ( int i = 1 ; i <= maxDist ; i ++ ) { if ( rec [ i ] < 1 ) return 0 ; } long [ ] dp = new long [ maxDist + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= maxDist ; i ++ ) { long base = power ( 2 , rec [ i - 1 ] ) - 1 ; dp [ i ] = power ( base , rec [ i ] ) * dp [ i - 1 ] % mod ; long sameLayer = ( ( long ) rec [ i ] ) * ( ( long ) rec [ i ] - 1 ) / 2 ; dp [ i ] = dp [ i ] * power ( 2 , sameLayer ) % mod ; } return dp [ maxDist ] ; } static long power ( long base , long p ) { long ans = 1 ; while ( p > 0 ) { if ( p % 2 == 1 ) ans = ans * base % mod ; base = base * base % mod ; p /= 2 ; } return ans ; } }
| from collections import defaultdict NEW_LINE def mod_pow ( x , n , mod ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT res = mod_pow ( x * x % mod , n // 2 , mod ) NEW_LINE if n & 1 : NEW_LINE INDENT res = res * x % mod NEW_LINE DEDENT return res NEW_LINE DEDENT MOD = 10 ** 9 + 7 NEW_LINE N = int ( input ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE ok = True NEW_LINE zeros = 0 NEW_LINE cnt = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if i == 0 : NEW_LINE INDENT if A [ i ] != 0 : NEW_LINE INDENT ok = False NEW_LINE DEDENT DEDENT if A [ i ] == 0 : NEW_LINE INDENT zeros += 1 NEW_LINE DEDENT cnt [ A [ i ] ] += 1 NEW_LINE DEDENT maxn = max ( cnt . keys ( ) ) NEW_LINE if zeros != 1 : NEW_LINE INDENT ok = False NEW_LINE DEDENT res = 1 NEW_LINE if ok == True : NEW_LINE INDENT for i in range ( 0 , maxn + 1 ) : NEW_LINE INDENT if i != 0 and cnt [ i ] == 0 : NEW_LINE INDENT ok = False ; NEW_LINE break NEW_LINE DEDENT K = cnt [ i ] NEW_LINE res = ( res * mod_pow ( 2 , K * ( K - 1 ) // 2 , MOD ) ) % MOD NEW_LINE if i != 0 : NEW_LINE INDENT t = cnt [ i - 1 ] NEW_LINE curr = mod_pow ( 2 , t , MOD ) NEW_LINE s = cnt [ i ] NEW_LINE curr2 = mod_pow ( curr - 1 + MOD , s , MOD ) NEW_LINE res = ( res * curr2 ) % MOD NEW_LINE DEDENT DEDENT DEDENT print ( res if ok == True else 0 ) NEW_LINE
|
T3 | import java . io . * ; import java . util . * ; import static java . lang . System . in ; class Main { static long mod = 1000000000 + 7 ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = sc . nextInt ( ) ; long ans = help ( a ) ; System . out . println ( ans ) ; } static long help ( int [ ] a ) { if ( a [ 0 ] != 0 ) return 0 ; int maxDist = 0 ; for ( int w : a ) maxDist = Math . max ( maxDist , w ) ; int [ ] rec = new int [ maxDist + 1 ] ; for ( int w : a ) rec [ w ] ++ ; if ( rec [ 0 ] != 1 ) return 0 ; for ( int i = 1 ; i <= maxDist ; i ++ ) { if ( rec [ i ] < 1 ) return 0 ; } long [ ] dp = new long [ maxDist + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= maxDist ; i ++ ) { long base = power ( 2 , rec [ i - 1 ] ) - 1 ; dp [ i ] = power ( base , rec [ i ] ) * dp [ i - 1 ] % mod ; long sameLayer = ( ( long ) rec [ i ] ) * ( ( long ) rec [ i ] - 1 ) / 2 ; dp [ i ] = dp [ i ] * power ( 2 , sameLayer ) % mod ; } return dp [ maxDist ] ; } static long power ( long base , long p ) { long ans = 1 ; while ( p > 0 ) { if ( p % 2 == 1 ) ans = ans * base % mod ; base = base * base % mod ; p /= 2 ; } return ans ; } }
| import collections NEW_LINE MAX_N = 10 ** 5 + 1 NEW_LINE MOD = 1000000007 NEW_LINE same_dist = [ 0 ] * ( MAX_N + 1 ) NEW_LINE same_dist [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAX_N + 1 ) : NEW_LINE INDENT same_dist [ i ] = same_dist [ i - 1 ] * pow ( 2 , i - 1 , MOD ) NEW_LINE same_dist [ i ] %= MOD NEW_LINE DEDENT n = int ( input ( ) ) NEW_LINE a = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE cnt_num = collections . Counter ( a ) NEW_LINE if not ( cnt_num [ 0 ] == 1 and a [ 0 ] == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE exit ( ) NEW_LINE DEDENT if len ( a ) == 1 : NEW_LINE INDENT print ( 1 ) NEW_LINE exit ( ) NEW_LINE DEDENT ans = 1 NEW_LINE cnt = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if cnt_num [ i ] == 0 : NEW_LINE INDENT print ( 0 ) NEW_LINE exit ( ) NEW_LINE DEDENT else : NEW_LINE INDENT ans *= same_dist [ cnt_num [ i ] ] NEW_LINE ans %= MOD NEW_LINE ans *= pow ( ( 2 ** cnt_num [ i - 1 ] - 1 ) , cnt_num [ i ] , MOD ) NEW_LINE ans %= MOD NEW_LINE DEDENT cnt += cnt_num [ i ] NEW_LINE if cnt == len ( a ) : NEW_LINE INDENT print ( ans ) NEW_LINE exit ( ) NEW_LINE DEDENT DEDENT
|
T4 | import java . io . * ; import java . util . * ; import static java . lang . System . in ; class Main { static long mod = 1000000000 + 7 ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = sc . nextInt ( ) ; long ans = help ( a ) ; System . out . println ( ans ) ; } static long help ( int [ ] a ) { if ( a [ 0 ] != 0 ) return 0 ; int maxDist = 0 ; for ( int w : a ) maxDist = Math . max ( maxDist , w ) ; int [ ] rec = new int [ maxDist + 1 ] ; for ( int w : a ) rec [ w ] ++ ; if ( rec [ 0 ] != 1 ) return 0 ; for ( int i = 1 ; i <= maxDist ; i ++ ) { if ( rec [ i ] < 1 ) return 0 ; } long [ ] dp = new long [ maxDist + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= maxDist ; i ++ ) { long base = power ( 2 , rec [ i - 1 ] ) - 1 ; dp [ i ] = power ( base , rec [ i ] ) * dp [ i - 1 ] % mod ; long sameLayer = ( ( long ) rec [ i ] ) * ( ( long ) rec [ i ] - 1 ) / 2 ; dp [ i ] = dp [ i ] * power ( 2 , sameLayer ) % mod ; } return dp [ maxDist ] ; } static long power ( long base , long p ) { long ans = 1 ; while ( p > 0 ) { if ( p % 2 == 1 ) ans = ans * base % mod ; base = base * base % mod ; p /= 2 ; } return ans ; } }
| from collections import Counter NEW_LINE n = int ( input ( ) ) NEW_LINE a = [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE def power ( n , k ) : NEW_LINE INDENT if k == 0 : return 1 NEW_LINE elif k == 1 : return n NEW_LINE elif k % 2 == 0 : return power ( ( n ** 2 ) % mod , k // 2 ) NEW_LINE else : return ( n * power ( n , k - 1 ) ) % mod NEW_LINE DEDENT if a [ 0 ] != 0 or a . count ( 0 ) != 1 : print ( 0 ) NEW_LINE else : NEW_LINE INDENT c , ans , num , mod = Counter ( a ) , 1 , 1 , 10 ** 9 + 7 NEW_LINE for i in range ( 1 , max ( a ) + 1 ) : NEW_LINE INDENT if c [ i ] == 0 : NEW_LINE INDENT ans = 0 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT ans = ( ans * power ( ( power ( 2 , num ) - 1 ) , c [ i ] ) * power ( 2 , c [ i ] * ( c [ i ] - 1 ) // 2 ) ) % mod NEW_LINE num = c [ i ] NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT
|
T5 | import java . util . * ; public class Main { static final int MOD = 1_000_000_007 ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; int [ ] d = new int [ n ] ; int dmax = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; d [ a [ i ] ] ++ ; dmax = Math . max ( a [ i ] , dmax ) ; } if ( a [ 0 ] != 0 || d [ 0 ] != 1 ) { System . out . println ( 0 ) ; return ; } for ( int i = 0 ; i <= dmax ; i ++ ) { if ( d [ i ] == 0 ) { System . out . println ( 0 ) ; return ; } } long ans = 1L ; for ( int i = 1 ; i <= dmax ; i ++ ) { long tmp = pow ( 2 , d [ i - 1 ] ) ; tmp = ( tmp + MOD - 1 ) % MOD ; ans = ( ans * pow ( tmp , d [ i ] ) ) % MOD ; ans = ( ans * pow ( 2 , ( long ) ( d [ i ] - 1 ) * d [ i ] / 2 ) ) % MOD ; } System . out . println ( ans ) ; } public static long pow ( long a , long n ) { long x = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) { x = x * a % MOD ; } a = a * a % MOD ; n >>= 1 ; } return x ; } }
| def main ( ) : NEW_LINE INDENT R = 10 ** 9 + 7 NEW_LINE N = int ( input ( ) ) NEW_LINE a = list ( map ( int , input ( ) . split ( " ▁ " ) ) ) NEW_LINE if a [ 0 ] != 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT amax = max ( a ) NEW_LINE h = [ 0 ] * ( amax + 1 ) NEW_LINE for i in a : NEW_LINE INDENT h [ i ] += 1 NEW_LINE DEDENT if h [ 0 ] != 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in h : NEW_LINE INDENT if i == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT ans = 1 NEW_LINE b = 1 NEW_LINE for i in h [ 1 : ] : NEW_LINE INDENT if i > 1 : NEW_LINE INDENT ans *= pow ( 2 , i * ( i - 1 ) // 2 , R ) NEW_LINE ans %= R NEW_LINE DEDENT ans *= pow ( pow ( 2 , b , R ) - 1 , i , R ) NEW_LINE ans %= R NEW_LINE b = i NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( main ( ) ) NEW_LINE DEDENT
|
T6 | import java . util . * ; public class Main { static final int MOD = 1_000_000_007 ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; int [ ] d = new int [ n ] ; int dmax = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; d [ a [ i ] ] ++ ; dmax = Math . max ( a [ i ] , dmax ) ; } if ( a [ 0 ] != 0 || d [ 0 ] != 1 ) { System . out . println ( 0 ) ; return ; } for ( int i = 0 ; i <= dmax ; i ++ ) { if ( d [ i ] == 0 ) { System . out . println ( 0 ) ; return ; } } long ans = 1L ; for ( int i = 1 ; i <= dmax ; i ++ ) { long tmp = pow ( 2 , d [ i - 1 ] ) ; tmp = ( tmp + MOD - 1 ) % MOD ; ans = ( ans * pow ( tmp , d [ i ] ) ) % MOD ; ans = ( ans * pow ( 2 , ( long ) ( d [ i ] - 1 ) * d [ i ] / 2 ) ) % MOD ; } System . out . println ( ans ) ; } public static long pow ( long a , long n ) { long x = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) { x = x * a % MOD ; } a = a * a % MOD ; n >>= 1 ; } return x ; } }
| def powmod ( a , b , P ) : NEW_LINE INDENT if b == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT stk = [ ] NEW_LINE while b > 1 : NEW_LINE INDENT stk . append ( b % 2 ) NEW_LINE b //= 2 NEW_LINE DEDENT c = a % P NEW_LINE while len ( stk ) > 0 : NEW_LINE INDENT t = stk . pop ( ) NEW_LINE c = ( c * c ) % P NEW_LINE if t == 1 : NEW_LINE INDENT c = ( c * a ) % P NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT N = int ( input ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE P = 10 ** 9 + 7 NEW_LINE def count ( A ) : NEW_LINE INDENT S = [ 0 for _ in range ( max ( A ) + 1 ) ] NEW_LINE for a in A : NEW_LINE INDENT S [ a ] += 1 NEW_LINE DEDENT if A [ 0 ] != 0 or S [ 0 ] != 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT cnt = 1 NEW_LINE for i in range ( 1 , len ( S ) ) : NEW_LINE INDENT if S [ i ] == 0 : return 0 NEW_LINE path1 = powmod ( ( powmod ( 2 , S [ i - 1 ] , P ) - 1 ) % P , S [ i ] , P ) NEW_LINE path2 = powmod ( 2 , S [ i ] * ( S [ i ] - 1 ) // 2 , P ) % P NEW_LINE cnt = cnt * path1 * path2 % P NEW_LINE DEDENT return cnt NEW_LINE DEDENT print ( count ( A ) ) NEW_LINE
|
T7 | import java . util . * ; public class Main { static final int MOD = 1_000_000_007 ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; int [ ] d = new int [ n ] ; int dmax = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; d [ a [ i ] ] ++ ; dmax = Math . max ( a [ i ] , dmax ) ; } if ( a [ 0 ] != 0 || d [ 0 ] != 1 ) { System . out . println ( 0 ) ; return ; } for ( int i = 0 ; i <= dmax ; i ++ ) { if ( d [ i ] == 0 ) { System . out . println ( 0 ) ; return ; } } long ans = 1L ; for ( int i = 1 ; i <= dmax ; i ++ ) { long tmp = pow ( 2 , d [ i - 1 ] ) ; tmp = ( tmp + MOD - 1 ) % MOD ; ans = ( ans * pow ( tmp , d [ i ] ) ) % MOD ; ans = ( ans * pow ( 2 , ( long ) ( d [ i ] - 1 ) * d [ i ] / 2 ) ) % MOD ; } System . out . println ( ans ) ; } public static long pow ( long a , long n ) { long x = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) { x = x * a % MOD ; } a = a * a % MOD ; n >>= 1 ; } return x ; } }
| from collections import defaultdict NEW_LINE def mod_pow ( x , n , mod ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT res = mod_pow ( x * x % mod , n // 2 , mod ) NEW_LINE if n & 1 : NEW_LINE INDENT res = res * x % mod NEW_LINE DEDENT return res NEW_LINE DEDENT MOD = 10 ** 9 + 7 NEW_LINE N = int ( input ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE ok = True NEW_LINE zeros = 0 NEW_LINE cnt = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if i == 0 : NEW_LINE INDENT if A [ i ] != 0 : NEW_LINE INDENT ok = False NEW_LINE DEDENT DEDENT if A [ i ] == 0 : NEW_LINE INDENT zeros += 1 NEW_LINE DEDENT cnt [ A [ i ] ] += 1 NEW_LINE DEDENT maxn = max ( cnt . keys ( ) ) NEW_LINE if zeros != 1 : NEW_LINE INDENT ok = False NEW_LINE DEDENT res = 1 NEW_LINE if ok == True : NEW_LINE INDENT for i in range ( 0 , maxn + 1 ) : NEW_LINE INDENT if i != 0 and cnt [ i ] == 0 : NEW_LINE INDENT ok = False ; NEW_LINE break NEW_LINE DEDENT K = cnt [ i ] NEW_LINE res = ( res * mod_pow ( 2 , K * ( K - 1 ) // 2 , MOD ) ) % MOD NEW_LINE if i != 0 : NEW_LINE INDENT t = cnt [ i - 1 ] NEW_LINE curr = mod_pow ( 2 , t , MOD ) NEW_LINE s = cnt [ i ] NEW_LINE curr2 = mod_pow ( curr - 1 + MOD , s , MOD ) NEW_LINE res = ( res * curr2 ) % MOD NEW_LINE DEDENT DEDENT DEDENT print ( res if ok == True else 0 ) NEW_LINE
|
T8 | import java . util . * ; public class Main { static final int MOD = 1_000_000_007 ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; int [ ] d = new int [ n ] ; int dmax = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; d [ a [ i ] ] ++ ; dmax = Math . max ( a [ i ] , dmax ) ; } if ( a [ 0 ] != 0 || d [ 0 ] != 1 ) { System . out . println ( 0 ) ; return ; } for ( int i = 0 ; i <= dmax ; i ++ ) { if ( d [ i ] == 0 ) { System . out . println ( 0 ) ; return ; } } long ans = 1L ; for ( int i = 1 ; i <= dmax ; i ++ ) { long tmp = pow ( 2 , d [ i - 1 ] ) ; tmp = ( tmp + MOD - 1 ) % MOD ; ans = ( ans * pow ( tmp , d [ i ] ) ) % MOD ; ans = ( ans * pow ( 2 , ( long ) ( d [ i ] - 1 ) * d [ i ] / 2 ) ) % MOD ; } System . out . println ( ans ) ; } public static long pow ( long a , long n ) { long x = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) { x = x * a % MOD ; } a = a * a % MOD ; n >>= 1 ; } return x ; } }
| import collections NEW_LINE MAX_N = 10 ** 5 + 1 NEW_LINE MOD = 1000000007 NEW_LINE same_dist = [ 0 ] * ( MAX_N + 1 ) NEW_LINE same_dist [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAX_N + 1 ) : NEW_LINE INDENT same_dist [ i ] = same_dist [ i - 1 ] * pow ( 2 , i - 1 , MOD ) NEW_LINE same_dist [ i ] %= MOD NEW_LINE DEDENT n = int ( input ( ) ) NEW_LINE a = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE cnt_num = collections . Counter ( a ) NEW_LINE if not ( cnt_num [ 0 ] == 1 and a [ 0 ] == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE exit ( ) NEW_LINE DEDENT if len ( a ) == 1 : NEW_LINE INDENT print ( 1 ) NEW_LINE exit ( ) NEW_LINE DEDENT ans = 1 NEW_LINE cnt = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if cnt_num [ i ] == 0 : NEW_LINE INDENT print ( 0 ) NEW_LINE exit ( ) NEW_LINE DEDENT else : NEW_LINE INDENT ans *= same_dist [ cnt_num [ i ] ] NEW_LINE ans %= MOD NEW_LINE ans *= pow ( ( 2 ** cnt_num [ i - 1 ] - 1 ) , cnt_num [ i ] , MOD ) NEW_LINE ans %= MOD NEW_LINE DEDENT cnt += cnt_num [ i ] NEW_LINE if cnt == len ( a ) : NEW_LINE INDENT print ( ans ) NEW_LINE exit ( ) NEW_LINE DEDENT DEDENT
|
T9 | import java . util . * ; public class Main { static final int MOD = 1_000_000_007 ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; int [ ] d = new int [ n ] ; int dmax = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; d [ a [ i ] ] ++ ; dmax = Math . max ( a [ i ] , dmax ) ; } if ( a [ 0 ] != 0 || d [ 0 ] != 1 ) { System . out . println ( 0 ) ; return ; } for ( int i = 0 ; i <= dmax ; i ++ ) { if ( d [ i ] == 0 ) { System . out . println ( 0 ) ; return ; } } long ans = 1L ; for ( int i = 1 ; i <= dmax ; i ++ ) { long tmp = pow ( 2 , d [ i - 1 ] ) ; tmp = ( tmp + MOD - 1 ) % MOD ; ans = ( ans * pow ( tmp , d [ i ] ) ) % MOD ; ans = ( ans * pow ( 2 , ( long ) ( d [ i ] - 1 ) * d [ i ] / 2 ) ) % MOD ; } System . out . println ( ans ) ; } public static long pow ( long a , long n ) { long x = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) { x = x * a % MOD ; } a = a * a % MOD ; n >>= 1 ; } return x ; } }
| from collections import Counter NEW_LINE n = int ( input ( ) ) NEW_LINE a = [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE def power ( n , k ) : NEW_LINE INDENT if k == 0 : return 1 NEW_LINE elif k == 1 : return n NEW_LINE elif k % 2 == 0 : return power ( ( n ** 2 ) % mod , k // 2 ) NEW_LINE else : return ( n * power ( n , k - 1 ) ) % mod NEW_LINE DEDENT if a [ 0 ] != 0 or a . count ( 0 ) != 1 : print ( 0 ) NEW_LINE else : NEW_LINE INDENT c , ans , num , mod = Counter ( a ) , 1 , 1 , 10 ** 9 + 7 NEW_LINE for i in range ( 1 , max ( a ) + 1 ) : NEW_LINE INDENT if c [ i ] == 0 : NEW_LINE INDENT ans = 0 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT ans = ( ans * power ( ( power ( 2 , num ) - 1 ) , c [ i ] ) * power ( 2 , c [ i ] * ( c [ i ] - 1 ) // 2 ) ) % mod NEW_LINE num = c [ i ] NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT
|
T10 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; UnionFind uf = new UnionFind ( N ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) - 1 ; int b = sc . nextInt ( ) - 1 ; uf . union ( a , b ) ; } System . out . println ( uf . count ( N ) - 1 ) ; } private static class UnionFind { int parent [ ] ; int rank [ ] ; public UnionFind ( int n ) { parent = new int [ n ] ; rank = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i ; rank [ i ] = 0 ; } } boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } public int find ( int x ) { if ( parent [ x ] == x ) return x ; else return parent [ x ] = find ( parent [ x ] ) ; } public void union ( int x , int y ) { x = find ( x ) ; y = find ( y ) ; if ( x != y ) { if ( rank [ x ] > rank [ y ] ) { parent [ y ] = x ; } else { parent [ x ] = y ; if ( rank [ x ] == rank [ y ] ) { rank [ y ] ++ ; } } } return ; } public int count ( int n ) { int ret = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == find ( i ) ) ret ++ ; } return ret ; } } }
| import sys NEW_LINE input = sys . stdin . readline NEW_LINE n , m = map ( int , input ( ) . split ( ) ) NEW_LINE par = [ ] NEW_LINE rank = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT par . append ( i ) NEW_LINE rank . append ( 0 ) NEW_LINE DEDENT def find ( x , par ) : NEW_LINE INDENT if par [ x ] == x : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT return find ( par [ x ] , par ) NEW_LINE DEDENT DEDENT def unite ( x , y , par , rank ) : NEW_LINE INDENT x = find ( x , par ) NEW_LINE y = find ( y , par ) NEW_LINE if x != y : NEW_LINE INDENT if rank [ x ] < rank [ y ] : NEW_LINE INDENT par [ x ] = y NEW_LINE DEDENT else : NEW_LINE INDENT par [ y ] = x NEW_LINE if rank [ x ] == rank [ y ] : NEW_LINE INDENT rank [ x ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def same ( x , y , par ) : NEW_LINE INDENT return find ( x , par ) == find ( y , par ) NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT a , b = map ( lambda x : int ( x ) - 1 , input ( ) . split ( ) ) NEW_LINE unite ( a , b , par , rank ) NEW_LINE DEDENT res = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if par [ i ] == i : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE
|
T11 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; UnionFind uf = new UnionFind ( N ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) - 1 ; int b = sc . nextInt ( ) - 1 ; uf . union ( a , b ) ; } System . out . println ( uf . count ( N ) - 1 ) ; } private static class UnionFind { int parent [ ] ; int rank [ ] ; public UnionFind ( int n ) { parent = new int [ n ] ; rank = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i ; rank [ i ] = 0 ; } } boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } public int find ( int x ) { if ( parent [ x ] == x ) return x ; else return parent [ x ] = find ( parent [ x ] ) ; } public void union ( int x , int y ) { x = find ( x ) ; y = find ( y ) ; if ( x != y ) { if ( rank [ x ] > rank [ y ] ) { parent [ y ] = x ; } else { parent [ x ] = y ; if ( rank [ x ] == rank [ y ] ) { rank [ y ] ++ ; } } } return ; } public int count ( int n ) { int ret = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == find ( i ) ) ret ++ ; } return ret ; } } }
| class UnionFind ( ) : NEW_LINE INDENT def __init__ ( self , size ) : NEW_LINE INDENT self . table = [ - 1 ] * size NEW_LINE DEDENT def root ( self , x ) : NEW_LINE INDENT while self . table [ x ] >= 0 : NEW_LINE INDENT x = self . table [ x ] NEW_LINE DEDENT return x NEW_LINE DEDENT def unite ( self , x , y ) : NEW_LINE INDENT s1 = self . root ( x ) NEW_LINE s2 = self . root ( y ) NEW_LINE if s1 != s2 : NEW_LINE INDENT if self . table [ s1 ] > self . table [ s2 ] : NEW_LINE INDENT s1 , s2 = s2 , s1 NEW_LINE DEDENT self . table [ s1 ] += self . table [ s2 ] NEW_LINE self . table [ s2 ] = s1 NEW_LINE DEDENT return NEW_LINE DEDENT def same ( self , x , y ) : NEW_LINE INDENT return self . root ( x ) == self . root ( y ) NEW_LINE DEDENT def size ( self , x ) : NEW_LINE INDENT return - self . table [ self . root ( x ) ] NEW_LINE DEDENT DEDENT ( n , m ) , * e = [ map ( int , t . split ( ) ) for t in open ( 0 ) . readlines ( ) ] NEW_LINE u = UnionFind ( n ) NEW_LINE for x , y in e : NEW_LINE INDENT u . unite ( x - 1 , y - 1 ) NEW_LINE DEDENT print ( sum ( i < 0 for i in u . table ) - 1 ) NEW_LINE
|
T12 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; UnionFind uf = new UnionFind ( N ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) - 1 ; int b = sc . nextInt ( ) - 1 ; uf . union ( a , b ) ; } System . out . println ( uf . count ( N ) - 1 ) ; } private static class UnionFind { int parent [ ] ; int rank [ ] ; public UnionFind ( int n ) { parent = new int [ n ] ; rank = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i ; rank [ i ] = 0 ; } } boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } public int find ( int x ) { if ( parent [ x ] == x ) return x ; else return parent [ x ] = find ( parent [ x ] ) ; } public void union ( int x , int y ) { x = find ( x ) ; y = find ( y ) ; if ( x != y ) { if ( rank [ x ] > rank [ y ] ) { parent [ y ] = x ; } else { parent [ x ] = y ; if ( rank [ x ] == rank [ y ] ) { rank [ y ] ++ ; } } } return ; } public int count ( int n ) { int ret = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == find ( i ) ) ret ++ ; } return ret ; } } }
| from collections import defaultdict NEW_LINE import sys NEW_LINE sys . setrecursionlimit ( sys . getrecursionlimit ( ) * 10000 ) NEW_LINE N , M = map ( int , input ( ) . split ( ) ) NEW_LINE g = defaultdict ( list ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE g [ a ] . append ( b ) NEW_LINE g [ b ] . append ( a ) NEW_LINE DEDENT visited = set ( ) NEW_LINE def dfs ( v ) : NEW_LINE INDENT visited . add ( v ) NEW_LINE for c in g [ v ] : NEW_LINE INDENT if not c in visited : NEW_LINE INDENT dfs ( c ) NEW_LINE DEDENT DEDENT DEDENT connected_component = 0 NEW_LINE for v in range ( 1 , N + 1 ) : NEW_LINE INDENT if not v in visited : NEW_LINE INDENT dfs ( v ) NEW_LINE connected_component += 1 NEW_LINE DEDENT DEDENT print ( connected_component - 1 ) NEW_LINE
|
T13 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; UnionFind uf = new UnionFind ( N ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) - 1 ; int b = sc . nextInt ( ) - 1 ; uf . union ( a , b ) ; } System . out . println ( uf . count ( N ) - 1 ) ; } private static class UnionFind { int parent [ ] ; int rank [ ] ; public UnionFind ( int n ) { parent = new int [ n ] ; rank = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i ; rank [ i ] = 0 ; } } boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } public int find ( int x ) { if ( parent [ x ] == x ) return x ; else return parent [ x ] = find ( parent [ x ] ) ; } public void union ( int x , int y ) { x = find ( x ) ; y = find ( y ) ; if ( x != y ) { if ( rank [ x ] > rank [ y ] ) { parent [ y ] = x ; } else { parent [ x ] = y ; if ( rank [ x ] == rank [ y ] ) { rank [ y ] ++ ; } } } return ; } public int count ( int n ) { int ret = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == find ( i ) ) ret ++ ; } return ret ; } } }
| def b_construction ( N , M , Road ) : NEW_LINE INDENT class UnionFind ( object ) : NEW_LINE INDENT def __init__ ( self , N ) : NEW_LINE INDENT self . p = list ( range ( N ) ) NEW_LINE self . rank = [ 0 ] * N NEW_LINE self . size = [ 1 ] * N NEW_LINE DEDENT def union ( self , x , y ) : NEW_LINE INDENT u = self . find ( x ) NEW_LINE v = self . find ( y ) NEW_LINE if u == v : NEW_LINE INDENT return NEW_LINE DEDENT if self . rank [ u ] < self . rank [ v ] : NEW_LINE INDENT self . p [ u ] = v NEW_LINE self . size [ v ] += self . size [ u ] NEW_LINE self . size [ u ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT self . p [ v ] = u NEW_LINE self . size [ u ] += self . size [ v ] NEW_LINE self . size [ v ] = 0 NEW_LINE if self . rank [ u ] == self . rank [ v ] : NEW_LINE INDENT self . rank [ u ] += 1 NEW_LINE DEDENT DEDENT DEDENT def find ( self , x ) : NEW_LINE INDENT if self . p [ x ] != x : NEW_LINE INDENT self . p [ x ] = self . find ( self . p [ x ] ) NEW_LINE DEDENT return self . p [ x ] NEW_LINE DEDENT def __str__ ( self ) : NEW_LINE INDENT return ' [ { } ] ' . format ( ' , ▁ ' . join ( map ( str , self . p ) ) ) NEW_LINE DEDENT DEDENT uf = UnionFind ( N ) NEW_LINE for a , b in Road : NEW_LINE INDENT uf . union ( a - 1 , b - 1 ) NEW_LINE DEDENT ans = len ( [ 1 for v in range ( N ) if uf . find ( v ) == v ] ) - 1 NEW_LINE return ans NEW_LINE DEDENT N , M = [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE Road = [ [ int ( i ) for i in input ( ) . split ( ) ] for j in range ( M ) ] NEW_LINE print ( b_construction ( N , M , Road ) ) NEW_LINE
|
T14 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; UnionFind uf = new UnionFind ( N ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) - 1 ; int b = sc . nextInt ( ) - 1 ; uf . union ( a , b ) ; } System . out . println ( uf . count ( N ) - 1 ) ; } private static class UnionFind { int parent [ ] ; int rank [ ] ; public UnionFind ( int n ) { parent = new int [ n ] ; rank = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i ; rank [ i ] = 0 ; } } boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } public int find ( int x ) { if ( parent [ x ] == x ) return x ; else return parent [ x ] = find ( parent [ x ] ) ; } public void union ( int x , int y ) { x = find ( x ) ; y = find ( y ) ; if ( x != y ) { if ( rank [ x ] > rank [ y ] ) { parent [ y ] = x ; } else { parent [ x ] = y ; if ( rank [ x ] == rank [ y ] ) { rank [ y ] ++ ; } } } return ; } public int count ( int n ) { int ret = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == find ( i ) ) ret ++ ; } return ret ; } } }
| class DisjointSet : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . rank = { } NEW_LINE self . p = { } NEW_LINE DEDENT def makeSet ( self , x ) : NEW_LINE INDENT self . p [ x ] = x NEW_LINE self . rank [ x ] = 0 NEW_LINE DEDENT def same ( self , x , y ) : NEW_LINE INDENT return self . findSet ( x ) == self . findSet ( y ) NEW_LINE DEDENT def unite ( self , x , y ) : NEW_LINE INDENT self . link ( self . findSet ( x ) , self . findSet ( y ) ) NEW_LINE DEDENT def link ( self , x , y ) : NEW_LINE INDENT if x == y : NEW_LINE INDENT return NEW_LINE DEDENT if ( self . rank [ x ] > self . rank [ y ] ) : NEW_LINE INDENT self . p [ y ] = x NEW_LINE DEDENT else : NEW_LINE INDENT self . p [ x ] = y NEW_LINE if ( self . rank [ x ] == self . rank [ y ] ) : NEW_LINE INDENT self . rank [ y ] += 1 NEW_LINE DEDENT DEDENT DEDENT def findSet ( self , x ) : NEW_LINE INDENT if ( x != self . p [ x ] ) : NEW_LINE INDENT self . p [ x ] = self . findSet ( self . p [ x ] ) NEW_LINE DEDENT return self . p [ x ] NEW_LINE DEDENT DEDENT N , M = map ( int , input ( ) . split ( ) ) NEW_LINE ds = DisjointSet ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT ds . makeSet ( i ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE ds . unite ( a , b ) NEW_LINE DEDENT connected_component = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if i == ds . findSet ( i ) : NEW_LINE INDENT connected_component += 1 NEW_LINE DEDENT DEDENT print ( connected_component - 1 ) NEW_LINE
|
T15 | import java . util . * ; public class Main { public static class Node { ArrayList < Integer > to ; boolean visit ; Node ( ) { to = new ArrayList < Integer > ( ) ; visit = false ; } } static Scanner sc = new Scanner ( System . in ) ; static int N = sc . nextInt ( ) ; static int M = sc . nextInt ( ) ; static Node [ ] node = new Node [ 100000 ] ; public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) node [ i ] = new Node ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; node [ a - 1 ] . to . add ( b - 1 ) ; node [ b - 1 ] . to . add ( a - 1 ) ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( node [ i ] . visit ) continue ; node [ i ] . visit = true ; solve ( node [ i ] ) ; count ++ ; } System . out . println ( count - 1 ) ; } public static void solve ( Node n ) { for ( int i = 0 ; i < n . to . size ( ) ; i ++ ) { int next = n . to . get ( i ) ; if ( node [ next ] . visit ) continue ; node [ next ] . visit = true ; solve ( node [ next ] ) ; } } }
| import sys NEW_LINE input = sys . stdin . readline NEW_LINE n , m = map ( int , input ( ) . split ( ) ) NEW_LINE par = [ ] NEW_LINE rank = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT par . append ( i ) NEW_LINE rank . append ( 0 ) NEW_LINE DEDENT def find ( x , par ) : NEW_LINE INDENT if par [ x ] == x : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT return find ( par [ x ] , par ) NEW_LINE DEDENT DEDENT def unite ( x , y , par , rank ) : NEW_LINE INDENT x = find ( x , par ) NEW_LINE y = find ( y , par ) NEW_LINE if x != y : NEW_LINE INDENT if rank [ x ] < rank [ y ] : NEW_LINE INDENT par [ x ] = y NEW_LINE DEDENT else : NEW_LINE INDENT par [ y ] = x NEW_LINE if rank [ x ] == rank [ y ] : NEW_LINE INDENT rank [ x ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def same ( x , y , par ) : NEW_LINE INDENT return find ( x , par ) == find ( y , par ) NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT a , b = map ( lambda x : int ( x ) - 1 , input ( ) . split ( ) ) NEW_LINE unite ( a , b , par , rank ) NEW_LINE DEDENT res = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if par [ i ] == i : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE
|
T16 | import java . util . * ; public class Main { public static class Node { ArrayList < Integer > to ; boolean visit ; Node ( ) { to = new ArrayList < Integer > ( ) ; visit = false ; } } static Scanner sc = new Scanner ( System . in ) ; static int N = sc . nextInt ( ) ; static int M = sc . nextInt ( ) ; static Node [ ] node = new Node [ 100000 ] ; public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) node [ i ] = new Node ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; node [ a - 1 ] . to . add ( b - 1 ) ; node [ b - 1 ] . to . add ( a - 1 ) ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( node [ i ] . visit ) continue ; node [ i ] . visit = true ; solve ( node [ i ] ) ; count ++ ; } System . out . println ( count - 1 ) ; } public static void solve ( Node n ) { for ( int i = 0 ; i < n . to . size ( ) ; i ++ ) { int next = n . to . get ( i ) ; if ( node [ next ] . visit ) continue ; node [ next ] . visit = true ; solve ( node [ next ] ) ; } } }
| class UnionFind ( ) : NEW_LINE INDENT def __init__ ( self , size ) : NEW_LINE INDENT self . table = [ - 1 ] * size NEW_LINE DEDENT def root ( self , x ) : NEW_LINE INDENT while self . table [ x ] >= 0 : NEW_LINE INDENT x = self . table [ x ] NEW_LINE DEDENT return x NEW_LINE DEDENT def unite ( self , x , y ) : NEW_LINE INDENT s1 = self . root ( x ) NEW_LINE s2 = self . root ( y ) NEW_LINE if s1 != s2 : NEW_LINE INDENT if self . table [ s1 ] > self . table [ s2 ] : NEW_LINE INDENT s1 , s2 = s2 , s1 NEW_LINE DEDENT self . table [ s1 ] += self . table [ s2 ] NEW_LINE self . table [ s2 ] = s1 NEW_LINE DEDENT return NEW_LINE DEDENT def same ( self , x , y ) : NEW_LINE INDENT return self . root ( x ) == self . root ( y ) NEW_LINE DEDENT def size ( self , x ) : NEW_LINE INDENT return - self . table [ self . root ( x ) ] NEW_LINE DEDENT DEDENT ( n , m ) , * e = [ map ( int , t . split ( ) ) for t in open ( 0 ) . readlines ( ) ] NEW_LINE u = UnionFind ( n ) NEW_LINE for x , y in e : NEW_LINE INDENT u . unite ( x - 1 , y - 1 ) NEW_LINE DEDENT print ( sum ( i < 0 for i in u . table ) - 1 ) NEW_LINE
|
T17 | import java . util . * ; public class Main { public static class Node { ArrayList < Integer > to ; boolean visit ; Node ( ) { to = new ArrayList < Integer > ( ) ; visit = false ; } } static Scanner sc = new Scanner ( System . in ) ; static int N = sc . nextInt ( ) ; static int M = sc . nextInt ( ) ; static Node [ ] node = new Node [ 100000 ] ; public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) node [ i ] = new Node ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; node [ a - 1 ] . to . add ( b - 1 ) ; node [ b - 1 ] . to . add ( a - 1 ) ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( node [ i ] . visit ) continue ; node [ i ] . visit = true ; solve ( node [ i ] ) ; count ++ ; } System . out . println ( count - 1 ) ; } public static void solve ( Node n ) { for ( int i = 0 ; i < n . to . size ( ) ; i ++ ) { int next = n . to . get ( i ) ; if ( node [ next ] . visit ) continue ; node [ next ] . visit = true ; solve ( node [ next ] ) ; } } }
| from collections import defaultdict NEW_LINE import sys NEW_LINE sys . setrecursionlimit ( sys . getrecursionlimit ( ) * 10000 ) NEW_LINE N , M = map ( int , input ( ) . split ( ) ) NEW_LINE g = defaultdict ( list ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE g [ a ] . append ( b ) NEW_LINE g [ b ] . append ( a ) NEW_LINE DEDENT visited = set ( ) NEW_LINE def dfs ( v ) : NEW_LINE INDENT visited . add ( v ) NEW_LINE for c in g [ v ] : NEW_LINE INDENT if not c in visited : NEW_LINE INDENT dfs ( c ) NEW_LINE DEDENT DEDENT DEDENT connected_component = 0 NEW_LINE for v in range ( 1 , N + 1 ) : NEW_LINE INDENT if not v in visited : NEW_LINE INDENT dfs ( v ) NEW_LINE connected_component += 1 NEW_LINE DEDENT DEDENT print ( connected_component - 1 ) NEW_LINE
|
T18 | import java . util . * ; public class Main { public static class Node { ArrayList < Integer > to ; boolean visit ; Node ( ) { to = new ArrayList < Integer > ( ) ; visit = false ; } } static Scanner sc = new Scanner ( System . in ) ; static int N = sc . nextInt ( ) ; static int M = sc . nextInt ( ) ; static Node [ ] node = new Node [ 100000 ] ; public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) node [ i ] = new Node ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; node [ a - 1 ] . to . add ( b - 1 ) ; node [ b - 1 ] . to . add ( a - 1 ) ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( node [ i ] . visit ) continue ; node [ i ] . visit = true ; solve ( node [ i ] ) ; count ++ ; } System . out . println ( count - 1 ) ; } public static void solve ( Node n ) { for ( int i = 0 ; i < n . to . size ( ) ; i ++ ) { int next = n . to . get ( i ) ; if ( node [ next ] . visit ) continue ; node [ next ] . visit = true ; solve ( node [ next ] ) ; } } }
| def b_construction ( N , M , Road ) : NEW_LINE INDENT class UnionFind ( object ) : NEW_LINE INDENT def __init__ ( self , N ) : NEW_LINE INDENT self . p = list ( range ( N ) ) NEW_LINE self . rank = [ 0 ] * N NEW_LINE self . size = [ 1 ] * N NEW_LINE DEDENT def union ( self , x , y ) : NEW_LINE INDENT u = self . find ( x ) NEW_LINE v = self . find ( y ) NEW_LINE if u == v : NEW_LINE INDENT return NEW_LINE DEDENT if self . rank [ u ] < self . rank [ v ] : NEW_LINE INDENT self . p [ u ] = v NEW_LINE self . size [ v ] += self . size [ u ] NEW_LINE self . size [ u ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT self . p [ v ] = u NEW_LINE self . size [ u ] += self . size [ v ] NEW_LINE self . size [ v ] = 0 NEW_LINE if self . rank [ u ] == self . rank [ v ] : NEW_LINE INDENT self . rank [ u ] += 1 NEW_LINE DEDENT DEDENT DEDENT def find ( self , x ) : NEW_LINE INDENT if self . p [ x ] != x : NEW_LINE INDENT self . p [ x ] = self . find ( self . p [ x ] ) NEW_LINE DEDENT return self . p [ x ] NEW_LINE DEDENT def __str__ ( self ) : NEW_LINE INDENT return ' [ { } ] ' . format ( ' , ▁ ' . join ( map ( str , self . p ) ) ) NEW_LINE DEDENT DEDENT uf = UnionFind ( N ) NEW_LINE for a , b in Road : NEW_LINE INDENT uf . union ( a - 1 , b - 1 ) NEW_LINE DEDENT ans = len ( [ 1 for v in range ( N ) if uf . find ( v ) == v ] ) - 1 NEW_LINE return ans NEW_LINE DEDENT N , M = [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE Road = [ [ int ( i ) for i in input ( ) . split ( ) ] for j in range ( M ) ] NEW_LINE print ( b_construction ( N , M , Road ) ) NEW_LINE
|
T19 | import java . util . * ; public class Main { public static class Node { ArrayList < Integer > to ; boolean visit ; Node ( ) { to = new ArrayList < Integer > ( ) ; visit = false ; } } static Scanner sc = new Scanner ( System . in ) ; static int N = sc . nextInt ( ) ; static int M = sc . nextInt ( ) ; static Node [ ] node = new Node [ 100000 ] ; public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) node [ i ] = new Node ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; node [ a - 1 ] . to . add ( b - 1 ) ; node [ b - 1 ] . to . add ( a - 1 ) ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( node [ i ] . visit ) continue ; node [ i ] . visit = true ; solve ( node [ i ] ) ; count ++ ; } System . out . println ( count - 1 ) ; } public static void solve ( Node n ) { for ( int i = 0 ; i < n . to . size ( ) ; i ++ ) { int next = n . to . get ( i ) ; if ( node [ next ] . visit ) continue ; node [ next ] . visit = true ; solve ( node [ next ] ) ; } } }
| class DisjointSet : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . rank = { } NEW_LINE self . p = { } NEW_LINE DEDENT def makeSet ( self , x ) : NEW_LINE INDENT self . p [ x ] = x NEW_LINE self . rank [ x ] = 0 NEW_LINE DEDENT def same ( self , x , y ) : NEW_LINE INDENT return self . findSet ( x ) == self . findSet ( y ) NEW_LINE DEDENT def unite ( self , x , y ) : NEW_LINE INDENT self . link ( self . findSet ( x ) , self . findSet ( y ) ) NEW_LINE DEDENT def link ( self , x , y ) : NEW_LINE INDENT if x == y : NEW_LINE INDENT return NEW_LINE DEDENT if ( self . rank [ x ] > self . rank [ y ] ) : NEW_LINE INDENT self . p [ y ] = x NEW_LINE DEDENT else : NEW_LINE INDENT self . p [ x ] = y NEW_LINE if ( self . rank [ x ] == self . rank [ y ] ) : NEW_LINE INDENT self . rank [ y ] += 1 NEW_LINE DEDENT DEDENT DEDENT def findSet ( self , x ) : NEW_LINE INDENT if ( x != self . p [ x ] ) : NEW_LINE INDENT self . p [ x ] = self . findSet ( self . p [ x ] ) NEW_LINE DEDENT return self . p [ x ] NEW_LINE DEDENT DEDENT N , M = map ( int , input ( ) . split ( ) ) NEW_LINE ds = DisjointSet ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT ds . makeSet ( i ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE ds . unite ( a , b ) NEW_LINE DEDENT connected_component = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if i == ds . findSet ( i ) : NEW_LINE INDENT connected_component += 1 NEW_LINE DEDENT DEDENT print ( connected_component - 1 ) NEW_LINE
|
T20 | import java . util . Scanner ; public class Main { static Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; UnionFind uf = new UnionFind ( N ) ; uf . init ( N ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) - 1 ; int b = sc . nextInt ( ) - 1 ; uf . unite ( a , b ) ; } for ( int i = 0 ; i < N ; i ++ ) { uf . find ( i ) ; } boolean [ ] used = new boolean [ N ] ; int ans = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! used [ uf . parent [ i ] ] ) { used [ uf . parent [ i ] ] = true ; ans ++ ; } } System . out . println ( ans ) ; } } class UnionFind { public int [ ] parent ; public int [ ] rank ; public UnionFind ( int MAX_SIZE ) { parent = new int [ MAX_SIZE ] ; rank = new int [ MAX_SIZE ] ; } public void init ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i ; rank [ i ] = 0 ; } } public int find ( int x ) { if ( parent [ x ] == x ) { return x ; } else { return parent [ x ] = find ( parent [ x ] ) ; } } public void unite ( int x , int y ) { x = find ( x ) ; y = find ( y ) ; if ( x == y ) return ; if ( rank [ x ] < rank [ y ] ) parent [ x ] = y ; else parent [ y ] = x ; if ( rank [ x ] == rank [ y ] ) rank [ x ] ++ ; } public boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } }
| import sys NEW_LINE input = sys . stdin . readline NEW_LINE n , m = map ( int , input ( ) . split ( ) ) NEW_LINE par = [ ] NEW_LINE rank = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT par . append ( i ) NEW_LINE rank . append ( 0 ) NEW_LINE DEDENT def find ( x , par ) : NEW_LINE INDENT if par [ x ] == x : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT return find ( par [ x ] , par ) NEW_LINE DEDENT DEDENT def unite ( x , y , par , rank ) : NEW_LINE INDENT x = find ( x , par ) NEW_LINE y = find ( y , par ) NEW_LINE if x != y : NEW_LINE INDENT if rank [ x ] < rank [ y ] : NEW_LINE INDENT par [ x ] = y NEW_LINE DEDENT else : NEW_LINE INDENT par [ y ] = x NEW_LINE if rank [ x ] == rank [ y ] : NEW_LINE INDENT rank [ x ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def same ( x , y , par ) : NEW_LINE INDENT return find ( x , par ) == find ( y , par ) NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT a , b = map ( lambda x : int ( x ) - 1 , input ( ) . split ( ) ) NEW_LINE unite ( a , b , par , rank ) NEW_LINE DEDENT res = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if par [ i ] == i : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE
|
T21 | import java . util . Scanner ; public class Main { static Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; UnionFind uf = new UnionFind ( N ) ; uf . init ( N ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) - 1 ; int b = sc . nextInt ( ) - 1 ; uf . unite ( a , b ) ; } for ( int i = 0 ; i < N ; i ++ ) { uf . find ( i ) ; } boolean [ ] used = new boolean [ N ] ; int ans = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! used [ uf . parent [ i ] ] ) { used [ uf . parent [ i ] ] = true ; ans ++ ; } } System . out . println ( ans ) ; } } class UnionFind { public int [ ] parent ; public int [ ] rank ; public UnionFind ( int MAX_SIZE ) { parent = new int [ MAX_SIZE ] ; rank = new int [ MAX_SIZE ] ; } public void init ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i ; rank [ i ] = 0 ; } } public int find ( int x ) { if ( parent [ x ] == x ) { return x ; } else { return parent [ x ] = find ( parent [ x ] ) ; } } public void unite ( int x , int y ) { x = find ( x ) ; y = find ( y ) ; if ( x == y ) return ; if ( rank [ x ] < rank [ y ] ) parent [ x ] = y ; else parent [ y ] = x ; if ( rank [ x ] == rank [ y ] ) rank [ x ] ++ ; } public boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } }
| class UnionFind ( ) : NEW_LINE INDENT def __init__ ( self , size ) : NEW_LINE INDENT self . table = [ - 1 ] * size NEW_LINE DEDENT def root ( self , x ) : NEW_LINE INDENT while self . table [ x ] >= 0 : NEW_LINE INDENT x = self . table [ x ] NEW_LINE DEDENT return x NEW_LINE DEDENT def unite ( self , x , y ) : NEW_LINE INDENT s1 = self . root ( x ) NEW_LINE s2 = self . root ( y ) NEW_LINE if s1 != s2 : NEW_LINE INDENT if self . table [ s1 ] > self . table [ s2 ] : NEW_LINE INDENT s1 , s2 = s2 , s1 NEW_LINE DEDENT self . table [ s1 ] += self . table [ s2 ] NEW_LINE self . table [ s2 ] = s1 NEW_LINE DEDENT return NEW_LINE DEDENT def same ( self , x , y ) : NEW_LINE INDENT return self . root ( x ) == self . root ( y ) NEW_LINE DEDENT def size ( self , x ) : NEW_LINE INDENT return - self . table [ self . root ( x ) ] NEW_LINE DEDENT DEDENT ( n , m ) , * e = [ map ( int , t . split ( ) ) for t in open ( 0 ) . readlines ( ) ] NEW_LINE u = UnionFind ( n ) NEW_LINE for x , y in e : NEW_LINE INDENT u . unite ( x - 1 , y - 1 ) NEW_LINE DEDENT print ( sum ( i < 0 for i in u . table ) - 1 ) NEW_LINE
|
T22 | import java . util . Scanner ; public class Main { static Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; UnionFind uf = new UnionFind ( N ) ; uf . init ( N ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) - 1 ; int b = sc . nextInt ( ) - 1 ; uf . unite ( a , b ) ; } for ( int i = 0 ; i < N ; i ++ ) { uf . find ( i ) ; } boolean [ ] used = new boolean [ N ] ; int ans = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! used [ uf . parent [ i ] ] ) { used [ uf . parent [ i ] ] = true ; ans ++ ; } } System . out . println ( ans ) ; } } class UnionFind { public int [ ] parent ; public int [ ] rank ; public UnionFind ( int MAX_SIZE ) { parent = new int [ MAX_SIZE ] ; rank = new int [ MAX_SIZE ] ; } public void init ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i ; rank [ i ] = 0 ; } } public int find ( int x ) { if ( parent [ x ] == x ) { return x ; } else { return parent [ x ] = find ( parent [ x ] ) ; } } public void unite ( int x , int y ) { x = find ( x ) ; y = find ( y ) ; if ( x == y ) return ; if ( rank [ x ] < rank [ y ] ) parent [ x ] = y ; else parent [ y ] = x ; if ( rank [ x ] == rank [ y ] ) rank [ x ] ++ ; } public boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } }
| from collections import defaultdict NEW_LINE import sys NEW_LINE sys . setrecursionlimit ( sys . getrecursionlimit ( ) * 10000 ) NEW_LINE N , M = map ( int , input ( ) . split ( ) ) NEW_LINE g = defaultdict ( list ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE g [ a ] . append ( b ) NEW_LINE g [ b ] . append ( a ) NEW_LINE DEDENT visited = set ( ) NEW_LINE def dfs ( v ) : NEW_LINE INDENT visited . add ( v ) NEW_LINE for c in g [ v ] : NEW_LINE INDENT if not c in visited : NEW_LINE INDENT dfs ( c ) NEW_LINE DEDENT DEDENT DEDENT connected_component = 0 NEW_LINE for v in range ( 1 , N + 1 ) : NEW_LINE INDENT if not v in visited : NEW_LINE INDENT dfs ( v ) NEW_LINE connected_component += 1 NEW_LINE DEDENT DEDENT print ( connected_component - 1 ) NEW_LINE
|
T23 | import java . util . Scanner ; public class Main { static Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; UnionFind uf = new UnionFind ( N ) ; uf . init ( N ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) - 1 ; int b = sc . nextInt ( ) - 1 ; uf . unite ( a , b ) ; } for ( int i = 0 ; i < N ; i ++ ) { uf . find ( i ) ; } boolean [ ] used = new boolean [ N ] ; int ans = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! used [ uf . parent [ i ] ] ) { used [ uf . parent [ i ] ] = true ; ans ++ ; } } System . out . println ( ans ) ; } } class UnionFind { public int [ ] parent ; public int [ ] rank ; public UnionFind ( int MAX_SIZE ) { parent = new int [ MAX_SIZE ] ; rank = new int [ MAX_SIZE ] ; } public void init ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i ; rank [ i ] = 0 ; } } public int find ( int x ) { if ( parent [ x ] == x ) { return x ; } else { return parent [ x ] = find ( parent [ x ] ) ; } } public void unite ( int x , int y ) { x = find ( x ) ; y = find ( y ) ; if ( x == y ) return ; if ( rank [ x ] < rank [ y ] ) parent [ x ] = y ; else parent [ y ] = x ; if ( rank [ x ] == rank [ y ] ) rank [ x ] ++ ; } public boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } }
| def b_construction ( N , M , Road ) : NEW_LINE INDENT class UnionFind ( object ) : NEW_LINE INDENT def __init__ ( self , N ) : NEW_LINE INDENT self . p = list ( range ( N ) ) NEW_LINE self . rank = [ 0 ] * N NEW_LINE self . size = [ 1 ] * N NEW_LINE DEDENT def union ( self , x , y ) : NEW_LINE INDENT u = self . find ( x ) NEW_LINE v = self . find ( y ) NEW_LINE if u == v : NEW_LINE INDENT return NEW_LINE DEDENT if self . rank [ u ] < self . rank [ v ] : NEW_LINE INDENT self . p [ u ] = v NEW_LINE self . size [ v ] += self . size [ u ] NEW_LINE self . size [ u ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT self . p [ v ] = u NEW_LINE self . size [ u ] += self . size [ v ] NEW_LINE self . size [ v ] = 0 NEW_LINE if self . rank [ u ] == self . rank [ v ] : NEW_LINE INDENT self . rank [ u ] += 1 NEW_LINE DEDENT DEDENT DEDENT def find ( self , x ) : NEW_LINE INDENT if self . p [ x ] != x : NEW_LINE INDENT self . p [ x ] = self . find ( self . p [ x ] ) NEW_LINE DEDENT return self . p [ x ] NEW_LINE DEDENT def __str__ ( self ) : NEW_LINE INDENT return ' [ { } ] ' . format ( ' , ▁ ' . join ( map ( str , self . p ) ) ) NEW_LINE DEDENT DEDENT uf = UnionFind ( N ) NEW_LINE for a , b in Road : NEW_LINE INDENT uf . union ( a - 1 , b - 1 ) NEW_LINE DEDENT ans = len ( [ 1 for v in range ( N ) if uf . find ( v ) == v ] ) - 1 NEW_LINE return ans NEW_LINE DEDENT N , M = [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE Road = [ [ int ( i ) for i in input ( ) . split ( ) ] for j in range ( M ) ] NEW_LINE print ( b_construction ( N , M , Road ) ) NEW_LINE
|
T24 | import java . util . Scanner ; public class Main { static Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; UnionFind uf = new UnionFind ( N ) ; uf . init ( N ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) - 1 ; int b = sc . nextInt ( ) - 1 ; uf . unite ( a , b ) ; } for ( int i = 0 ; i < N ; i ++ ) { uf . find ( i ) ; } boolean [ ] used = new boolean [ N ] ; int ans = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! used [ uf . parent [ i ] ] ) { used [ uf . parent [ i ] ] = true ; ans ++ ; } } System . out . println ( ans ) ; } } class UnionFind { public int [ ] parent ; public int [ ] rank ; public UnionFind ( int MAX_SIZE ) { parent = new int [ MAX_SIZE ] ; rank = new int [ MAX_SIZE ] ; } public void init ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i ; rank [ i ] = 0 ; } } public int find ( int x ) { if ( parent [ x ] == x ) { return x ; } else { return parent [ x ] = find ( parent [ x ] ) ; } } public void unite ( int x , int y ) { x = find ( x ) ; y = find ( y ) ; if ( x == y ) return ; if ( rank [ x ] < rank [ y ] ) parent [ x ] = y ; else parent [ y ] = x ; if ( rank [ x ] == rank [ y ] ) rank [ x ] ++ ; } public boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } }
| class DisjointSet : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . rank = { } NEW_LINE self . p = { } NEW_LINE DEDENT def makeSet ( self , x ) : NEW_LINE INDENT self . p [ x ] = x NEW_LINE self . rank [ x ] = 0 NEW_LINE DEDENT def same ( self , x , y ) : NEW_LINE INDENT return self . findSet ( x ) == self . findSet ( y ) NEW_LINE DEDENT def unite ( self , x , y ) : NEW_LINE INDENT self . link ( self . findSet ( x ) , self . findSet ( y ) ) NEW_LINE DEDENT def link ( self , x , y ) : NEW_LINE INDENT if x == y : NEW_LINE INDENT return NEW_LINE DEDENT if ( self . rank [ x ] > self . rank [ y ] ) : NEW_LINE INDENT self . p [ y ] = x NEW_LINE DEDENT else : NEW_LINE INDENT self . p [ x ] = y NEW_LINE if ( self . rank [ x ] == self . rank [ y ] ) : NEW_LINE INDENT self . rank [ y ] += 1 NEW_LINE DEDENT DEDENT DEDENT def findSet ( self , x ) : NEW_LINE INDENT if ( x != self . p [ x ] ) : NEW_LINE INDENT self . p [ x ] = self . findSet ( self . p [ x ] ) NEW_LINE DEDENT return self . p [ x ] NEW_LINE DEDENT DEDENT N , M = map ( int , input ( ) . split ( ) ) NEW_LINE ds = DisjointSet ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT ds . makeSet ( i ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE ds . unite ( a , b ) NEW_LINE DEDENT connected_component = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if i == ds . findSet ( i ) : NEW_LINE INDENT connected_component += 1 NEW_LINE DEDENT DEDENT print ( connected_component - 1 ) NEW_LINE
|
T25 | import java . util . * ; public class Main { public static class Node { ArrayList < Integer > to ; boolean visit ; Node ( ) { to = new ArrayList < Integer > ( ) ; visit = false ; } } static Node [ ] node = new Node [ 100000 ] ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; for ( int i = 0 ; i < N ; i ++ ) node [ i ] = new Node ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; node [ a - 1 ] . to . add ( b - 1 ) ; node [ b - 1 ] . to . add ( a - 1 ) ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( node [ i ] . visit ) continue ; node [ i ] . visit = true ; solve ( node [ i ] ) ; count ++ ; } System . out . println ( count - 1 ) ; } public static void solve ( Node n ) { for ( int i = 0 ; i < n . to . size ( ) ; i ++ ) { int next = n . to . get ( i ) ; if ( node [ next ] . visit ) continue ; node [ next ] . visit = true ; solve ( node [ next ] ) ; } } }
| import sys NEW_LINE input = sys . stdin . readline NEW_LINE n , m = map ( int , input ( ) . split ( ) ) NEW_LINE par = [ ] NEW_LINE rank = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT par . append ( i ) NEW_LINE rank . append ( 0 ) NEW_LINE DEDENT def find ( x , par ) : NEW_LINE INDENT if par [ x ] == x : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT return find ( par [ x ] , par ) NEW_LINE DEDENT DEDENT def unite ( x , y , par , rank ) : NEW_LINE INDENT x = find ( x , par ) NEW_LINE y = find ( y , par ) NEW_LINE if x != y : NEW_LINE INDENT if rank [ x ] < rank [ y ] : NEW_LINE INDENT par [ x ] = y NEW_LINE DEDENT else : NEW_LINE INDENT par [ y ] = x NEW_LINE if rank [ x ] == rank [ y ] : NEW_LINE INDENT rank [ x ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def same ( x , y , par ) : NEW_LINE INDENT return find ( x , par ) == find ( y , par ) NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT a , b = map ( lambda x : int ( x ) - 1 , input ( ) . split ( ) ) NEW_LINE unite ( a , b , par , rank ) NEW_LINE DEDENT res = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if par [ i ] == i : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE
|
T26 | import java . util . * ; public class Main { public static class Node { ArrayList < Integer > to ; boolean visit ; Node ( ) { to = new ArrayList < Integer > ( ) ; visit = false ; } } static Node [ ] node = new Node [ 100000 ] ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; for ( int i = 0 ; i < N ; i ++ ) node [ i ] = new Node ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; node [ a - 1 ] . to . add ( b - 1 ) ; node [ b - 1 ] . to . add ( a - 1 ) ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( node [ i ] . visit ) continue ; node [ i ] . visit = true ; solve ( node [ i ] ) ; count ++ ; } System . out . println ( count - 1 ) ; } public static void solve ( Node n ) { for ( int i = 0 ; i < n . to . size ( ) ; i ++ ) { int next = n . to . get ( i ) ; if ( node [ next ] . visit ) continue ; node [ next ] . visit = true ; solve ( node [ next ] ) ; } } }
| class UnionFind ( ) : NEW_LINE INDENT def __init__ ( self , size ) : NEW_LINE INDENT self . table = [ - 1 ] * size NEW_LINE DEDENT def root ( self , x ) : NEW_LINE INDENT while self . table [ x ] >= 0 : NEW_LINE INDENT x = self . table [ x ] NEW_LINE DEDENT return x NEW_LINE DEDENT def unite ( self , x , y ) : NEW_LINE INDENT s1 = self . root ( x ) NEW_LINE s2 = self . root ( y ) NEW_LINE if s1 != s2 : NEW_LINE INDENT if self . table [ s1 ] > self . table [ s2 ] : NEW_LINE INDENT s1 , s2 = s2 , s1 NEW_LINE DEDENT self . table [ s1 ] += self . table [ s2 ] NEW_LINE self . table [ s2 ] = s1 NEW_LINE DEDENT return NEW_LINE DEDENT def same ( self , x , y ) : NEW_LINE INDENT return self . root ( x ) == self . root ( y ) NEW_LINE DEDENT def size ( self , x ) : NEW_LINE INDENT return - self . table [ self . root ( x ) ] NEW_LINE DEDENT DEDENT ( n , m ) , * e = [ map ( int , t . split ( ) ) for t in open ( 0 ) . readlines ( ) ] NEW_LINE u = UnionFind ( n ) NEW_LINE for x , y in e : NEW_LINE INDENT u . unite ( x - 1 , y - 1 ) NEW_LINE DEDENT print ( sum ( i < 0 for i in u . table ) - 1 ) NEW_LINE
|
T27 | import java . util . * ; public class Main { public static class Node { ArrayList < Integer > to ; boolean visit ; Node ( ) { to = new ArrayList < Integer > ( ) ; visit = false ; } } static Node [ ] node = new Node [ 100000 ] ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; for ( int i = 0 ; i < N ; i ++ ) node [ i ] = new Node ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; node [ a - 1 ] . to . add ( b - 1 ) ; node [ b - 1 ] . to . add ( a - 1 ) ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( node [ i ] . visit ) continue ; node [ i ] . visit = true ; solve ( node [ i ] ) ; count ++ ; } System . out . println ( count - 1 ) ; } public static void solve ( Node n ) { for ( int i = 0 ; i < n . to . size ( ) ; i ++ ) { int next = n . to . get ( i ) ; if ( node [ next ] . visit ) continue ; node [ next ] . visit = true ; solve ( node [ next ] ) ; } } }
| from collections import defaultdict NEW_LINE import sys NEW_LINE sys . setrecursionlimit ( sys . getrecursionlimit ( ) * 10000 ) NEW_LINE N , M = map ( int , input ( ) . split ( ) ) NEW_LINE g = defaultdict ( list ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE g [ a ] . append ( b ) NEW_LINE g [ b ] . append ( a ) NEW_LINE DEDENT visited = set ( ) NEW_LINE def dfs ( v ) : NEW_LINE INDENT visited . add ( v ) NEW_LINE for c in g [ v ] : NEW_LINE INDENT if not c in visited : NEW_LINE INDENT dfs ( c ) NEW_LINE DEDENT DEDENT DEDENT connected_component = 0 NEW_LINE for v in range ( 1 , N + 1 ) : NEW_LINE INDENT if not v in visited : NEW_LINE INDENT dfs ( v ) NEW_LINE connected_component += 1 NEW_LINE DEDENT DEDENT print ( connected_component - 1 ) NEW_LINE
|
T28 | import java . util . * ; public class Main { public static class Node { ArrayList < Integer > to ; boolean visit ; Node ( ) { to = new ArrayList < Integer > ( ) ; visit = false ; } } static Node [ ] node = new Node [ 100000 ] ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; for ( int i = 0 ; i < N ; i ++ ) node [ i ] = new Node ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; node [ a - 1 ] . to . add ( b - 1 ) ; node [ b - 1 ] . to . add ( a - 1 ) ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( node [ i ] . visit ) continue ; node [ i ] . visit = true ; solve ( node [ i ] ) ; count ++ ; } System . out . println ( count - 1 ) ; } public static void solve ( Node n ) { for ( int i = 0 ; i < n . to . size ( ) ; i ++ ) { int next = n . to . get ( i ) ; if ( node [ next ] . visit ) continue ; node [ next ] . visit = true ; solve ( node [ next ] ) ; } } }
| def b_construction ( N , M , Road ) : NEW_LINE INDENT class UnionFind ( object ) : NEW_LINE INDENT def __init__ ( self , N ) : NEW_LINE INDENT self . p = list ( range ( N ) ) NEW_LINE self . rank = [ 0 ] * N NEW_LINE self . size = [ 1 ] * N NEW_LINE DEDENT def union ( self , x , y ) : NEW_LINE INDENT u = self . find ( x ) NEW_LINE v = self . find ( y ) NEW_LINE if u == v : NEW_LINE INDENT return NEW_LINE DEDENT if self . rank [ u ] < self . rank [ v ] : NEW_LINE INDENT self . p [ u ] = v NEW_LINE self . size [ v ] += self . size [ u ] NEW_LINE self . size [ u ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT self . p [ v ] = u NEW_LINE self . size [ u ] += self . size [ v ] NEW_LINE self . size [ v ] = 0 NEW_LINE if self . rank [ u ] == self . rank [ v ] : NEW_LINE INDENT self . rank [ u ] += 1 NEW_LINE DEDENT DEDENT DEDENT def find ( self , x ) : NEW_LINE INDENT if self . p [ x ] != x : NEW_LINE INDENT self . p [ x ] = self . find ( self . p [ x ] ) NEW_LINE DEDENT return self . p [ x ] NEW_LINE DEDENT def __str__ ( self ) : NEW_LINE INDENT return ' [ { } ] ' . format ( ' , ▁ ' . join ( map ( str , self . p ) ) ) NEW_LINE DEDENT DEDENT uf = UnionFind ( N ) NEW_LINE for a , b in Road : NEW_LINE INDENT uf . union ( a - 1 , b - 1 ) NEW_LINE DEDENT ans = len ( [ 1 for v in range ( N ) if uf . find ( v ) == v ] ) - 1 NEW_LINE return ans NEW_LINE DEDENT N , M = [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE Road = [ [ int ( i ) for i in input ( ) . split ( ) ] for j in range ( M ) ] NEW_LINE print ( b_construction ( N , M , Road ) ) NEW_LINE
|
T29 | import java . util . * ; public class Main { public static class Node { ArrayList < Integer > to ; boolean visit ; Node ( ) { to = new ArrayList < Integer > ( ) ; visit = false ; } } static Node [ ] node = new Node [ 100000 ] ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; for ( int i = 0 ; i < N ; i ++ ) node [ i ] = new Node ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; node [ a - 1 ] . to . add ( b - 1 ) ; node [ b - 1 ] . to . add ( a - 1 ) ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( node [ i ] . visit ) continue ; node [ i ] . visit = true ; solve ( node [ i ] ) ; count ++ ; } System . out . println ( count - 1 ) ; } public static void solve ( Node n ) { for ( int i = 0 ; i < n . to . size ( ) ; i ++ ) { int next = n . to . get ( i ) ; if ( node [ next ] . visit ) continue ; node [ next ] . visit = true ; solve ( node [ next ] ) ; } } }
| class DisjointSet : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . rank = { } NEW_LINE self . p = { } NEW_LINE DEDENT def makeSet ( self , x ) : NEW_LINE INDENT self . p [ x ] = x NEW_LINE self . rank [ x ] = 0 NEW_LINE DEDENT def same ( self , x , y ) : NEW_LINE INDENT return self . findSet ( x ) == self . findSet ( y ) NEW_LINE DEDENT def unite ( self , x , y ) : NEW_LINE INDENT self . link ( self . findSet ( x ) , self . findSet ( y ) ) NEW_LINE DEDENT def link ( self , x , y ) : NEW_LINE INDENT if x == y : NEW_LINE INDENT return NEW_LINE DEDENT if ( self . rank [ x ] > self . rank [ y ] ) : NEW_LINE INDENT self . p [ y ] = x NEW_LINE DEDENT else : NEW_LINE INDENT self . p [ x ] = y NEW_LINE if ( self . rank [ x ] == self . rank [ y ] ) : NEW_LINE INDENT self . rank [ y ] += 1 NEW_LINE DEDENT DEDENT DEDENT def findSet ( self , x ) : NEW_LINE INDENT if ( x != self . p [ x ] ) : NEW_LINE INDENT self . p [ x ] = self . findSet ( self . p [ x ] ) NEW_LINE DEDENT return self . p [ x ] NEW_LINE DEDENT DEDENT N , M = map ( int , input ( ) . split ( ) ) NEW_LINE ds = DisjointSet ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT ds . makeSet ( i ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE ds . unite ( a , b ) NEW_LINE DEDENT connected_component = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if i == ds . findSet ( i ) : NEW_LINE INDENT connected_component += 1 NEW_LINE DEDENT DEDENT print ( connected_component - 1 ) NEW_LINE
|
T30 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; UnionFindTree uft = new UnionFindTree ( n ) ; for ( int i = 0 ; i < m ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; uft . union ( a - 1 , b - 1 ) ; } System . out . println ( uft . countBlock ( ) - 1 ) ; } static class UnionFindTree { int [ ] parent ; int [ ] rank ; public UnionFindTree ( int size ) { parent = new int [ size ] ; rank = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { parent [ i ] = i ; } } public int find ( int x ) { if ( parent [ x ] == x ) { return x ; } else { int v = find ( parent [ x ] ) ; parent [ x ] = v ; return v ; } } public boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } public void union ( int x , int y ) { int rootX = find ( x ) ; int rootY = find ( y ) ; if ( rank [ rootX ] > rank [ rootY ] ) { parent [ rootY ] = rootX ; } else if ( rank [ rootX ] < rank [ rootY ] ) { parent [ rootX ] = rootY ; } else if ( rootX != rootY ) { parent [ rootX ] = rootY ; rank [ rootX ] ++ ; } } public int countBlock ( ) { int count = 0 ; for ( int i = 0 ; i < parent . length ; i ++ ) { if ( parent [ i ] == i ) { count ++ ; } } return count ; } } }
| import sys NEW_LINE input = sys . stdin . readline NEW_LINE n , m = map ( int , input ( ) . split ( ) ) NEW_LINE par = [ ] NEW_LINE rank = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT par . append ( i ) NEW_LINE rank . append ( 0 ) NEW_LINE DEDENT def find ( x , par ) : NEW_LINE INDENT if par [ x ] == x : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT return find ( par [ x ] , par ) NEW_LINE DEDENT DEDENT def unite ( x , y , par , rank ) : NEW_LINE INDENT x = find ( x , par ) NEW_LINE y = find ( y , par ) NEW_LINE if x != y : NEW_LINE INDENT if rank [ x ] < rank [ y ] : NEW_LINE INDENT par [ x ] = y NEW_LINE DEDENT else : NEW_LINE INDENT par [ y ] = x NEW_LINE if rank [ x ] == rank [ y ] : NEW_LINE INDENT rank [ x ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def same ( x , y , par ) : NEW_LINE INDENT return find ( x , par ) == find ( y , par ) NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT a , b = map ( lambda x : int ( x ) - 1 , input ( ) . split ( ) ) NEW_LINE unite ( a , b , par , rank ) NEW_LINE DEDENT res = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if par [ i ] == i : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE
|
T31 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; UnionFindTree uft = new UnionFindTree ( n ) ; for ( int i = 0 ; i < m ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; uft . union ( a - 1 , b - 1 ) ; } System . out . println ( uft . countBlock ( ) - 1 ) ; } static class UnionFindTree { int [ ] parent ; int [ ] rank ; public UnionFindTree ( int size ) { parent = new int [ size ] ; rank = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { parent [ i ] = i ; } } public int find ( int x ) { if ( parent [ x ] == x ) { return x ; } else { int v = find ( parent [ x ] ) ; parent [ x ] = v ; return v ; } } public boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } public void union ( int x , int y ) { int rootX = find ( x ) ; int rootY = find ( y ) ; if ( rank [ rootX ] > rank [ rootY ] ) { parent [ rootY ] = rootX ; } else if ( rank [ rootX ] < rank [ rootY ] ) { parent [ rootX ] = rootY ; } else if ( rootX != rootY ) { parent [ rootX ] = rootY ; rank [ rootX ] ++ ; } } public int countBlock ( ) { int count = 0 ; for ( int i = 0 ; i < parent . length ; i ++ ) { if ( parent [ i ] == i ) { count ++ ; } } return count ; } } }
| class UnionFind ( ) : NEW_LINE INDENT def __init__ ( self , size ) : NEW_LINE INDENT self . table = [ - 1 ] * size NEW_LINE DEDENT def root ( self , x ) : NEW_LINE INDENT while self . table [ x ] >= 0 : NEW_LINE INDENT x = self . table [ x ] NEW_LINE DEDENT return x NEW_LINE DEDENT def unite ( self , x , y ) : NEW_LINE INDENT s1 = self . root ( x ) NEW_LINE s2 = self . root ( y ) NEW_LINE if s1 != s2 : NEW_LINE INDENT if self . table [ s1 ] > self . table [ s2 ] : NEW_LINE INDENT s1 , s2 = s2 , s1 NEW_LINE DEDENT self . table [ s1 ] += self . table [ s2 ] NEW_LINE self . table [ s2 ] = s1 NEW_LINE DEDENT return NEW_LINE DEDENT def same ( self , x , y ) : NEW_LINE INDENT return self . root ( x ) == self . root ( y ) NEW_LINE DEDENT def size ( self , x ) : NEW_LINE INDENT return - self . table [ self . root ( x ) ] NEW_LINE DEDENT DEDENT ( n , m ) , * e = [ map ( int , t . split ( ) ) for t in open ( 0 ) . readlines ( ) ] NEW_LINE u = UnionFind ( n ) NEW_LINE for x , y in e : NEW_LINE INDENT u . unite ( x - 1 , y - 1 ) NEW_LINE DEDENT print ( sum ( i < 0 for i in u . table ) - 1 ) NEW_LINE
|
T32 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; UnionFindTree uft = new UnionFindTree ( n ) ; for ( int i = 0 ; i < m ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; uft . union ( a - 1 , b - 1 ) ; } System . out . println ( uft . countBlock ( ) - 1 ) ; } static class UnionFindTree { int [ ] parent ; int [ ] rank ; public UnionFindTree ( int size ) { parent = new int [ size ] ; rank = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { parent [ i ] = i ; } } public int find ( int x ) { if ( parent [ x ] == x ) { return x ; } else { int v = find ( parent [ x ] ) ; parent [ x ] = v ; return v ; } } public boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } public void union ( int x , int y ) { int rootX = find ( x ) ; int rootY = find ( y ) ; if ( rank [ rootX ] > rank [ rootY ] ) { parent [ rootY ] = rootX ; } else if ( rank [ rootX ] < rank [ rootY ] ) { parent [ rootX ] = rootY ; } else if ( rootX != rootY ) { parent [ rootX ] = rootY ; rank [ rootX ] ++ ; } } public int countBlock ( ) { int count = 0 ; for ( int i = 0 ; i < parent . length ; i ++ ) { if ( parent [ i ] == i ) { count ++ ; } } return count ; } } }
| from collections import defaultdict NEW_LINE import sys NEW_LINE sys . setrecursionlimit ( sys . getrecursionlimit ( ) * 10000 ) NEW_LINE N , M = map ( int , input ( ) . split ( ) ) NEW_LINE g = defaultdict ( list ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE g [ a ] . append ( b ) NEW_LINE g [ b ] . append ( a ) NEW_LINE DEDENT visited = set ( ) NEW_LINE def dfs ( v ) : NEW_LINE INDENT visited . add ( v ) NEW_LINE for c in g [ v ] : NEW_LINE INDENT if not c in visited : NEW_LINE INDENT dfs ( c ) NEW_LINE DEDENT DEDENT DEDENT connected_component = 0 NEW_LINE for v in range ( 1 , N + 1 ) : NEW_LINE INDENT if not v in visited : NEW_LINE INDENT dfs ( v ) NEW_LINE connected_component += 1 NEW_LINE DEDENT DEDENT print ( connected_component - 1 ) NEW_LINE
|
T33 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; UnionFindTree uft = new UnionFindTree ( n ) ; for ( int i = 0 ; i < m ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; uft . union ( a - 1 , b - 1 ) ; } System . out . println ( uft . countBlock ( ) - 1 ) ; } static class UnionFindTree { int [ ] parent ; int [ ] rank ; public UnionFindTree ( int size ) { parent = new int [ size ] ; rank = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { parent [ i ] = i ; } } public int find ( int x ) { if ( parent [ x ] == x ) { return x ; } else { int v = find ( parent [ x ] ) ; parent [ x ] = v ; return v ; } } public boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } public void union ( int x , int y ) { int rootX = find ( x ) ; int rootY = find ( y ) ; if ( rank [ rootX ] > rank [ rootY ] ) { parent [ rootY ] = rootX ; } else if ( rank [ rootX ] < rank [ rootY ] ) { parent [ rootX ] = rootY ; } else if ( rootX != rootY ) { parent [ rootX ] = rootY ; rank [ rootX ] ++ ; } } public int countBlock ( ) { int count = 0 ; for ( int i = 0 ; i < parent . length ; i ++ ) { if ( parent [ i ] == i ) { count ++ ; } } return count ; } } }
| def b_construction ( N , M , Road ) : NEW_LINE INDENT class UnionFind ( object ) : NEW_LINE INDENT def __init__ ( self , N ) : NEW_LINE INDENT self . p = list ( range ( N ) ) NEW_LINE self . rank = [ 0 ] * N NEW_LINE self . size = [ 1 ] * N NEW_LINE DEDENT def union ( self , x , y ) : NEW_LINE INDENT u = self . find ( x ) NEW_LINE v = self . find ( y ) NEW_LINE if u == v : NEW_LINE INDENT return NEW_LINE DEDENT if self . rank [ u ] < self . rank [ v ] : NEW_LINE INDENT self . p [ u ] = v NEW_LINE self . size [ v ] += self . size [ u ] NEW_LINE self . size [ u ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT self . p [ v ] = u NEW_LINE self . size [ u ] += self . size [ v ] NEW_LINE self . size [ v ] = 0 NEW_LINE if self . rank [ u ] == self . rank [ v ] : NEW_LINE INDENT self . rank [ u ] += 1 NEW_LINE DEDENT DEDENT DEDENT def find ( self , x ) : NEW_LINE INDENT if self . p [ x ] != x : NEW_LINE INDENT self . p [ x ] = self . find ( self . p [ x ] ) NEW_LINE DEDENT return self . p [ x ] NEW_LINE DEDENT def __str__ ( self ) : NEW_LINE INDENT return ' [ { } ] ' . format ( ' , ▁ ' . join ( map ( str , self . p ) ) ) NEW_LINE DEDENT DEDENT uf = UnionFind ( N ) NEW_LINE for a , b in Road : NEW_LINE INDENT uf . union ( a - 1 , b - 1 ) NEW_LINE DEDENT ans = len ( [ 1 for v in range ( N ) if uf . find ( v ) == v ] ) - 1 NEW_LINE return ans NEW_LINE DEDENT N , M = [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE Road = [ [ int ( i ) for i in input ( ) . split ( ) ] for j in range ( M ) ] NEW_LINE print ( b_construction ( N , M , Road ) ) NEW_LINE
|
T34 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; UnionFindTree uft = new UnionFindTree ( n ) ; for ( int i = 0 ; i < m ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; uft . union ( a - 1 , b - 1 ) ; } System . out . println ( uft . countBlock ( ) - 1 ) ; } static class UnionFindTree { int [ ] parent ; int [ ] rank ; public UnionFindTree ( int size ) { parent = new int [ size ] ; rank = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { parent [ i ] = i ; } } public int find ( int x ) { if ( parent [ x ] == x ) { return x ; } else { int v = find ( parent [ x ] ) ; parent [ x ] = v ; return v ; } } public boolean same ( int x , int y ) { return find ( x ) == find ( y ) ; } public void union ( int x , int y ) { int rootX = find ( x ) ; int rootY = find ( y ) ; if ( rank [ rootX ] > rank [ rootY ] ) { parent [ rootY ] = rootX ; } else if ( rank [ rootX ] < rank [ rootY ] ) { parent [ rootX ] = rootY ; } else if ( rootX != rootY ) { parent [ rootX ] = rootY ; rank [ rootX ] ++ ; } } public int countBlock ( ) { int count = 0 ; for ( int i = 0 ; i < parent . length ; i ++ ) { if ( parent [ i ] == i ) { count ++ ; } } return count ; } } }
| class DisjointSet : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . rank = { } NEW_LINE self . p = { } NEW_LINE DEDENT def makeSet ( self , x ) : NEW_LINE INDENT self . p [ x ] = x NEW_LINE self . rank [ x ] = 0 NEW_LINE DEDENT def same ( self , x , y ) : NEW_LINE INDENT return self . findSet ( x ) == self . findSet ( y ) NEW_LINE DEDENT def unite ( self , x , y ) : NEW_LINE INDENT self . link ( self . findSet ( x ) , self . findSet ( y ) ) NEW_LINE DEDENT def link ( self , x , y ) : NEW_LINE INDENT if x == y : NEW_LINE INDENT return NEW_LINE DEDENT if ( self . rank [ x ] > self . rank [ y ] ) : NEW_LINE INDENT self . p [ y ] = x NEW_LINE DEDENT else : NEW_LINE INDENT self . p [ x ] = y NEW_LINE if ( self . rank [ x ] == self . rank [ y ] ) : NEW_LINE INDENT self . rank [ y ] += 1 NEW_LINE DEDENT DEDENT DEDENT def findSet ( self , x ) : NEW_LINE INDENT if ( x != self . p [ x ] ) : NEW_LINE INDENT self . p [ x ] = self . findSet ( self . p [ x ] ) NEW_LINE DEDENT return self . p [ x ] NEW_LINE DEDENT DEDENT N , M = map ( int , input ( ) . split ( ) ) NEW_LINE ds = DisjointSet ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT ds . makeSet ( i ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE ds . unite ( a , b ) NEW_LINE DEDENT connected_component = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if i == ds . findSet ( i ) : NEW_LINE INDENT connected_component += 1 NEW_LINE DEDENT DEDENT print ( connected_component - 1 ) NEW_LINE
|
T35 | import java . util . * ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; String n = sc . next ( ) , m = sc . next ( ) ; if ( n . length ( ) > m . length ( ) ) System . out . println ( n ) ; else System . out . println ( m ) ; } }
| s1 = input ( ) NEW_LINE s2 = input ( ) NEW_LINE if len ( s1 ) > len ( s2 ) : NEW_LINE INDENT print ( s1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( s2 ) NEW_LINE DEDENT
|
T36 | import java . util . * ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; String n = sc . next ( ) , m = sc . next ( ) ; if ( n . length ( ) > m . length ( ) ) System . out . println ( n ) ; else System . out . println ( m ) ; } }
| print ( max ( open ( 0 ) , key = len ) ) NEW_LINE
|
T37 | import java . util . * ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; String n = sc . next ( ) , m = sc . next ( ) ; if ( n . length ( ) > m . length ( ) ) System . out . println ( n ) ; else System . out . println ( m ) ; } }
| a = input ( ) NEW_LINE b = input ( ) NEW_LINE print ( a if len ( a ) >= len ( b ) else b ) NEW_LINE
|
T38 | import java . util . * ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; String n = sc . next ( ) , m = sc . next ( ) ; if ( n . length ( ) > m . length ( ) ) System . out . println ( n ) ; else System . out . println ( m ) ; } }
| A = input ( ) NEW_LINE B = input ( ) NEW_LINE if A . islower ( ) and B . islower ( ) : NEW_LINE INDENT C = len ( A ) NEW_LINE D = len ( B ) NEW_LINE if C > D : NEW_LINE INDENT print ( A ) NEW_LINE DEDENT elif D > C : NEW_LINE INDENT print ( B ) NEW_LINE DEDENT DEDENT
|
T39 | import java . util . * ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; String n = sc . next ( ) , m = sc . next ( ) ; if ( n . length ( ) > m . length ( ) ) System . out . println ( n ) ; else System . out . println ( m ) ; } }
| print ( max ( input ( ) , input ( ) , key = len ) ) NEW_LINE
|
T40 | import java . io . OutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . InputMismatchException ; import java . io . IOException ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; FastScanner in = new FastScanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , FastScanner in , PrintWriter out ) { String s = in . next ( ) ; String t = in . next ( ) ; out . println ( s . length ( ) > t . length ( ) ? s : t ) ; } } static class FastScanner { private InputStream in ; private byte [ ] buffer = new byte [ 1024 ] ; private int bufPointer ; private int bufLength ; public FastScanner ( InputStream in ) { this . in = in ; this . bufPointer = 0 ; this . bufLength = 0 ; } private int readByte ( ) { if ( bufPointer >= bufLength ) { if ( bufLength == - 1 ) throw new InputMismatchException ( ) ; bufPointer = 0 ; try { bufLength = in . read ( buffer ) ; } catch ( IOException e ) { throw new InputMismatchException ( ) ; } if ( bufLength <= 0 ) return - 1 ; } return buffer [ bufPointer ++ ] ; } private static boolean isPrintableChar ( int c ) { return c >= 33 && c <= 126 ; } public String next ( ) { StringBuilder sb = new StringBuilder ( ) ; int b = readByte ( ) ; while ( ! isPrintableChar ( b ) ) b = readByte ( ) ; while ( isPrintableChar ( b ) ) { sb . appendCodePoint ( b ) ; b = readByte ( ) ; } return sb . toString ( ) ; } } }
| s1 = input ( ) NEW_LINE s2 = input ( ) NEW_LINE if len ( s1 ) > len ( s2 ) : NEW_LINE INDENT print ( s1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( s2 ) NEW_LINE DEDENT
|
T41 | import java . io . OutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . InputMismatchException ; import java . io . IOException ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; FastScanner in = new FastScanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , FastScanner in , PrintWriter out ) { String s = in . next ( ) ; String t = in . next ( ) ; out . println ( s . length ( ) > t . length ( ) ? s : t ) ; } } static class FastScanner { private InputStream in ; private byte [ ] buffer = new byte [ 1024 ] ; private int bufPointer ; private int bufLength ; public FastScanner ( InputStream in ) { this . in = in ; this . bufPointer = 0 ; this . bufLength = 0 ; } private int readByte ( ) { if ( bufPointer >= bufLength ) { if ( bufLength == - 1 ) throw new InputMismatchException ( ) ; bufPointer = 0 ; try { bufLength = in . read ( buffer ) ; } catch ( IOException e ) { throw new InputMismatchException ( ) ; } if ( bufLength <= 0 ) return - 1 ; } return buffer [ bufPointer ++ ] ; } private static boolean isPrintableChar ( int c ) { return c >= 33 && c <= 126 ; } public String next ( ) { StringBuilder sb = new StringBuilder ( ) ; int b = readByte ( ) ; while ( ! isPrintableChar ( b ) ) b = readByte ( ) ; while ( isPrintableChar ( b ) ) { sb . appendCodePoint ( b ) ; b = readByte ( ) ; } return sb . toString ( ) ; } } }
| print ( max ( open ( 0 ) , key = len ) ) NEW_LINE
|
T42 | import java . io . OutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . InputMismatchException ; import java . io . IOException ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; FastScanner in = new FastScanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , FastScanner in , PrintWriter out ) { String s = in . next ( ) ; String t = in . next ( ) ; out . println ( s . length ( ) > t . length ( ) ? s : t ) ; } } static class FastScanner { private InputStream in ; private byte [ ] buffer = new byte [ 1024 ] ; private int bufPointer ; private int bufLength ; public FastScanner ( InputStream in ) { this . in = in ; this . bufPointer = 0 ; this . bufLength = 0 ; } private int readByte ( ) { if ( bufPointer >= bufLength ) { if ( bufLength == - 1 ) throw new InputMismatchException ( ) ; bufPointer = 0 ; try { bufLength = in . read ( buffer ) ; } catch ( IOException e ) { throw new InputMismatchException ( ) ; } if ( bufLength <= 0 ) return - 1 ; } return buffer [ bufPointer ++ ] ; } private static boolean isPrintableChar ( int c ) { return c >= 33 && c <= 126 ; } public String next ( ) { StringBuilder sb = new StringBuilder ( ) ; int b = readByte ( ) ; while ( ! isPrintableChar ( b ) ) b = readByte ( ) ; while ( isPrintableChar ( b ) ) { sb . appendCodePoint ( b ) ; b = readByte ( ) ; } return sb . toString ( ) ; } } }
| a = input ( ) NEW_LINE b = input ( ) NEW_LINE print ( a if len ( a ) >= len ( b ) else b ) NEW_LINE
|
T43 | import java . io . OutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . InputMismatchException ; import java . io . IOException ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; FastScanner in = new FastScanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , FastScanner in , PrintWriter out ) { String s = in . next ( ) ; String t = in . next ( ) ; out . println ( s . length ( ) > t . length ( ) ? s : t ) ; } } static class FastScanner { private InputStream in ; private byte [ ] buffer = new byte [ 1024 ] ; private int bufPointer ; private int bufLength ; public FastScanner ( InputStream in ) { this . in = in ; this . bufPointer = 0 ; this . bufLength = 0 ; } private int readByte ( ) { if ( bufPointer >= bufLength ) { if ( bufLength == - 1 ) throw new InputMismatchException ( ) ; bufPointer = 0 ; try { bufLength = in . read ( buffer ) ; } catch ( IOException e ) { throw new InputMismatchException ( ) ; } if ( bufLength <= 0 ) return - 1 ; } return buffer [ bufPointer ++ ] ; } private static boolean isPrintableChar ( int c ) { return c >= 33 && c <= 126 ; } public String next ( ) { StringBuilder sb = new StringBuilder ( ) ; int b = readByte ( ) ; while ( ! isPrintableChar ( b ) ) b = readByte ( ) ; while ( isPrintableChar ( b ) ) { sb . appendCodePoint ( b ) ; b = readByte ( ) ; } return sb . toString ( ) ; } } }
| A = input ( ) NEW_LINE B = input ( ) NEW_LINE if A . islower ( ) and B . islower ( ) : NEW_LINE INDENT C = len ( A ) NEW_LINE D = len ( B ) NEW_LINE if C > D : NEW_LINE INDENT print ( A ) NEW_LINE DEDENT elif D > C : NEW_LINE INDENT print ( B ) NEW_LINE DEDENT DEDENT
|
T44 | import java . io . OutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . InputMismatchException ; import java . io . IOException ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; FastScanner in = new FastScanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , FastScanner in , PrintWriter out ) { String s = in . next ( ) ; String t = in . next ( ) ; out . println ( s . length ( ) > t . length ( ) ? s : t ) ; } } static class FastScanner { private InputStream in ; private byte [ ] buffer = new byte [ 1024 ] ; private int bufPointer ; private int bufLength ; public FastScanner ( InputStream in ) { this . in = in ; this . bufPointer = 0 ; this . bufLength = 0 ; } private int readByte ( ) { if ( bufPointer >= bufLength ) { if ( bufLength == - 1 ) throw new InputMismatchException ( ) ; bufPointer = 0 ; try { bufLength = in . read ( buffer ) ; } catch ( IOException e ) { throw new InputMismatchException ( ) ; } if ( bufLength <= 0 ) return - 1 ; } return buffer [ bufPointer ++ ] ; } private static boolean isPrintableChar ( int c ) { return c >= 33 && c <= 126 ; } public String next ( ) { StringBuilder sb = new StringBuilder ( ) ; int b = readByte ( ) ; while ( ! isPrintableChar ( b ) ) b = readByte ( ) ; while ( isPrintableChar ( b ) ) { sb . appendCodePoint ( b ) ; b = readByte ( ) ; } return sb . toString ( ) ; } } }
| print ( max ( input ( ) , input ( ) , key = len ) ) NEW_LINE
|
T45 | import java . io . * ; import java . util . * ; public class Main { public static void main ( String [ ] args ) { MyScanner sc = new MyScanner ( ) ; String s = sc . next ( ) ; String s1 = sc . next ( ) ; int a = s . length ( ) ; int b = s1 . length ( ) ; System . out . println ( a > b ? s : s1 ) ; } static int l_min ( int [ ] a ) { Arrays . sort ( a ) ; return a [ 0 ] ; } static int l_max ( int [ ] a ) { int l = a . length ; Arrays . sort ( a ) ; return a [ l - 1 ] ; } public static PrintWriter out ; public static class MyScanner { BufferedReader br ; StringTokenizer st ; public MyScanner ( ) { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; } String next ( ) { while ( st == null || ! st . hasMoreElements ( ) ) { try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } return st . nextToken ( ) ; } int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } long nextLong ( ) { return Long . parseLong ( next ( ) ) ; } double nextDouble ( ) { return Double . parseDouble ( next ( ) ) ; } String nextLine ( ) { String str = " " ; try { str = br . readLine ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } return str ; } } }
| s1 = input ( ) NEW_LINE s2 = input ( ) NEW_LINE if len ( s1 ) > len ( s2 ) : NEW_LINE INDENT print ( s1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( s2 ) NEW_LINE DEDENT
|
T46 | import java . io . * ; import java . util . * ; public class Main { public static void main ( String [ ] args ) { MyScanner sc = new MyScanner ( ) ; String s = sc . next ( ) ; String s1 = sc . next ( ) ; int a = s . length ( ) ; int b = s1 . length ( ) ; System . out . println ( a > b ? s : s1 ) ; } static int l_min ( int [ ] a ) { Arrays . sort ( a ) ; return a [ 0 ] ; } static int l_max ( int [ ] a ) { int l = a . length ; Arrays . sort ( a ) ; return a [ l - 1 ] ; } public static PrintWriter out ; public static class MyScanner { BufferedReader br ; StringTokenizer st ; public MyScanner ( ) { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; } String next ( ) { while ( st == null || ! st . hasMoreElements ( ) ) { try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } return st . nextToken ( ) ; } int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } long nextLong ( ) { return Long . parseLong ( next ( ) ) ; } double nextDouble ( ) { return Double . parseDouble ( next ( ) ) ; } String nextLine ( ) { String str = " " ; try { str = br . readLine ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } return str ; } } }
| print ( max ( open ( 0 ) , key = len ) ) NEW_LINE
|
T47 | import java . io . * ; import java . util . * ; public class Main { public static void main ( String [ ] args ) { MyScanner sc = new MyScanner ( ) ; String s = sc . next ( ) ; String s1 = sc . next ( ) ; int a = s . length ( ) ; int b = s1 . length ( ) ; System . out . println ( a > b ? s : s1 ) ; } static int l_min ( int [ ] a ) { Arrays . sort ( a ) ; return a [ 0 ] ; } static int l_max ( int [ ] a ) { int l = a . length ; Arrays . sort ( a ) ; return a [ l - 1 ] ; } public static PrintWriter out ; public static class MyScanner { BufferedReader br ; StringTokenizer st ; public MyScanner ( ) { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; } String next ( ) { while ( st == null || ! st . hasMoreElements ( ) ) { try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } return st . nextToken ( ) ; } int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } long nextLong ( ) { return Long . parseLong ( next ( ) ) ; } double nextDouble ( ) { return Double . parseDouble ( next ( ) ) ; } String nextLine ( ) { String str = " " ; try { str = br . readLine ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } return str ; } } }
| a = input ( ) NEW_LINE b = input ( ) NEW_LINE print ( a if len ( a ) >= len ( b ) else b ) NEW_LINE
|
T48 | import java . io . * ; import java . util . * ; public class Main { public static void main ( String [ ] args ) { MyScanner sc = new MyScanner ( ) ; String s = sc . next ( ) ; String s1 = sc . next ( ) ; int a = s . length ( ) ; int b = s1 . length ( ) ; System . out . println ( a > b ? s : s1 ) ; } static int l_min ( int [ ] a ) { Arrays . sort ( a ) ; return a [ 0 ] ; } static int l_max ( int [ ] a ) { int l = a . length ; Arrays . sort ( a ) ; return a [ l - 1 ] ; } public static PrintWriter out ; public static class MyScanner { BufferedReader br ; StringTokenizer st ; public MyScanner ( ) { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; } String next ( ) { while ( st == null || ! st . hasMoreElements ( ) ) { try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } return st . nextToken ( ) ; } int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } long nextLong ( ) { return Long . parseLong ( next ( ) ) ; } double nextDouble ( ) { return Double . parseDouble ( next ( ) ) ; } String nextLine ( ) { String str = " " ; try { str = br . readLine ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } return str ; } } }
| A = input ( ) NEW_LINE B = input ( ) NEW_LINE if A . islower ( ) and B . islower ( ) : NEW_LINE INDENT C = len ( A ) NEW_LINE D = len ( B ) NEW_LINE if C > D : NEW_LINE INDENT print ( A ) NEW_LINE DEDENT elif D > C : NEW_LINE INDENT print ( B ) NEW_LINE DEDENT DEDENT
|
T49 | import java . io . * ; import java . util . * ; public class Main { public static void main ( String [ ] args ) { MyScanner sc = new MyScanner ( ) ; String s = sc . next ( ) ; String s1 = sc . next ( ) ; int a = s . length ( ) ; int b = s1 . length ( ) ; System . out . println ( a > b ? s : s1 ) ; } static int l_min ( int [ ] a ) { Arrays . sort ( a ) ; return a [ 0 ] ; } static int l_max ( int [ ] a ) { int l = a . length ; Arrays . sort ( a ) ; return a [ l - 1 ] ; } public static PrintWriter out ; public static class MyScanner { BufferedReader br ; StringTokenizer st ; public MyScanner ( ) { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; } String next ( ) { while ( st == null || ! st . hasMoreElements ( ) ) { try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } return st . nextToken ( ) ; } int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } long nextLong ( ) { return Long . parseLong ( next ( ) ) ; } double nextDouble ( ) { return Double . parseDouble ( next ( ) ) ; } String nextLine ( ) { String str = " " ; try { str = br . readLine ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } return str ; } } }
| print ( max ( input ( ) , input ( ) , key = len ) ) NEW_LINE
|
T50 | import java . util . * ; import java . util . stream . * ; import static java . lang . System . * ; class Main { static Scanner sc = new Scanner ( System . in ) ; static int nextInt ( ) { return Integer . parseInt ( sc . next ( ) ) ; } static int [ ] nextIntArray ( int n ) { return IntStream . range ( 0 , n ) . map ( i -> nextInt ( ) ) . toArray ( ) ; } static int max ( int ... ar ) { Arrays . sort ( ar ) ; return ar [ ar . length - 1 ] ; } static int min ( int ... ar ) { Arrays . sort ( ar ) ; return ar [ 0 ] ; } static int maxInt = Integer . MAX_VALUE ; static int minInt = Integer . MIN_VALUE ; public static void main ( String [ ] args ) { String a = sc . next ( ) , b = sc . next ( ) ; out . println ( a . length ( ) > b . length ( ) ? a : b ) ; } }
| s1 = input ( ) NEW_LINE s2 = input ( ) NEW_LINE if len ( s1 ) > len ( s2 ) : NEW_LINE INDENT print ( s1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( s2 ) NEW_LINE DEDENT
|
T51 | import java . util . * ; import java . util . stream . * ; import static java . lang . System . * ; class Main { static Scanner sc = new Scanner ( System . in ) ; static int nextInt ( ) { return Integer . parseInt ( sc . next ( ) ) ; } static int [ ] nextIntArray ( int n ) { return IntStream . range ( 0 , n ) . map ( i -> nextInt ( ) ) . toArray ( ) ; } static int max ( int ... ar ) { Arrays . sort ( ar ) ; return ar [ ar . length - 1 ] ; } static int min ( int ... ar ) { Arrays . sort ( ar ) ; return ar [ 0 ] ; } static int maxInt = Integer . MAX_VALUE ; static int minInt = Integer . MIN_VALUE ; public static void main ( String [ ] args ) { String a = sc . next ( ) , b = sc . next ( ) ; out . println ( a . length ( ) > b . length ( ) ? a : b ) ; } }
| print ( max ( open ( 0 ) , key = len ) ) NEW_LINE
|
T52 | import java . util . * ; import java . util . stream . * ; import static java . lang . System . * ; class Main { static Scanner sc = new Scanner ( System . in ) ; static int nextInt ( ) { return Integer . parseInt ( sc . next ( ) ) ; } static int [ ] nextIntArray ( int n ) { return IntStream . range ( 0 , n ) . map ( i -> nextInt ( ) ) . toArray ( ) ; } static int max ( int ... ar ) { Arrays . sort ( ar ) ; return ar [ ar . length - 1 ] ; } static int min ( int ... ar ) { Arrays . sort ( ar ) ; return ar [ 0 ] ; } static int maxInt = Integer . MAX_VALUE ; static int minInt = Integer . MIN_VALUE ; public static void main ( String [ ] args ) { String a = sc . next ( ) , b = sc . next ( ) ; out . println ( a . length ( ) > b . length ( ) ? a : b ) ; } }
| a = input ( ) NEW_LINE b = input ( ) NEW_LINE print ( a if len ( a ) >= len ( b ) else b ) NEW_LINE
|
T53 | import java . util . * ; import java . util . stream . * ; import static java . lang . System . * ; class Main { static Scanner sc = new Scanner ( System . in ) ; static int nextInt ( ) { return Integer . parseInt ( sc . next ( ) ) ; } static int [ ] nextIntArray ( int n ) { return IntStream . range ( 0 , n ) . map ( i -> nextInt ( ) ) . toArray ( ) ; } static int max ( int ... ar ) { Arrays . sort ( ar ) ; return ar [ ar . length - 1 ] ; } static int min ( int ... ar ) { Arrays . sort ( ar ) ; return ar [ 0 ] ; } static int maxInt = Integer . MAX_VALUE ; static int minInt = Integer . MIN_VALUE ; public static void main ( String [ ] args ) { String a = sc . next ( ) , b = sc . next ( ) ; out . println ( a . length ( ) > b . length ( ) ? a : b ) ; } }
| A = input ( ) NEW_LINE B = input ( ) NEW_LINE if A . islower ( ) and B . islower ( ) : NEW_LINE INDENT C = len ( A ) NEW_LINE D = len ( B ) NEW_LINE if C > D : NEW_LINE INDENT print ( A ) NEW_LINE DEDENT elif D > C : NEW_LINE INDENT print ( B ) NEW_LINE DEDENT DEDENT
|
T54 | import java . util . * ; import java . util . stream . * ; import static java . lang . System . * ; class Main { static Scanner sc = new Scanner ( System . in ) ; static int nextInt ( ) { return Integer . parseInt ( sc . next ( ) ) ; } static int [ ] nextIntArray ( int n ) { return IntStream . range ( 0 , n ) . map ( i -> nextInt ( ) ) . toArray ( ) ; } static int max ( int ... ar ) { Arrays . sort ( ar ) ; return ar [ ar . length - 1 ] ; } static int min ( int ... ar ) { Arrays . sort ( ar ) ; return ar [ 0 ] ; } static int maxInt = Integer . MAX_VALUE ; static int minInt = Integer . MIN_VALUE ; public static void main ( String [ ] args ) { String a = sc . next ( ) , b = sc . next ( ) ; out . println ( a . length ( ) > b . length ( ) ? a : b ) ; } }
| print ( max ( input ( ) , input ( ) , key = len ) ) NEW_LINE
|
T55 | import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . OutputStream ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; InputReader in = new InputReader ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; Task solver = new Task ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class Task { public void solve ( int testNumber , InputReader in , PrintWriter out ) { String A = in . next ( ) ; String B = in . next ( ) ; if ( A . length ( ) > B . length ( ) ) { out . println ( A ) ; } else { out . println ( B ) ; } } } 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 ( ) ) ; } } }
| s1 = input ( ) NEW_LINE s2 = input ( ) NEW_LINE if len ( s1 ) > len ( s2 ) : NEW_LINE INDENT print ( s1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( s2 ) NEW_LINE DEDENT
|
T56 | import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . OutputStream ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; InputReader in = new InputReader ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; Task solver = new Task ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class Task { public void solve ( int testNumber , InputReader in , PrintWriter out ) { String A = in . next ( ) ; String B = in . next ( ) ; if ( A . length ( ) > B . length ( ) ) { out . println ( A ) ; } else { out . println ( B ) ; } } } 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 ( ) ) ; } } }
| print ( max ( open ( 0 ) , key = len ) ) NEW_LINE
|
T57 | import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . OutputStream ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; InputReader in = new InputReader ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; Task solver = new Task ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class Task { public void solve ( int testNumber , InputReader in , PrintWriter out ) { String A = in . next ( ) ; String B = in . next ( ) ; if ( A . length ( ) > B . length ( ) ) { out . println ( A ) ; } else { out . println ( B ) ; } } } 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 ( ) ) ; } } }
| a = input ( ) NEW_LINE b = input ( ) NEW_LINE print ( a if len ( a ) >= len ( b ) else b ) NEW_LINE
|
T58 | import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . OutputStream ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; InputReader in = new InputReader ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; Task solver = new Task ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class Task { public void solve ( int testNumber , InputReader in , PrintWriter out ) { String A = in . next ( ) ; String B = in . next ( ) ; if ( A . length ( ) > B . length ( ) ) { out . println ( A ) ; } else { out . println ( B ) ; } } } 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 ( ) ) ; } } }
| A = input ( ) NEW_LINE B = input ( ) NEW_LINE if A . islower ( ) and B . islower ( ) : NEW_LINE INDENT C = len ( A ) NEW_LINE D = len ( B ) NEW_LINE if C > D : NEW_LINE INDENT print ( A ) NEW_LINE DEDENT elif D > C : NEW_LINE INDENT print ( B ) NEW_LINE DEDENT DEDENT
|
T59 | import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . OutputStream ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; InputReader in = new InputReader ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; Task solver = new Task ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class Task { public void solve ( int testNumber , InputReader in , PrintWriter out ) { String A = in . next ( ) ; String B = in . next ( ) ; if ( A . length ( ) > B . length ( ) ) { out . println ( A ) ; } else { out . println ( B ) ; } } } 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 ( ) ) ; } } }
| print ( max ( input ( ) , input ( ) , key = len ) ) NEW_LINE
|
T60 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = sc . nextInt ( ) ; } sc . close ( ) ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; Arrays . sort ( c ) ; long ans = 0 ; long [ ] cnum = new long [ n + 1 ] ; int pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( c [ pos ] <= b [ i ] ) { pos ++ ; if ( pos >= n ) { pos = n ; break ; } } if ( pos >= n ) break ; cnum [ i ] = n - pos ; } for ( int i = 1 ; i < n ; i ++ ) { cnum [ n - i - 1 ] = cnum [ n - i ] + cnum [ n - i - 1 ] ; } pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( b [ pos ] <= a [ i ] ) { pos ++ ; if ( pos >= n ) { pos = n ; break ; } } if ( pos >= n ) break ; ans += cnum [ pos ] ; } System . out . println ( ans ) ; } }
| N = int ( input ( ) ) NEW_LINE a = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE b = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE c = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE sorted_a = sorted ( a ) NEW_LINE sorted_b = sorted ( b ) NEW_LINE sorted_c = sorted ( c ) NEW_LINE ans = 0 NEW_LINE before_a = 0 NEW_LINE before_c = 0 NEW_LINE count_a = 0 NEW_LINE count_c_rest = 0 NEW_LINE for each_b in sorted_b : NEW_LINE INDENT for ai in range ( before_a , len ( sorted_a ) ) : NEW_LINE INDENT if each_b > sorted_a [ ai ] : NEW_LINE INDENT count_a += 1 NEW_LINE before_a += 1 NEW_LINE DEDENT else : NEW_LINE INDENT before_a = ai NEW_LINE break NEW_LINE DEDENT DEDENT for ci in range ( before_c , len ( sorted_c ) ) : NEW_LINE INDENT if sorted_c [ ci ] <= each_b : NEW_LINE INDENT count_c_rest += 1 NEW_LINE before_c += 1 NEW_LINE DEDENT else : NEW_LINE INDENT before_c = ci NEW_LINE break NEW_LINE DEDENT DEDENT ans += count_a * ( len ( sorted_c ) - count_c_rest ) NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T61 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = sc . nextInt ( ) ; } sc . close ( ) ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; Arrays . sort ( c ) ; long ans = 0 ; long [ ] cnum = new long [ n + 1 ] ; int pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( c [ pos ] <= b [ i ] ) { pos ++ ; if ( pos >= n ) { pos = n ; break ; } } if ( pos >= n ) break ; cnum [ i ] = n - pos ; } for ( int i = 1 ; i < n ; i ++ ) { cnum [ n - i - 1 ] = cnum [ n - i ] + cnum [ n - i - 1 ] ; } pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( b [ pos ] <= a [ i ] ) { pos ++ ; if ( pos >= n ) { pos = n ; break ; } } if ( pos >= n ) break ; ans += cnum [ pos ] ; } System . out . println ( ans ) ; } }
| def inpl ( ) : return [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE import bisect NEW_LINE N = int ( input ( ) ) NEW_LINE A = sorted ( inpl ( ) ) NEW_LINE B = inpl ( ) NEW_LINE C = sorted ( inpl ( ) ) NEW_LINE ans = 0 NEW_LINE for b in B : NEW_LINE INDENT ans += bisect . bisect_left ( A , b ) * ( N - bisect . bisect_right ( C , b ) ) NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T62 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = sc . nextInt ( ) ; } sc . close ( ) ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; Arrays . sort ( c ) ; long ans = 0 ; long [ ] cnum = new long [ n + 1 ] ; int pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( c [ pos ] <= b [ i ] ) { pos ++ ; if ( pos >= n ) { pos = n ; break ; } } if ( pos >= n ) break ; cnum [ i ] = n - pos ; } for ( int i = 1 ; i < n ; i ++ ) { cnum [ n - i - 1 ] = cnum [ n - i ] + cnum [ n - i - 1 ] ; } pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( b [ pos ] <= a [ i ] ) { pos ++ ; if ( pos >= n ) { pos = n ; break ; } } if ( pos >= n ) break ; ans += cnum [ pos ] ; } System . out . println ( ans ) ; } }
| II = lambda : int ( input ( ) ) NEW_LINE MI = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE def main ( ) : NEW_LINE INDENT N = II ( ) NEW_LINE A , B , C = sorted ( list ( MI ( ) ) ) , sorted ( list ( MI ( ) ) ) , sorted ( list ( MI ( ) ) ) NEW_LINE s = [ i for i in range ( 1 , N + 1 ) ] NEW_LINE ia = N - 1 NEW_LINE ns = [ None ] * N NEW_LINE for ib in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ia >= 0 and A [ ia ] >= B [ ib ] : NEW_LINE INDENT ia -= 1 NEW_LINE DEDENT if ia >= 0 : NEW_LINE INDENT ns [ ib ] = s [ ia ] NEW_LINE DEDENT else : NEW_LINE INDENT ns [ ib ] = 0 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT ns [ i ] += ns [ i - 1 ] NEW_LINE DEDENT s = ns NEW_LINE ib = N - 1 NEW_LINE ns = [ None ] * N NEW_LINE for ic in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ib >= 0 and B [ ib ] >= C [ ic ] : NEW_LINE INDENT ib -= 1 NEW_LINE DEDENT if ib >= 0 : NEW_LINE INDENT ns [ ic ] = s [ ib ] NEW_LINE DEDENT else : NEW_LINE INDENT ns [ ic ] = 0 NEW_LINE DEDENT DEDENT return sum ( ns ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( main ( ) ) NEW_LINE DEDENT
|
T63 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = sc . nextInt ( ) ; } sc . close ( ) ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; Arrays . sort ( c ) ; long ans = 0 ; long [ ] cnum = new long [ n + 1 ] ; int pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( c [ pos ] <= b [ i ] ) { pos ++ ; if ( pos >= n ) { pos = n ; break ; } } if ( pos >= n ) break ; cnum [ i ] = n - pos ; } for ( int i = 1 ; i < n ; i ++ ) { cnum [ n - i - 1 ] = cnum [ n - i ] + cnum [ n - i - 1 ] ; } pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( b [ pos ] <= a [ i ] ) { pos ++ ; if ( pos >= n ) { pos = n ; break ; } } if ( pos >= n ) break ; ans += cnum [ pos ] ; } System . out . println ( ans ) ; } }
| from itertools import accumulate NEW_LINE N = int ( input ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE B = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE C = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE C = sorted ( C ) NEW_LINE acc_a = [ 0 ] * N NEW_LINE acc_c = [ 0 ] * N NEW_LINE i_a = 0 NEW_LINE i_c = N - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT b = B [ i ] NEW_LINE while i_a < N and A [ i_a ] < b : NEW_LINE INDENT acc_a [ i ] += 1 NEW_LINE i_a += 1 NEW_LINE DEDENT DEDENT for i in reversed ( range ( N ) ) : NEW_LINE INDENT b = B [ i ] NEW_LINE while i_c >= 0 and C [ i_c ] > b : NEW_LINE INDENT acc_c [ i ] += 1 NEW_LINE i_c -= 1 NEW_LINE DEDENT DEDENT acc_a = list ( accumulate ( acc_a ) ) NEW_LINE acc_c = list ( accumulate ( acc_c [ : : - 1 ] ) ) NEW_LINE acc_c = acc_c [ : : - 1 ] NEW_LINE ans = 0 NEW_LINE for a , c in zip ( acc_a , acc_c ) : NEW_LINE INDENT ans += a * c NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T64 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = sc . nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = sc . nextInt ( ) ; } sc . close ( ) ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; Arrays . sort ( c ) ; long ans = 0 ; long [ ] cnum = new long [ n + 1 ] ; int pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( c [ pos ] <= b [ i ] ) { pos ++ ; if ( pos >= n ) { pos = n ; break ; } } if ( pos >= n ) break ; cnum [ i ] = n - pos ; } for ( int i = 1 ; i < n ; i ++ ) { cnum [ n - i - 1 ] = cnum [ n - i ] + cnum [ n - i - 1 ] ; } pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( b [ pos ] <= a [ i ] ) { pos ++ ; if ( pos >= n ) { pos = n ; break ; } } if ( pos >= n ) break ; ans += cnum [ pos ] ; } System . out . println ( ans ) ; } }
| N = int ( input ( ) ) NEW_LINE A = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE B = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE C = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE C = sorted ( C ) NEW_LINE ans = 0 NEW_LINE midnum = [ ] NEW_LINE index = 0 NEW_LINE for i in B : NEW_LINE INDENT while True : NEW_LINE INDENT if index == N or A [ index ] >= i : NEW_LINE INDENT midnum . append ( index ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT DEDENT index = 0 NEW_LINE midCnum = [ ] NEW_LINE tmp = 0 NEW_LINE for i in midnum : NEW_LINE INDENT tmp += i NEW_LINE midCnum . append ( tmp ) NEW_LINE DEDENT for i in C : NEW_LINE INDENT while True : NEW_LINE INDENT if index == N : NEW_LINE INDENT ans += midCnum [ index - 1 ] NEW_LINE break NEW_LINE DEDENT elif i <= B [ index ] : NEW_LINE INDENT if index != 0 : NEW_LINE INDENT ans += midCnum [ index - 1 ] NEW_LINE DEDENT break NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE
|
T65 | import java . util . * ; class Main { static Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int n = sc . nextInt ( ) ; Integer [ ] A = new Integer [ n ] , B = new Integer [ n ] , C = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) B [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) C [ i ] = sc . nextInt ( ) ; Arrays . sort ( A ) ; Arrays . sort ( C ) ; long ans = 0L ; for ( int i = 0 ; i < n ; i ++ ) { long a = ~ Arrays . binarySearch ( A , B [ i ] , ( x , y ) -> x . compareTo ( y ) >= 0 ? 1 : - 1 ) ; long c = n - ~ Arrays . binarySearch ( C , B [ i ] , ( x , y ) -> x . compareTo ( y ) > 0 ? 1 : - 1 ) ; ans += a * c ; } System . out . println ( ans ) ; } }
| N = int ( input ( ) ) NEW_LINE a = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE b = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE c = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE sorted_a = sorted ( a ) NEW_LINE sorted_b = sorted ( b ) NEW_LINE sorted_c = sorted ( c ) NEW_LINE ans = 0 NEW_LINE before_a = 0 NEW_LINE before_c = 0 NEW_LINE count_a = 0 NEW_LINE count_c_rest = 0 NEW_LINE for each_b in sorted_b : NEW_LINE INDENT for ai in range ( before_a , len ( sorted_a ) ) : NEW_LINE INDENT if each_b > sorted_a [ ai ] : NEW_LINE INDENT count_a += 1 NEW_LINE before_a += 1 NEW_LINE DEDENT else : NEW_LINE INDENT before_a = ai NEW_LINE break NEW_LINE DEDENT DEDENT for ci in range ( before_c , len ( sorted_c ) ) : NEW_LINE INDENT if sorted_c [ ci ] <= each_b : NEW_LINE INDENT count_c_rest += 1 NEW_LINE before_c += 1 NEW_LINE DEDENT else : NEW_LINE INDENT before_c = ci NEW_LINE break NEW_LINE DEDENT DEDENT ans += count_a * ( len ( sorted_c ) - count_c_rest ) NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T66 | import java . util . * ; class Main { static Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int n = sc . nextInt ( ) ; Integer [ ] A = new Integer [ n ] , B = new Integer [ n ] , C = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) B [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) C [ i ] = sc . nextInt ( ) ; Arrays . sort ( A ) ; Arrays . sort ( C ) ; long ans = 0L ; for ( int i = 0 ; i < n ; i ++ ) { long a = ~ Arrays . binarySearch ( A , B [ i ] , ( x , y ) -> x . compareTo ( y ) >= 0 ? 1 : - 1 ) ; long c = n - ~ Arrays . binarySearch ( C , B [ i ] , ( x , y ) -> x . compareTo ( y ) > 0 ? 1 : - 1 ) ; ans += a * c ; } System . out . println ( ans ) ; } }
| def inpl ( ) : return [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE import bisect NEW_LINE N = int ( input ( ) ) NEW_LINE A = sorted ( inpl ( ) ) NEW_LINE B = inpl ( ) NEW_LINE C = sorted ( inpl ( ) ) NEW_LINE ans = 0 NEW_LINE for b in B : NEW_LINE INDENT ans += bisect . bisect_left ( A , b ) * ( N - bisect . bisect_right ( C , b ) ) NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T67 | import java . util . * ; class Main { static Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int n = sc . nextInt ( ) ; Integer [ ] A = new Integer [ n ] , B = new Integer [ n ] , C = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) B [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) C [ i ] = sc . nextInt ( ) ; Arrays . sort ( A ) ; Arrays . sort ( C ) ; long ans = 0L ; for ( int i = 0 ; i < n ; i ++ ) { long a = ~ Arrays . binarySearch ( A , B [ i ] , ( x , y ) -> x . compareTo ( y ) >= 0 ? 1 : - 1 ) ; long c = n - ~ Arrays . binarySearch ( C , B [ i ] , ( x , y ) -> x . compareTo ( y ) > 0 ? 1 : - 1 ) ; ans += a * c ; } System . out . println ( ans ) ; } }
| II = lambda : int ( input ( ) ) NEW_LINE MI = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE def main ( ) : NEW_LINE INDENT N = II ( ) NEW_LINE A , B , C = sorted ( list ( MI ( ) ) ) , sorted ( list ( MI ( ) ) ) , sorted ( list ( MI ( ) ) ) NEW_LINE s = [ i for i in range ( 1 , N + 1 ) ] NEW_LINE ia = N - 1 NEW_LINE ns = [ None ] * N NEW_LINE for ib in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ia >= 0 and A [ ia ] >= B [ ib ] : NEW_LINE INDENT ia -= 1 NEW_LINE DEDENT if ia >= 0 : NEW_LINE INDENT ns [ ib ] = s [ ia ] NEW_LINE DEDENT else : NEW_LINE INDENT ns [ ib ] = 0 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT ns [ i ] += ns [ i - 1 ] NEW_LINE DEDENT s = ns NEW_LINE ib = N - 1 NEW_LINE ns = [ None ] * N NEW_LINE for ic in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ib >= 0 and B [ ib ] >= C [ ic ] : NEW_LINE INDENT ib -= 1 NEW_LINE DEDENT if ib >= 0 : NEW_LINE INDENT ns [ ic ] = s [ ib ] NEW_LINE DEDENT else : NEW_LINE INDENT ns [ ic ] = 0 NEW_LINE DEDENT DEDENT return sum ( ns ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( main ( ) ) NEW_LINE DEDENT
|
T68 | import java . util . * ; class Main { static Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int n = sc . nextInt ( ) ; Integer [ ] A = new Integer [ n ] , B = new Integer [ n ] , C = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) B [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) C [ i ] = sc . nextInt ( ) ; Arrays . sort ( A ) ; Arrays . sort ( C ) ; long ans = 0L ; for ( int i = 0 ; i < n ; i ++ ) { long a = ~ Arrays . binarySearch ( A , B [ i ] , ( x , y ) -> x . compareTo ( y ) >= 0 ? 1 : - 1 ) ; long c = n - ~ Arrays . binarySearch ( C , B [ i ] , ( x , y ) -> x . compareTo ( y ) > 0 ? 1 : - 1 ) ; ans += a * c ; } System . out . println ( ans ) ; } }
| from itertools import accumulate NEW_LINE N = int ( input ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE B = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE C = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE C = sorted ( C ) NEW_LINE acc_a = [ 0 ] * N NEW_LINE acc_c = [ 0 ] * N NEW_LINE i_a = 0 NEW_LINE i_c = N - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT b = B [ i ] NEW_LINE while i_a < N and A [ i_a ] < b : NEW_LINE INDENT acc_a [ i ] += 1 NEW_LINE i_a += 1 NEW_LINE DEDENT DEDENT for i in reversed ( range ( N ) ) : NEW_LINE INDENT b = B [ i ] NEW_LINE while i_c >= 0 and C [ i_c ] > b : NEW_LINE INDENT acc_c [ i ] += 1 NEW_LINE i_c -= 1 NEW_LINE DEDENT DEDENT acc_a = list ( accumulate ( acc_a ) ) NEW_LINE acc_c = list ( accumulate ( acc_c [ : : - 1 ] ) ) NEW_LINE acc_c = acc_c [ : : - 1 ] NEW_LINE ans = 0 NEW_LINE for a , c in zip ( acc_a , acc_c ) : NEW_LINE INDENT ans += a * c NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T69 | import java . util . * ; class Main { static Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int n = sc . nextInt ( ) ; Integer [ ] A = new Integer [ n ] , B = new Integer [ n ] , C = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) B [ i ] = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) C [ i ] = sc . nextInt ( ) ; Arrays . sort ( A ) ; Arrays . sort ( C ) ; long ans = 0L ; for ( int i = 0 ; i < n ; i ++ ) { long a = ~ Arrays . binarySearch ( A , B [ i ] , ( x , y ) -> x . compareTo ( y ) >= 0 ? 1 : - 1 ) ; long c = n - ~ Arrays . binarySearch ( C , B [ i ] , ( x , y ) -> x . compareTo ( y ) > 0 ? 1 : - 1 ) ; ans += a * c ; } System . out . println ( ans ) ; } }
| N = int ( input ( ) ) NEW_LINE A = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE B = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE C = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE C = sorted ( C ) NEW_LINE ans = 0 NEW_LINE midnum = [ ] NEW_LINE index = 0 NEW_LINE for i in B : NEW_LINE INDENT while True : NEW_LINE INDENT if index == N or A [ index ] >= i : NEW_LINE INDENT midnum . append ( index ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT DEDENT index = 0 NEW_LINE midCnum = [ ] NEW_LINE tmp = 0 NEW_LINE for i in midnum : NEW_LINE INDENT tmp += i NEW_LINE midCnum . append ( tmp ) NEW_LINE DEDENT for i in C : NEW_LINE INDENT while True : NEW_LINE INDENT if index == N : NEW_LINE INDENT ans += midCnum [ index - 1 ] NEW_LINE break NEW_LINE DEDENT elif i <= B [ index ] : NEW_LINE INDENT if index != 0 : NEW_LINE INDENT ans += midCnum [ index - 1 ] NEW_LINE DEDENT break NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE
|
T70 | import java . util . Arrays ; import java . util . Scanner ; import java . util . stream . IntStream ; class Main { static Scanner s = new Scanner ( System . in ) ; static int gInt ( ) { return Integer . parseInt ( s . next ( ) ) ; } public static void main ( String [ ] $ ) { int n = gInt ( ) ; int [ ] a = in ( n ) , b = in ( n ) , c = in ( n ) ; long [ ] v1 = new long [ n ] , v2 = new long [ n ] ; { int v = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( ; v < n && a [ v ] < b [ i ] ; ++ v ) { } v1 [ i ] = v ; } } Arrays . parallelPrefix ( v1 , Long :: sum ) ; { int v = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( ; v < n && b [ v ] < c [ i ] ; ++ v ) { } v2 [ i ] = v == 0 ? 0 : v1 [ v - 1 ] ; } } System . out . println ( Arrays . stream ( v2 ) . sum ( ) ) ; } static int [ ] in ( int n ) { return IntStream . range ( 0 , n ) . map ( i -> gInt ( ) ) . sorted ( ) . toArray ( ) ; } }
| N = int ( input ( ) ) NEW_LINE a = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE b = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE c = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE sorted_a = sorted ( a ) NEW_LINE sorted_b = sorted ( b ) NEW_LINE sorted_c = sorted ( c ) NEW_LINE ans = 0 NEW_LINE before_a = 0 NEW_LINE before_c = 0 NEW_LINE count_a = 0 NEW_LINE count_c_rest = 0 NEW_LINE for each_b in sorted_b : NEW_LINE INDENT for ai in range ( before_a , len ( sorted_a ) ) : NEW_LINE INDENT if each_b > sorted_a [ ai ] : NEW_LINE INDENT count_a += 1 NEW_LINE before_a += 1 NEW_LINE DEDENT else : NEW_LINE INDENT before_a = ai NEW_LINE break NEW_LINE DEDENT DEDENT for ci in range ( before_c , len ( sorted_c ) ) : NEW_LINE INDENT if sorted_c [ ci ] <= each_b : NEW_LINE INDENT count_c_rest += 1 NEW_LINE before_c += 1 NEW_LINE DEDENT else : NEW_LINE INDENT before_c = ci NEW_LINE break NEW_LINE DEDENT DEDENT ans += count_a * ( len ( sorted_c ) - count_c_rest ) NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T71 | import java . util . Arrays ; import java . util . Scanner ; import java . util . stream . IntStream ; class Main { static Scanner s = new Scanner ( System . in ) ; static int gInt ( ) { return Integer . parseInt ( s . next ( ) ) ; } public static void main ( String [ ] $ ) { int n = gInt ( ) ; int [ ] a = in ( n ) , b = in ( n ) , c = in ( n ) ; long [ ] v1 = new long [ n ] , v2 = new long [ n ] ; { int v = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( ; v < n && a [ v ] < b [ i ] ; ++ v ) { } v1 [ i ] = v ; } } Arrays . parallelPrefix ( v1 , Long :: sum ) ; { int v = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( ; v < n && b [ v ] < c [ i ] ; ++ v ) { } v2 [ i ] = v == 0 ? 0 : v1 [ v - 1 ] ; } } System . out . println ( Arrays . stream ( v2 ) . sum ( ) ) ; } static int [ ] in ( int n ) { return IntStream . range ( 0 , n ) . map ( i -> gInt ( ) ) . sorted ( ) . toArray ( ) ; } }
| def inpl ( ) : return [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE import bisect NEW_LINE N = int ( input ( ) ) NEW_LINE A = sorted ( inpl ( ) ) NEW_LINE B = inpl ( ) NEW_LINE C = sorted ( inpl ( ) ) NEW_LINE ans = 0 NEW_LINE for b in B : NEW_LINE INDENT ans += bisect . bisect_left ( A , b ) * ( N - bisect . bisect_right ( C , b ) ) NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T72 | import java . util . Arrays ; import java . util . Scanner ; import java . util . stream . IntStream ; class Main { static Scanner s = new Scanner ( System . in ) ; static int gInt ( ) { return Integer . parseInt ( s . next ( ) ) ; } public static void main ( String [ ] $ ) { int n = gInt ( ) ; int [ ] a = in ( n ) , b = in ( n ) , c = in ( n ) ; long [ ] v1 = new long [ n ] , v2 = new long [ n ] ; { int v = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( ; v < n && a [ v ] < b [ i ] ; ++ v ) { } v1 [ i ] = v ; } } Arrays . parallelPrefix ( v1 , Long :: sum ) ; { int v = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( ; v < n && b [ v ] < c [ i ] ; ++ v ) { } v2 [ i ] = v == 0 ? 0 : v1 [ v - 1 ] ; } } System . out . println ( Arrays . stream ( v2 ) . sum ( ) ) ; } static int [ ] in ( int n ) { return IntStream . range ( 0 , n ) . map ( i -> gInt ( ) ) . sorted ( ) . toArray ( ) ; } }
| II = lambda : int ( input ( ) ) NEW_LINE MI = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE def main ( ) : NEW_LINE INDENT N = II ( ) NEW_LINE A , B , C = sorted ( list ( MI ( ) ) ) , sorted ( list ( MI ( ) ) ) , sorted ( list ( MI ( ) ) ) NEW_LINE s = [ i for i in range ( 1 , N + 1 ) ] NEW_LINE ia = N - 1 NEW_LINE ns = [ None ] * N NEW_LINE for ib in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ia >= 0 and A [ ia ] >= B [ ib ] : NEW_LINE INDENT ia -= 1 NEW_LINE DEDENT if ia >= 0 : NEW_LINE INDENT ns [ ib ] = s [ ia ] NEW_LINE DEDENT else : NEW_LINE INDENT ns [ ib ] = 0 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT ns [ i ] += ns [ i - 1 ] NEW_LINE DEDENT s = ns NEW_LINE ib = N - 1 NEW_LINE ns = [ None ] * N NEW_LINE for ic in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ib >= 0 and B [ ib ] >= C [ ic ] : NEW_LINE INDENT ib -= 1 NEW_LINE DEDENT if ib >= 0 : NEW_LINE INDENT ns [ ic ] = s [ ib ] NEW_LINE DEDENT else : NEW_LINE INDENT ns [ ic ] = 0 NEW_LINE DEDENT DEDENT return sum ( ns ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( main ( ) ) NEW_LINE DEDENT
|
T73 | import java . util . Arrays ; import java . util . Scanner ; import java . util . stream . IntStream ; class Main { static Scanner s = new Scanner ( System . in ) ; static int gInt ( ) { return Integer . parseInt ( s . next ( ) ) ; } public static void main ( String [ ] $ ) { int n = gInt ( ) ; int [ ] a = in ( n ) , b = in ( n ) , c = in ( n ) ; long [ ] v1 = new long [ n ] , v2 = new long [ n ] ; { int v = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( ; v < n && a [ v ] < b [ i ] ; ++ v ) { } v1 [ i ] = v ; } } Arrays . parallelPrefix ( v1 , Long :: sum ) ; { int v = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( ; v < n && b [ v ] < c [ i ] ; ++ v ) { } v2 [ i ] = v == 0 ? 0 : v1 [ v - 1 ] ; } } System . out . println ( Arrays . stream ( v2 ) . sum ( ) ) ; } static int [ ] in ( int n ) { return IntStream . range ( 0 , n ) . map ( i -> gInt ( ) ) . sorted ( ) . toArray ( ) ; } }
| from itertools import accumulate NEW_LINE N = int ( input ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE B = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE C = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE C = sorted ( C ) NEW_LINE acc_a = [ 0 ] * N NEW_LINE acc_c = [ 0 ] * N NEW_LINE i_a = 0 NEW_LINE i_c = N - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT b = B [ i ] NEW_LINE while i_a < N and A [ i_a ] < b : NEW_LINE INDENT acc_a [ i ] += 1 NEW_LINE i_a += 1 NEW_LINE DEDENT DEDENT for i in reversed ( range ( N ) ) : NEW_LINE INDENT b = B [ i ] NEW_LINE while i_c >= 0 and C [ i_c ] > b : NEW_LINE INDENT acc_c [ i ] += 1 NEW_LINE i_c -= 1 NEW_LINE DEDENT DEDENT acc_a = list ( accumulate ( acc_a ) ) NEW_LINE acc_c = list ( accumulate ( acc_c [ : : - 1 ] ) ) NEW_LINE acc_c = acc_c [ : : - 1 ] NEW_LINE ans = 0 NEW_LINE for a , c in zip ( acc_a , acc_c ) : NEW_LINE INDENT ans += a * c NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T74 | import java . util . Arrays ; import java . util . Scanner ; import java . util . stream . IntStream ; class Main { static Scanner s = new Scanner ( System . in ) ; static int gInt ( ) { return Integer . parseInt ( s . next ( ) ) ; } public static void main ( String [ ] $ ) { int n = gInt ( ) ; int [ ] a = in ( n ) , b = in ( n ) , c = in ( n ) ; long [ ] v1 = new long [ n ] , v2 = new long [ n ] ; { int v = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( ; v < n && a [ v ] < b [ i ] ; ++ v ) { } v1 [ i ] = v ; } } Arrays . parallelPrefix ( v1 , Long :: sum ) ; { int v = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( ; v < n && b [ v ] < c [ i ] ; ++ v ) { } v2 [ i ] = v == 0 ? 0 : v1 [ v - 1 ] ; } } System . out . println ( Arrays . stream ( v2 ) . sum ( ) ) ; } static int [ ] in ( int n ) { return IntStream . range ( 0 , n ) . map ( i -> gInt ( ) ) . sorted ( ) . toArray ( ) ; } }
| N = int ( input ( ) ) NEW_LINE A = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE B = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE C = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE C = sorted ( C ) NEW_LINE ans = 0 NEW_LINE midnum = [ ] NEW_LINE index = 0 NEW_LINE for i in B : NEW_LINE INDENT while True : NEW_LINE INDENT if index == N or A [ index ] >= i : NEW_LINE INDENT midnum . append ( index ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT DEDENT index = 0 NEW_LINE midCnum = [ ] NEW_LINE tmp = 0 NEW_LINE for i in midnum : NEW_LINE INDENT tmp += i NEW_LINE midCnum . append ( tmp ) NEW_LINE DEDENT for i in C : NEW_LINE INDENT while True : NEW_LINE INDENT if index == N : NEW_LINE INDENT ans += midCnum [ index - 1 ] NEW_LINE break NEW_LINE DEDENT elif i <= B [ index ] : NEW_LINE INDENT if index != 0 : NEW_LINE INDENT ans += midCnum [ index - 1 ] NEW_LINE DEDENT break NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE
|
T75 | import java . util . Scanner ; import java . util . Arrays ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; int n = Integer . parseInt ( sc . next ( ) ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = Integer . parseInt ( sc . next ( ) ) ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = Integer . parseInt ( sc . next ( ) ) ; int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) c [ i ] = Integer . parseInt ( sc . next ( ) ) ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; Arrays . sort ( c ) ; long ret = 0 ; int s = 0 , t = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { while ( s < n && c [ s ] <= b [ i ] ) s ++ ; while ( t < n - 1 && a [ t + 1 ] < b [ i ] ) t ++ ; ret += ( long ) ( n - s ) * ( t + 1 ) ; } System . out . println ( ret ) ; } }
| N = int ( input ( ) ) NEW_LINE a = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE b = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE c = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE sorted_a = sorted ( a ) NEW_LINE sorted_b = sorted ( b ) NEW_LINE sorted_c = sorted ( c ) NEW_LINE ans = 0 NEW_LINE before_a = 0 NEW_LINE before_c = 0 NEW_LINE count_a = 0 NEW_LINE count_c_rest = 0 NEW_LINE for each_b in sorted_b : NEW_LINE INDENT for ai in range ( before_a , len ( sorted_a ) ) : NEW_LINE INDENT if each_b > sorted_a [ ai ] : NEW_LINE INDENT count_a += 1 NEW_LINE before_a += 1 NEW_LINE DEDENT else : NEW_LINE INDENT before_a = ai NEW_LINE break NEW_LINE DEDENT DEDENT for ci in range ( before_c , len ( sorted_c ) ) : NEW_LINE INDENT if sorted_c [ ci ] <= each_b : NEW_LINE INDENT count_c_rest += 1 NEW_LINE before_c += 1 NEW_LINE DEDENT else : NEW_LINE INDENT before_c = ci NEW_LINE break NEW_LINE DEDENT DEDENT ans += count_a * ( len ( sorted_c ) - count_c_rest ) NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T76 | import java . util . Scanner ; import java . util . Arrays ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; int n = Integer . parseInt ( sc . next ( ) ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = Integer . parseInt ( sc . next ( ) ) ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = Integer . parseInt ( sc . next ( ) ) ; int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) c [ i ] = Integer . parseInt ( sc . next ( ) ) ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; Arrays . sort ( c ) ; long ret = 0 ; int s = 0 , t = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { while ( s < n && c [ s ] <= b [ i ] ) s ++ ; while ( t < n - 1 && a [ t + 1 ] < b [ i ] ) t ++ ; ret += ( long ) ( n - s ) * ( t + 1 ) ; } System . out . println ( ret ) ; } }
| def inpl ( ) : return [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE import bisect NEW_LINE N = int ( input ( ) ) NEW_LINE A = sorted ( inpl ( ) ) NEW_LINE B = inpl ( ) NEW_LINE C = sorted ( inpl ( ) ) NEW_LINE ans = 0 NEW_LINE for b in B : NEW_LINE INDENT ans += bisect . bisect_left ( A , b ) * ( N - bisect . bisect_right ( C , b ) ) NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T77 | import java . util . Scanner ; import java . util . Arrays ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; int n = Integer . parseInt ( sc . next ( ) ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = Integer . parseInt ( sc . next ( ) ) ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = Integer . parseInt ( sc . next ( ) ) ; int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) c [ i ] = Integer . parseInt ( sc . next ( ) ) ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; Arrays . sort ( c ) ; long ret = 0 ; int s = 0 , t = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { while ( s < n && c [ s ] <= b [ i ] ) s ++ ; while ( t < n - 1 && a [ t + 1 ] < b [ i ] ) t ++ ; ret += ( long ) ( n - s ) * ( t + 1 ) ; } System . out . println ( ret ) ; } }
| II = lambda : int ( input ( ) ) NEW_LINE MI = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE def main ( ) : NEW_LINE INDENT N = II ( ) NEW_LINE A , B , C = sorted ( list ( MI ( ) ) ) , sorted ( list ( MI ( ) ) ) , sorted ( list ( MI ( ) ) ) NEW_LINE s = [ i for i in range ( 1 , N + 1 ) ] NEW_LINE ia = N - 1 NEW_LINE ns = [ None ] * N NEW_LINE for ib in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ia >= 0 and A [ ia ] >= B [ ib ] : NEW_LINE INDENT ia -= 1 NEW_LINE DEDENT if ia >= 0 : NEW_LINE INDENT ns [ ib ] = s [ ia ] NEW_LINE DEDENT else : NEW_LINE INDENT ns [ ib ] = 0 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT ns [ i ] += ns [ i - 1 ] NEW_LINE DEDENT s = ns NEW_LINE ib = N - 1 NEW_LINE ns = [ None ] * N NEW_LINE for ic in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ib >= 0 and B [ ib ] >= C [ ic ] : NEW_LINE INDENT ib -= 1 NEW_LINE DEDENT if ib >= 0 : NEW_LINE INDENT ns [ ic ] = s [ ib ] NEW_LINE DEDENT else : NEW_LINE INDENT ns [ ic ] = 0 NEW_LINE DEDENT DEDENT return sum ( ns ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( main ( ) ) NEW_LINE DEDENT
|
T78 | import java . util . Scanner ; import java . util . Arrays ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; int n = Integer . parseInt ( sc . next ( ) ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = Integer . parseInt ( sc . next ( ) ) ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = Integer . parseInt ( sc . next ( ) ) ; int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) c [ i ] = Integer . parseInt ( sc . next ( ) ) ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; Arrays . sort ( c ) ; long ret = 0 ; int s = 0 , t = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { while ( s < n && c [ s ] <= b [ i ] ) s ++ ; while ( t < n - 1 && a [ t + 1 ] < b [ i ] ) t ++ ; ret += ( long ) ( n - s ) * ( t + 1 ) ; } System . out . println ( ret ) ; } }
| from itertools import accumulate NEW_LINE N = int ( input ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE B = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE C = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE C = sorted ( C ) NEW_LINE acc_a = [ 0 ] * N NEW_LINE acc_c = [ 0 ] * N NEW_LINE i_a = 0 NEW_LINE i_c = N - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT b = B [ i ] NEW_LINE while i_a < N and A [ i_a ] < b : NEW_LINE INDENT acc_a [ i ] += 1 NEW_LINE i_a += 1 NEW_LINE DEDENT DEDENT for i in reversed ( range ( N ) ) : NEW_LINE INDENT b = B [ i ] NEW_LINE while i_c >= 0 and C [ i_c ] > b : NEW_LINE INDENT acc_c [ i ] += 1 NEW_LINE i_c -= 1 NEW_LINE DEDENT DEDENT acc_a = list ( accumulate ( acc_a ) ) NEW_LINE acc_c = list ( accumulate ( acc_c [ : : - 1 ] ) ) NEW_LINE acc_c = acc_c [ : : - 1 ] NEW_LINE ans = 0 NEW_LINE for a , c in zip ( acc_a , acc_c ) : NEW_LINE INDENT ans += a * c NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T79 | import java . util . Scanner ; import java . util . Arrays ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( System . in ) ; int n = Integer . parseInt ( sc . next ( ) ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = Integer . parseInt ( sc . next ( ) ) ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = Integer . parseInt ( sc . next ( ) ) ; int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) c [ i ] = Integer . parseInt ( sc . next ( ) ) ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; Arrays . sort ( c ) ; long ret = 0 ; int s = 0 , t = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { while ( s < n && c [ s ] <= b [ i ] ) s ++ ; while ( t < n - 1 && a [ t + 1 ] < b [ i ] ) t ++ ; ret += ( long ) ( n - s ) * ( t + 1 ) ; } System . out . println ( ret ) ; } }
| N = int ( input ( ) ) NEW_LINE A = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE B = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE C = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE C = sorted ( C ) NEW_LINE ans = 0 NEW_LINE midnum = [ ] NEW_LINE index = 0 NEW_LINE for i in B : NEW_LINE INDENT while True : NEW_LINE INDENT if index == N or A [ index ] >= i : NEW_LINE INDENT midnum . append ( index ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT DEDENT index = 0 NEW_LINE midCnum = [ ] NEW_LINE tmp = 0 NEW_LINE for i in midnum : NEW_LINE INDENT tmp += i NEW_LINE midCnum . append ( tmp ) NEW_LINE DEDENT for i in C : NEW_LINE INDENT while True : NEW_LINE INDENT if index == N : NEW_LINE INDENT ans += midCnum [ index - 1 ] NEW_LINE break NEW_LINE DEDENT elif i <= B [ index ] : NEW_LINE INDENT if index != 0 : NEW_LINE INDENT ans += midCnum [ index - 1 ] NEW_LINE DEDENT break NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE
|
T80 | import java . util . Arrays ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( System . in ) ; ) { new Main ( ) . solve ( sc ) ; } } void solve ( Scanner sc ) { int n = sc . nextInt ( ) ; Integer [ ] as = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { as [ i ] = sc . nextInt ( ) ; } Arrays . sort ( as ) ; Integer [ ] bs = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { bs [ i ] = sc . nextInt ( ) ; } Integer [ ] cs = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { cs [ i ] = sc . nextInt ( ) ; } Arrays . sort ( cs ) ; long ans = 0 ; for ( Integer b : bs ) { long ai = Arrays . binarySearch ( as , b , ( x , y ) -> ( x . compareTo ( y ) >= 0 ) ? 1 : - 1 ) ; ai = ~ ai ; long ci = Arrays . binarySearch ( cs , b , ( x , y ) -> ( x . compareTo ( y ) > 0 ) ? 1 : - 1 ) ; ci = ~ ci ; ans += ai * ( cs . length - ci ) ; } System . out . println ( ans ) ; } }
| N = int ( input ( ) ) NEW_LINE a = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE b = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE c = list ( map ( int , input ( ) . split ( ' ▁ ' ) ) ) NEW_LINE sorted_a = sorted ( a ) NEW_LINE sorted_b = sorted ( b ) NEW_LINE sorted_c = sorted ( c ) NEW_LINE ans = 0 NEW_LINE before_a = 0 NEW_LINE before_c = 0 NEW_LINE count_a = 0 NEW_LINE count_c_rest = 0 NEW_LINE for each_b in sorted_b : NEW_LINE INDENT for ai in range ( before_a , len ( sorted_a ) ) : NEW_LINE INDENT if each_b > sorted_a [ ai ] : NEW_LINE INDENT count_a += 1 NEW_LINE before_a += 1 NEW_LINE DEDENT else : NEW_LINE INDENT before_a = ai NEW_LINE break NEW_LINE DEDENT DEDENT for ci in range ( before_c , len ( sorted_c ) ) : NEW_LINE INDENT if sorted_c [ ci ] <= each_b : NEW_LINE INDENT count_c_rest += 1 NEW_LINE before_c += 1 NEW_LINE DEDENT else : NEW_LINE INDENT before_c = ci NEW_LINE break NEW_LINE DEDENT DEDENT ans += count_a * ( len ( sorted_c ) - count_c_rest ) NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T81 | import java . util . Arrays ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( System . in ) ; ) { new Main ( ) . solve ( sc ) ; } } void solve ( Scanner sc ) { int n = sc . nextInt ( ) ; Integer [ ] as = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { as [ i ] = sc . nextInt ( ) ; } Arrays . sort ( as ) ; Integer [ ] bs = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { bs [ i ] = sc . nextInt ( ) ; } Integer [ ] cs = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { cs [ i ] = sc . nextInt ( ) ; } Arrays . sort ( cs ) ; long ans = 0 ; for ( Integer b : bs ) { long ai = Arrays . binarySearch ( as , b , ( x , y ) -> ( x . compareTo ( y ) >= 0 ) ? 1 : - 1 ) ; ai = ~ ai ; long ci = Arrays . binarySearch ( cs , b , ( x , y ) -> ( x . compareTo ( y ) > 0 ) ? 1 : - 1 ) ; ci = ~ ci ; ans += ai * ( cs . length - ci ) ; } System . out . println ( ans ) ; } }
| def inpl ( ) : return [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE import bisect NEW_LINE N = int ( input ( ) ) NEW_LINE A = sorted ( inpl ( ) ) NEW_LINE B = inpl ( ) NEW_LINE C = sorted ( inpl ( ) ) NEW_LINE ans = 0 NEW_LINE for b in B : NEW_LINE INDENT ans += bisect . bisect_left ( A , b ) * ( N - bisect . bisect_right ( C , b ) ) NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T82 | import java . util . Arrays ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( System . in ) ; ) { new Main ( ) . solve ( sc ) ; } } void solve ( Scanner sc ) { int n = sc . nextInt ( ) ; Integer [ ] as = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { as [ i ] = sc . nextInt ( ) ; } Arrays . sort ( as ) ; Integer [ ] bs = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { bs [ i ] = sc . nextInt ( ) ; } Integer [ ] cs = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { cs [ i ] = sc . nextInt ( ) ; } Arrays . sort ( cs ) ; long ans = 0 ; for ( Integer b : bs ) { long ai = Arrays . binarySearch ( as , b , ( x , y ) -> ( x . compareTo ( y ) >= 0 ) ? 1 : - 1 ) ; ai = ~ ai ; long ci = Arrays . binarySearch ( cs , b , ( x , y ) -> ( x . compareTo ( y ) > 0 ) ? 1 : - 1 ) ; ci = ~ ci ; ans += ai * ( cs . length - ci ) ; } System . out . println ( ans ) ; } }
| II = lambda : int ( input ( ) ) NEW_LINE MI = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE def main ( ) : NEW_LINE INDENT N = II ( ) NEW_LINE A , B , C = sorted ( list ( MI ( ) ) ) , sorted ( list ( MI ( ) ) ) , sorted ( list ( MI ( ) ) ) NEW_LINE s = [ i for i in range ( 1 , N + 1 ) ] NEW_LINE ia = N - 1 NEW_LINE ns = [ None ] * N NEW_LINE for ib in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ia >= 0 and A [ ia ] >= B [ ib ] : NEW_LINE INDENT ia -= 1 NEW_LINE DEDENT if ia >= 0 : NEW_LINE INDENT ns [ ib ] = s [ ia ] NEW_LINE DEDENT else : NEW_LINE INDENT ns [ ib ] = 0 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT ns [ i ] += ns [ i - 1 ] NEW_LINE DEDENT s = ns NEW_LINE ib = N - 1 NEW_LINE ns = [ None ] * N NEW_LINE for ic in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ib >= 0 and B [ ib ] >= C [ ic ] : NEW_LINE INDENT ib -= 1 NEW_LINE DEDENT if ib >= 0 : NEW_LINE INDENT ns [ ic ] = s [ ib ] NEW_LINE DEDENT else : NEW_LINE INDENT ns [ ic ] = 0 NEW_LINE DEDENT DEDENT return sum ( ns ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( main ( ) ) NEW_LINE DEDENT
|
T83 | import java . util . Arrays ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( System . in ) ; ) { new Main ( ) . solve ( sc ) ; } } void solve ( Scanner sc ) { int n = sc . nextInt ( ) ; Integer [ ] as = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { as [ i ] = sc . nextInt ( ) ; } Arrays . sort ( as ) ; Integer [ ] bs = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { bs [ i ] = sc . nextInt ( ) ; } Integer [ ] cs = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { cs [ i ] = sc . nextInt ( ) ; } Arrays . sort ( cs ) ; long ans = 0 ; for ( Integer b : bs ) { long ai = Arrays . binarySearch ( as , b , ( x , y ) -> ( x . compareTo ( y ) >= 0 ) ? 1 : - 1 ) ; ai = ~ ai ; long ci = Arrays . binarySearch ( cs , b , ( x , y ) -> ( x . compareTo ( y ) > 0 ) ? 1 : - 1 ) ; ci = ~ ci ; ans += ai * ( cs . length - ci ) ; } System . out . println ( ans ) ; } }
| from itertools import accumulate NEW_LINE N = int ( input ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE B = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE C = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE C = sorted ( C ) NEW_LINE acc_a = [ 0 ] * N NEW_LINE acc_c = [ 0 ] * N NEW_LINE i_a = 0 NEW_LINE i_c = N - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT b = B [ i ] NEW_LINE while i_a < N and A [ i_a ] < b : NEW_LINE INDENT acc_a [ i ] += 1 NEW_LINE i_a += 1 NEW_LINE DEDENT DEDENT for i in reversed ( range ( N ) ) : NEW_LINE INDENT b = B [ i ] NEW_LINE while i_c >= 0 and C [ i_c ] > b : NEW_LINE INDENT acc_c [ i ] += 1 NEW_LINE i_c -= 1 NEW_LINE DEDENT DEDENT acc_a = list ( accumulate ( acc_a ) ) NEW_LINE acc_c = list ( accumulate ( acc_c [ : : - 1 ] ) ) NEW_LINE acc_c = acc_c [ : : - 1 ] NEW_LINE ans = 0 NEW_LINE for a , c in zip ( acc_a , acc_c ) : NEW_LINE INDENT ans += a * c NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T84 | import java . util . Arrays ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( System . in ) ; ) { new Main ( ) . solve ( sc ) ; } } void solve ( Scanner sc ) { int n = sc . nextInt ( ) ; Integer [ ] as = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { as [ i ] = sc . nextInt ( ) ; } Arrays . sort ( as ) ; Integer [ ] bs = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { bs [ i ] = sc . nextInt ( ) ; } Integer [ ] cs = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { cs [ i ] = sc . nextInt ( ) ; } Arrays . sort ( cs ) ; long ans = 0 ; for ( Integer b : bs ) { long ai = Arrays . binarySearch ( as , b , ( x , y ) -> ( x . compareTo ( y ) >= 0 ) ? 1 : - 1 ) ; ai = ~ ai ; long ci = Arrays . binarySearch ( cs , b , ( x , y ) -> ( x . compareTo ( y ) > 0 ) ? 1 : - 1 ) ; ci = ~ ci ; ans += ai * ( cs . length - ci ) ; } System . out . println ( ans ) ; } }
| N = int ( input ( ) ) NEW_LINE A = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE B = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE C = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE C = sorted ( C ) NEW_LINE ans = 0 NEW_LINE midnum = [ ] NEW_LINE index = 0 NEW_LINE for i in B : NEW_LINE INDENT while True : NEW_LINE INDENT if index == N or A [ index ] >= i : NEW_LINE INDENT midnum . append ( index ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT DEDENT index = 0 NEW_LINE midCnum = [ ] NEW_LINE tmp = 0 NEW_LINE for i in midnum : NEW_LINE INDENT tmp += i NEW_LINE midCnum . append ( tmp ) NEW_LINE DEDENT for i in C : NEW_LINE INDENT while True : NEW_LINE INDENT if index == N : NEW_LINE INDENT ans += midCnum [ index - 1 ] NEW_LINE break NEW_LINE DEDENT elif i <= B [ index ] : NEW_LINE INDENT if index != 0 : NEW_LINE INDENT ans += midCnum [ index - 1 ] NEW_LINE DEDENT break NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE
|
T85 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String a = sc . next ( ) ; System . out . println ( sc . next ( ) . replaceAll ( a , " " ) ) ; } }
| X = input ( ) NEW_LINE s = input ( ) NEW_LINE result = " " . join ( [ c for c in s if c not in X ] ) NEW_LINE print ( result ) NEW_LINE
|
T86 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String a = sc . next ( ) ; System . out . println ( sc . next ( ) . replaceAll ( a , " " ) ) ; } }
| x , c = input ( ) , input ( ) NEW_LINE ans = " " NEW_LINE for i in range ( len ( c ) ) : NEW_LINE INDENT if c [ i ] != x : ans += c [ i ] NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T87 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String a = sc . next ( ) ; System . out . println ( sc . next ( ) . replaceAll ( a , " " ) ) ; } }
| s = input ( ) ; print ( input ( ) . replace ( s , " " ) ) NEW_LINE
|
T88 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String a = sc . next ( ) ; System . out . println ( sc . next ( ) . replaceAll ( a , " " ) ) ; } }
| X = input ( ) NEW_LINE s = [ str ( _ ) for _ in input ( ) ] NEW_LINE Res = [ ] NEW_LINE for i in s : NEW_LINE INDENT if X == i : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT Res . append ( i ) NEW_LINE DEDENT DEDENT print ( " " . join ( Res ) ) NEW_LINE
|
T89 | import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String a = sc . next ( ) ; System . out . println ( sc . next ( ) . replaceAll ( a , " " ) ) ; } }
| import sys NEW_LINE import copy NEW_LINE input = sys . stdin . readline NEW_LINE c = input ( ) . rstrip ( ) NEW_LINE s = input ( ) . rstrip ( ) NEW_LINE print ( s . replace ( c , " " ) ) NEW_LINE
|
T90 | import java . io . OutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . InputMismatchException ; import java . io . IOException ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; FastScanner in = new FastScanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , FastScanner in , PrintWriter out ) { String c = in . next ( ) ; out . println ( in . next ( ) . replaceAll ( c , " " ) ) ; } } static class FastScanner { private InputStream in ; private byte [ ] buffer = new byte [ 1024 ] ; private int bufPointer ; private int bufLength ; public FastScanner ( InputStream in ) { this . in = in ; this . bufPointer = 0 ; this . bufLength = 0 ; } private int readByte ( ) { if ( bufPointer >= bufLength ) { if ( bufLength == - 1 ) throw new InputMismatchException ( ) ; bufPointer = 0 ; try { bufLength = in . read ( buffer ) ; } catch ( IOException e ) { throw new InputMismatchException ( ) ; } if ( bufLength <= 0 ) return - 1 ; } return buffer [ bufPointer ++ ] ; } private static boolean isPrintableChar ( int c ) { return c >= 33 && c <= 126 ; } public String next ( ) { StringBuilder sb = new StringBuilder ( ) ; int b = readByte ( ) ; while ( ! isPrintableChar ( b ) ) b = readByte ( ) ; while ( isPrintableChar ( b ) ) { sb . appendCodePoint ( b ) ; b = readByte ( ) ; } return sb . toString ( ) ; } } }
| X = input ( ) NEW_LINE s = input ( ) NEW_LINE result = " " . join ( [ c for c in s if c not in X ] ) NEW_LINE print ( result ) NEW_LINE
|
T91 | import java . io . OutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . InputMismatchException ; import java . io . IOException ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; FastScanner in = new FastScanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , FastScanner in , PrintWriter out ) { String c = in . next ( ) ; out . println ( in . next ( ) . replaceAll ( c , " " ) ) ; } } static class FastScanner { private InputStream in ; private byte [ ] buffer = new byte [ 1024 ] ; private int bufPointer ; private int bufLength ; public FastScanner ( InputStream in ) { this . in = in ; this . bufPointer = 0 ; this . bufLength = 0 ; } private int readByte ( ) { if ( bufPointer >= bufLength ) { if ( bufLength == - 1 ) throw new InputMismatchException ( ) ; bufPointer = 0 ; try { bufLength = in . read ( buffer ) ; } catch ( IOException e ) { throw new InputMismatchException ( ) ; } if ( bufLength <= 0 ) return - 1 ; } return buffer [ bufPointer ++ ] ; } private static boolean isPrintableChar ( int c ) { return c >= 33 && c <= 126 ; } public String next ( ) { StringBuilder sb = new StringBuilder ( ) ; int b = readByte ( ) ; while ( ! isPrintableChar ( b ) ) b = readByte ( ) ; while ( isPrintableChar ( b ) ) { sb . appendCodePoint ( b ) ; b = readByte ( ) ; } return sb . toString ( ) ; } } }
| x , c = input ( ) , input ( ) NEW_LINE ans = " " NEW_LINE for i in range ( len ( c ) ) : NEW_LINE INDENT if c [ i ] != x : ans += c [ i ] NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T92 | import java . io . OutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . InputMismatchException ; import java . io . IOException ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; FastScanner in = new FastScanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , FastScanner in , PrintWriter out ) { String c = in . next ( ) ; out . println ( in . next ( ) . replaceAll ( c , " " ) ) ; } } static class FastScanner { private InputStream in ; private byte [ ] buffer = new byte [ 1024 ] ; private int bufPointer ; private int bufLength ; public FastScanner ( InputStream in ) { this . in = in ; this . bufPointer = 0 ; this . bufLength = 0 ; } private int readByte ( ) { if ( bufPointer >= bufLength ) { if ( bufLength == - 1 ) throw new InputMismatchException ( ) ; bufPointer = 0 ; try { bufLength = in . read ( buffer ) ; } catch ( IOException e ) { throw new InputMismatchException ( ) ; } if ( bufLength <= 0 ) return - 1 ; } return buffer [ bufPointer ++ ] ; } private static boolean isPrintableChar ( int c ) { return c >= 33 && c <= 126 ; } public String next ( ) { StringBuilder sb = new StringBuilder ( ) ; int b = readByte ( ) ; while ( ! isPrintableChar ( b ) ) b = readByte ( ) ; while ( isPrintableChar ( b ) ) { sb . appendCodePoint ( b ) ; b = readByte ( ) ; } return sb . toString ( ) ; } } }
| s = input ( ) ; print ( input ( ) . replace ( s , " " ) ) NEW_LINE
|
T93 | import java . io . OutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . InputMismatchException ; import java . io . IOException ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; FastScanner in = new FastScanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , FastScanner in , PrintWriter out ) { String c = in . next ( ) ; out . println ( in . next ( ) . replaceAll ( c , " " ) ) ; } } static class FastScanner { private InputStream in ; private byte [ ] buffer = new byte [ 1024 ] ; private int bufPointer ; private int bufLength ; public FastScanner ( InputStream in ) { this . in = in ; this . bufPointer = 0 ; this . bufLength = 0 ; } private int readByte ( ) { if ( bufPointer >= bufLength ) { if ( bufLength == - 1 ) throw new InputMismatchException ( ) ; bufPointer = 0 ; try { bufLength = in . read ( buffer ) ; } catch ( IOException e ) { throw new InputMismatchException ( ) ; } if ( bufLength <= 0 ) return - 1 ; } return buffer [ bufPointer ++ ] ; } private static boolean isPrintableChar ( int c ) { return c >= 33 && c <= 126 ; } public String next ( ) { StringBuilder sb = new StringBuilder ( ) ; int b = readByte ( ) ; while ( ! isPrintableChar ( b ) ) b = readByte ( ) ; while ( isPrintableChar ( b ) ) { sb . appendCodePoint ( b ) ; b = readByte ( ) ; } return sb . toString ( ) ; } } }
| X = input ( ) NEW_LINE s = [ str ( _ ) for _ in input ( ) ] NEW_LINE Res = [ ] NEW_LINE for i in s : NEW_LINE INDENT if X == i : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT Res . append ( i ) NEW_LINE DEDENT DEDENT print ( " " . join ( Res ) ) NEW_LINE
|
T94 | import java . io . OutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . InputMismatchException ; import java . io . IOException ; import java . io . InputStream ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; FastScanner in = new FastScanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , FastScanner in , PrintWriter out ) { String c = in . next ( ) ; out . println ( in . next ( ) . replaceAll ( c , " " ) ) ; } } static class FastScanner { private InputStream in ; private byte [ ] buffer = new byte [ 1024 ] ; private int bufPointer ; private int bufLength ; public FastScanner ( InputStream in ) { this . in = in ; this . bufPointer = 0 ; this . bufLength = 0 ; } private int readByte ( ) { if ( bufPointer >= bufLength ) { if ( bufLength == - 1 ) throw new InputMismatchException ( ) ; bufPointer = 0 ; try { bufLength = in . read ( buffer ) ; } catch ( IOException e ) { throw new InputMismatchException ( ) ; } if ( bufLength <= 0 ) return - 1 ; } return buffer [ bufPointer ++ ] ; } private static boolean isPrintableChar ( int c ) { return c >= 33 && c <= 126 ; } public String next ( ) { StringBuilder sb = new StringBuilder ( ) ; int b = readByte ( ) ; while ( ! isPrintableChar ( b ) ) b = readByte ( ) ; while ( isPrintableChar ( b ) ) { sb . appendCodePoint ( b ) ; b = readByte ( ) ; } return sb . toString ( ) ; } } }
| import sys NEW_LINE import copy NEW_LINE input = sys . stdin . readline NEW_LINE c = input ( ) . rstrip ( ) NEW_LINE s = input ( ) . rstrip ( ) NEW_LINE print ( s . replace ( c , " " ) ) NEW_LINE
|
T95 | import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . OutputStream ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; InputReader in = new InputReader ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; Task solver = new Task ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class Task { public void solve ( int testNumber , InputReader in , PrintWriter out ) { String X = in . next ( ) ; String s = in . next ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! X . equals ( s . substring ( i , i + 1 ) ) ) { out . print ( s . charAt ( i ) ) ; } } out . println ( ) ; } } 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 ( ) ) ; } } }
| X = input ( ) NEW_LINE s = input ( ) NEW_LINE result = " " . join ( [ c for c in s if c not in X ] ) NEW_LINE print ( result ) NEW_LINE
|
T96 | import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . OutputStream ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; InputReader in = new InputReader ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; Task solver = new Task ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class Task { public void solve ( int testNumber , InputReader in , PrintWriter out ) { String X = in . next ( ) ; String s = in . next ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! X . equals ( s . substring ( i , i + 1 ) ) ) { out . print ( s . charAt ( i ) ) ; } } out . println ( ) ; } } 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 ( ) ) ; } } }
| x , c = input ( ) , input ( ) NEW_LINE ans = " " NEW_LINE for i in range ( len ( c ) ) : NEW_LINE INDENT if c [ i ] != x : ans += c [ i ] NEW_LINE DEDENT print ( ans ) NEW_LINE
|
T97 | import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . OutputStream ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; InputReader in = new InputReader ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; Task solver = new Task ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class Task { public void solve ( int testNumber , InputReader in , PrintWriter out ) { String X = in . next ( ) ; String s = in . next ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! X . equals ( s . substring ( i , i + 1 ) ) ) { out . print ( s . charAt ( i ) ) ; } } out . println ( ) ; } } 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 ( ) ) ; } } }
| s = input ( ) ; print ( input ( ) . replace ( s , " " ) ) NEW_LINE
|
T98 | import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . OutputStream ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; InputReader in = new InputReader ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; Task solver = new Task ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class Task { public void solve ( int testNumber , InputReader in , PrintWriter out ) { String X = in . next ( ) ; String s = in . next ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! X . equals ( s . substring ( i , i + 1 ) ) ) { out . print ( s . charAt ( i ) ) ; } } out . println ( ) ; } } 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 ( ) ) ; } } }
| X = input ( ) NEW_LINE s = [ str ( _ ) for _ in input ( ) ] NEW_LINE Res = [ ] NEW_LINE for i in s : NEW_LINE INDENT if X == i : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT Res . append ( i ) NEW_LINE DEDENT DEDENT print ( " " . join ( Res ) ) NEW_LINE
|
T99 | import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . OutputStream ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class Main { public static void main ( String [ ] args ) { InputStream inputStream = System . in ; OutputStream outputStream = System . out ; InputReader in = new InputReader ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; Task solver = new Task ( ) ; solver . solve ( 1 , in , out ) ; out . close ( ) ; } static class Task { public void solve ( int testNumber , InputReader in , PrintWriter out ) { String X = in . next ( ) ; String s = in . next ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! X . equals ( s . substring ( i , i + 1 ) ) ) { out . print ( s . charAt ( i ) ) ; } } out . println ( ) ; } } 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 sys NEW_LINE import copy NEW_LINE input = sys . stdin . readline NEW_LINE c = input ( ) . rstrip ( ) NEW_LINE s = input ( ) . rstrip ( ) NEW_LINE print ( s . replace ( c , " " ) ) NEW_LINE
|
End of preview. Expand
in Dataset Viewer.
No dataset card yet
New: Create and edit this dataset card directly on the website!
Contribute a Dataset Card- Downloads last month
- 8