puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 001

C++ solution:
#include <iostream>

using namespace std;



long long N, K, L;

long long A[1 << 18];



bool solve(long long M) {

    long long cnt = 0, pre = 0;

    for (int i = 1; i <= N; i++) {

        if (A[i] - pre >= M && L - A[i] >= M) {

            cnt += 1;

            pre = A[i];

        }

    }

    if (cnt >= K) return true;

    return false;

}



int main() {

    // Step #1. “ü—Í

    cin >> N >> L;

    cin >> K;

    for (int i = 1; i <= N; i++) {

        cin >> A[i];

    }

    

    // Step #2. “š‚¦‚Å“ñ•ª’T�õ�i‚ß‚®‚é�®“ñ•ª’T�õ–@�j

    // https://qiita.com/drken/items/97e37dd6143e33a64c8c

    long long left = -1;

    long long right = L + 1;

    while (right - left > 1) {

        long long mid = left + (right - left) / 2;

        if (solve(mid) == false) right = mid;

        else left = mid;

    }

    cout << left << endl;

    return 0;

}

Crystal solution: _, l = read_line.split.map(&.to_i)
k = read_line.to_i
a = read_line.split.map(&.to_i) + [l]
puts (1..10**9).bsearch { |len|
  last, cnt = 0, 0
  a.each do |i|
    if i - last >= len
      cnt += 1
      last = i
    end
  end
  cnt < k + 1
}.not_nil! - 1
AtCoder problem 001

C++ solution:
#include <iostream>

using namespace std;



long long N, K, L;

long long A[1 << 18];



bool solve(long long M) {

    long long cnt = 0, pre = 0;

    for (int i = 1; i <= N; i++) {

        if (A[i] - pre >= M && L - A[i] >= M) {

            cnt += 1;

            pre = A[i];

        }

    }

    if (cnt >= K) return true;

    return false;

}



int main() {

    // Step #1. “ü—Í

    cin >> N >> L;

    cin >> K;

    for (int i = 1; i <= N; i++) {

        cin >> A[i];

    }

    

    // Step #2. “š‚¦‚Å“ñ•ª’T�õ�i‚ß‚®‚é�®“ñ•ª’T�õ–@�j

    // https://qiita.com/drken/items/97e37dd6143e33a64c8c

    long long left = -1;

    long long right = L + 1;

    while (right - left > 1) {

        long long mid = left + (right - left) / 2;

        if (solve(mid) == false) right = mid;

        else left = mid;

    }

    cout << left << endl;

    return 0;

}


Python solution: n, l = map(int,input().split())
k = int(input())
a = list(map(int,input().split()))
a.append(l)
dif = [a[0]] # 切り口の差を計算
for i in range(n):
    dif.append(a[i + 1]  - a[i])

def is_ok(x):
    length = 0
    cnt = 0
    for i in dif:
        length += i
        if length >= x:
            length = 0
            cnt += 1
    return cnt # 分割する領域の個数

def meguru_bisect(ng, ok):
    while ng - ok > 1:
        mid = (ng + ok) // 2
        if is_ok(mid) >= k + 1: # まだまだ大きくできる
            ok = mid
        else:
            ng = mid # 小さくしなければいけない
    return ok

print(meguru_bisect(l + 1, -1))
AtCoder problem 001

C++ solution:
#include <iostream>

using namespace std;



long long N, K, L;

long long A[1 << 18];



bool solve(long long M) {

    long long cnt = 0, pre = 0;

    for (int i = 1; i <= N; i++) {

        if (A[i] - pre >= M && L - A[i] >= M) {

            cnt += 1;

            pre = A[i];

        }

    }

    if (cnt >= K) return true;

    return false;

}



int main() {

    // Step #1. “ü—Í

    cin >> N >> L;

    cin >> K;

    for (int i = 1; i <= N; i++) {

        cin >> A[i];

    }

    

    // Step #2. “š‚¦‚Å“ñ•ª’T�õ�i‚ß‚®‚é�®“ñ•ª’T�õ–@�j

    // https://qiita.com/drken/items/97e37dd6143e33a64c8c

    long long left = -1;

    long long right = L + 1;

    while (right - left > 1) {

        long long mid = left + (right - left) / 2;

        if (solve(mid) == false) right = mid;

        else left = mid;

    }

    cout << left << endl;

    return 0;

}



Go solution: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	n, l := ScanInt(), ScanInt()
	k := ScanInt()
	a := ScanIntSlice(n)

	ok, ng := 0, l

	check := func(x int) bool {
		preAi := 0
		cnt := 0
		for i := 0; i < n; i++ {
			if a[i]-preAi >= x && l-a[i] >= x {
				cnt++
				preAi = a[i]
			}
		}
		return cnt >= k
	}

	for ng-ok > 1 {
		c := (ng + ok) / 2
		if check(c) {
			ok = c
		} else {
			ng = c
		}
	}
	fmt.Println(ok)
}

func ScanInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

func ScanIntSlice(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = ScanInt()
	}
	return s
}
AtCoder problem 001

C++ solution:
#include <iostream>

using namespace std;



long long N, K, L;

long long A[1 << 18];



bool solve(long long M) {

    long long cnt = 0, pre = 0;

    for (int i = 1; i <= N; i++) {

        if (A[i] - pre >= M && L - A[i] >= M) {

            cnt += 1;

            pre = A[i];

        }

    }

    if (cnt >= K) return true;

    return false;

}



int main() {

    // Step #1. “ü—Í

    cin >> N >> L;

    cin >> K;

    for (int i = 1; i <= N; i++) {

        cin >> A[i];

    }

    

    // Step #2. “š‚¦‚Å“ñ•ª’T�õ�i‚ß‚®‚é�®“ñ•ª’T�õ–@�j

    // https://qiita.com/drken/items/97e37dd6143e33a64c8c

    long long left = -1;

    long long right = L + 1;

    while (right - left > 1) {

        long long mid = left + (right - left) / 2;

        if (solve(mid) == false) right = mid;

        else left = mid;

    }

    cout << left << endl;

    return 0;

}




Rust solution: fn main() {
    proconio::input! {
        n: isize,
        l: isize,
        k: isize,
        mut a: [isize; n]
    }
    a.sort();
    let mut ok = 0;
    let mut ng = l + 1;
    while (ng - ok).abs() > 1 {
        let mid = (ok + ng) / 2;
        if is_ok(&a, l, k, mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    println!("{}", ok);
}

fn is_ok(a: &Vec<isize>, l: isize, k: isize, mid: isize) -> bool {
    let mut x = 0;
    let mut y = 0;
    for i in a {
        if i - x >= mid && l - i >= mid {
            x = *i;
            y += 1;
        }
    }
    y >= k
}
AtCoder problem 001

C++ solution:
#include <iostream>

using namespace std;



long long N, K, L;

long long A[1 << 18];



bool solve(long long M) {

    long long cnt = 0, pre = 0;

    for (int i = 1; i <= N; i++) {

        if (A[i] - pre >= M && L - A[i] >= M) {

            cnt += 1;

            pre = A[i];

        }

    }

    if (cnt >= K) return true;

    return false;

}



int main() {

    // Step #1. “ü—Í

    cin >> N >> L;

    cin >> K;

    for (int i = 1; i <= N; i++) {

        cin >> A[i];

    }

    

    // Step #2. “š‚¦‚Å“ñ•ª’T�õ�i‚ß‚®‚é�®“ñ•ª’T�õ–@�j

    // https://qiita.com/drken/items/97e37dd6143e33a64c8c

    long long left = -1;

    long long right = L + 1;

    while (right - left > 1) {

        long long mid = left + (right - left) / 2;

        if (solve(mid) == false) right = mid;

        else left = mid;

    }

    cout << left << endl;

    return 0;

}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var a: []int :: #[n + 1]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	do a[n] :: l
	
	var ok: int :: 0
	var ng: int :: l + 1
	while((ok - ng).abs() > 1)
		var mid: int :: (ok + ng) / 2
		var prev: int :: 0
		var cnt: int :: 0
		for i(0, n)
			if(a[i] - prev >= mid)
				do cnt :+ 1
				do prev :: a[i]
			end if
		end for
		
		var isOk: bool :: cnt >= k + 1
		if(isOk)
			do ok :: mid
		else
			do ng :: mid
		end if
	end while
	
	var ans: int :: ok
	do cui@print("\{ans}\n")
end func
AtCoder problem 001

C++ solution:

Crystal solution: _, l = read_line.split.map(&.to_i)
k = read_line.to_i
a = read_line.split.map(&.to_i) + [l]
puts (1..10**9).bsearch { |len|
  last, cnt = 0, 0
  a.each do |i|
    if i - last >= len
      cnt += 1
      last = i
    end
  end
  cnt < k + 1
}.not_nil! - 1


Python solution: n, l = map(int,input().split())
k = int(input())
a = list(map(int,input().split()))
a.append(l)
dif = [a[0]] # 切り口の差を計算
for i in range(n):
    dif.append(a[i + 1]  - a[i])

def is_ok(x):
    length = 0
    cnt = 0
    for i in dif:
        length += i
        if length >= x:
            length = 0
            cnt += 1
    return cnt # 分割する領域の個数

def meguru_bisect(ng, ok):
    while ng - ok > 1:
        mid = (ng + ok) // 2
        if is_ok(mid) >= k + 1: # まだまだ大きくできる
            ok = mid
        else:
            ng = mid # 小さくしなければいけない
    return ok

print(meguru_bisect(l + 1, -1))
AtCoder problem 001

C++ solution:

Crystal solution: _, l = read_line.split.map(&.to_i)
k = read_line.to_i
a = read_line.split.map(&.to_i) + [l]
puts (1..10**9).bsearch { |len|
  last, cnt = 0, 0
  a.each do |i|
    if i - last >= len
      cnt += 1
      last = i
    end
  end
  cnt < k + 1
}.not_nil! - 1



Go solution: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	n, l := ScanInt(), ScanInt()
	k := ScanInt()
	a := ScanIntSlice(n)

	ok, ng := 0, l

	check := func(x int) bool {
		preAi := 0
		cnt := 0
		for i := 0; i < n; i++ {
			if a[i]-preAi >= x && l-a[i] >= x {
				cnt++
				preAi = a[i]
			}
		}
		return cnt >= k
	}

	for ng-ok > 1 {
		c := (ng + ok) / 2
		if check(c) {
			ok = c
		} else {
			ng = c
		}
	}
	fmt.Println(ok)
}

func ScanInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

func ScanIntSlice(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = ScanInt()
	}
	return s
}
AtCoder problem 001

C++ solution:

Crystal solution: _, l = read_line.split.map(&.to_i)
k = read_line.to_i
a = read_line.split.map(&.to_i) + [l]
puts (1..10**9).bsearch { |len|
  last, cnt = 0, 0
  a.each do |i|
    if i - last >= len
      cnt += 1
      last = i
    end
  end
  cnt < k + 1
}.not_nil! - 1




Rust solution: fn main() {
    proconio::input! {
        n: isize,
        l: isize,
        k: isize,
        mut a: [isize; n]
    }
    a.sort();
    let mut ok = 0;
    let mut ng = l + 1;
    while (ng - ok).abs() > 1 {
        let mid = (ok + ng) / 2;
        if is_ok(&a, l, k, mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    println!("{}", ok);
}

fn is_ok(a: &Vec<isize>, l: isize, k: isize, mid: isize) -> bool {
    let mut x = 0;
    let mut y = 0;
    for i in a {
        if i - x >= mid && l - i >= mid {
            x = *i;
            y += 1;
        }
    }
    y >= k
}
AtCoder problem 001

C++ solution:

Crystal solution: _, l = read_line.split.map(&.to_i)
k = read_line.to_i
a = read_line.split.map(&.to_i) + [l]
puts (1..10**9).bsearch { |len|
  last, cnt = 0, 0
  a.each do |i|
    if i - last >= len
      cnt += 1
      last = i
    end
  end
  cnt < k + 1
}.not_nil! - 1





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var a: []int :: #[n + 1]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	do a[n] :: l
	
	var ok: int :: 0
	var ng: int :: l + 1
	while((ok - ng).abs() > 1)
		var mid: int :: (ok + ng) / 2
		var prev: int :: 0
		var cnt: int :: 0
		for i(0, n)
			if(a[i] - prev >= mid)
				do cnt :+ 1
				do prev :: a[i]
			end if
		end for
		
		var isOk: bool :: cnt >= k + 1
		if(isOk)
			do ok :: mid
		else
			do ng :: mid
		end if
	end while
	
	var ans: int :: ok
	do cui@print("\{ans}\n")
end func
AtCoder problem 001

C++ solution:


Python solution: n, l = map(int,input().split())
k = int(input())
a = list(map(int,input().split()))
a.append(l)
dif = [a[0]] # 切り口の差を計算
for i in range(n):
    dif.append(a[i + 1]  - a[i])

def is_ok(x):
    length = 0
    cnt = 0
    for i in dif:
        length += i
        if length >= x:
            length = 0
            cnt += 1
    return cnt # 分割する領域の個数

def meguru_bisect(ng, ok):
    while ng - ok > 1:
        mid = (ng + ok) // 2
        if is_ok(mid) >= k + 1: # まだまだ大きくできる
            ok = mid
        else:
            ng = mid # 小さくしなければいけない
    return ok

print(meguru_bisect(l + 1, -1))

Go solution: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	n, l := ScanInt(), ScanInt()
	k := ScanInt()
	a := ScanIntSlice(n)

	ok, ng := 0, l

	check := func(x int) bool {
		preAi := 0
		cnt := 0
		for i := 0; i < n; i++ {
			if a[i]-preAi >= x && l-a[i] >= x {
				cnt++
				preAi = a[i]
			}
		}
		return cnt >= k
	}

	for ng-ok > 1 {
		c := (ng + ok) / 2
		if check(c) {
			ok = c
		} else {
			ng = c
		}
	}
	fmt.Println(ok)
}

func ScanInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

func ScanIntSlice(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = ScanInt()
	}
	return s
}
AtCoder problem 001

C++ solution:


Python solution: n, l = map(int,input().split())
k = int(input())
a = list(map(int,input().split()))
a.append(l)
dif = [a[0]] # 切り口の差を計算
for i in range(n):
    dif.append(a[i + 1]  - a[i])

def is_ok(x):
    length = 0
    cnt = 0
    for i in dif:
        length += i
        if length >= x:
            length = 0
            cnt += 1
    return cnt # 分割する領域の個数

def meguru_bisect(ng, ok):
    while ng - ok > 1:
        mid = (ng + ok) // 2
        if is_ok(mid) >= k + 1: # まだまだ大きくできる
            ok = mid
        else:
            ng = mid # 小さくしなければいけない
    return ok

print(meguru_bisect(l + 1, -1))


Rust solution: fn main() {
    proconio::input! {
        n: isize,
        l: isize,
        k: isize,
        mut a: [isize; n]
    }
    a.sort();
    let mut ok = 0;
    let mut ng = l + 1;
    while (ng - ok).abs() > 1 {
        let mid = (ok + ng) / 2;
        if is_ok(&a, l, k, mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    println!("{}", ok);
}

fn is_ok(a: &Vec<isize>, l: isize, k: isize, mid: isize) -> bool {
    let mut x = 0;
    let mut y = 0;
    for i in a {
        if i - x >= mid && l - i >= mid {
            x = *i;
            y += 1;
        }
    }
    y >= k
}
AtCoder problem 001

C++ solution:


Python solution: n, l = map(int,input().split())
k = int(input())
a = list(map(int,input().split()))
a.append(l)
dif = [a[0]] # 切り口の差を計算
for i in range(n):
    dif.append(a[i + 1]  - a[i])

def is_ok(x):
    length = 0
    cnt = 0
    for i in dif:
        length += i
        if length >= x:
            length = 0
            cnt += 1
    return cnt # 分割する領域の個数

def meguru_bisect(ng, ok):
    while ng - ok > 1:
        mid = (ng + ok) // 2
        if is_ok(mid) >= k + 1: # まだまだ大きくできる
            ok = mid
        else:
            ng = mid # 小さくしなければいけない
    return ok

print(meguru_bisect(l + 1, -1))



Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var a: []int :: #[n + 1]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	do a[n] :: l
	
	var ok: int :: 0
	var ng: int :: l + 1
	while((ok - ng).abs() > 1)
		var mid: int :: (ok + ng) / 2
		var prev: int :: 0
		var cnt: int :: 0
		for i(0, n)
			if(a[i] - prev >= mid)
				do cnt :+ 1
				do prev :: a[i]
			end if
		end for
		
		var isOk: bool :: cnt >= k + 1
		if(isOk)
			do ok :: mid
		else
			do ng :: mid
		end if
	end while
	
	var ans: int :: ok
	do cui@print("\{ans}\n")
end func
AtCoder problem 001

C++ solution:



Go solution: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	n, l := ScanInt(), ScanInt()
	k := ScanInt()
	a := ScanIntSlice(n)

	ok, ng := 0, l

	check := func(x int) bool {
		preAi := 0
		cnt := 0
		for i := 0; i < n; i++ {
			if a[i]-preAi >= x && l-a[i] >= x {
				cnt++
				preAi = a[i]
			}
		}
		return cnt >= k
	}

	for ng-ok > 1 {
		c := (ng + ok) / 2
		if check(c) {
			ok = c
		} else {
			ng = c
		}
	}
	fmt.Println(ok)
}

func ScanInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

func ScanIntSlice(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = ScanInt()
	}
	return s
}


Rust solution: fn main() {
    proconio::input! {
        n: isize,
        l: isize,
        k: isize,
        mut a: [isize; n]
    }
    a.sort();
    let mut ok = 0;
    let mut ng = l + 1;
    while (ng - ok).abs() > 1 {
        let mid = (ok + ng) / 2;
        if is_ok(&a, l, k, mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    println!("{}", ok);
}

fn is_ok(a: &Vec<isize>, l: isize, k: isize, mid: isize) -> bool {
    let mut x = 0;
    let mut y = 0;
    for i in a {
        if i - x >= mid && l - i >= mid {
            x = *i;
            y += 1;
        }
    }
    y >= k
}
AtCoder problem 001

C++ solution:



Go solution: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	n, l := ScanInt(), ScanInt()
	k := ScanInt()
	a := ScanIntSlice(n)

	ok, ng := 0, l

	check := func(x int) bool {
		preAi := 0
		cnt := 0
		for i := 0; i < n; i++ {
			if a[i]-preAi >= x && l-a[i] >= x {
				cnt++
				preAi = a[i]
			}
		}
		return cnt >= k
	}

	for ng-ok > 1 {
		c := (ng + ok) / 2
		if check(c) {
			ok = c
		} else {
			ng = c
		}
	}
	fmt.Println(ok)
}

func ScanInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

func ScanIntSlice(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = ScanInt()
	}
	return s
}



Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var a: []int :: #[n + 1]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	do a[n] :: l
	
	var ok: int :: 0
	var ng: int :: l + 1
	while((ok - ng).abs() > 1)
		var mid: int :: (ok + ng) / 2
		var prev: int :: 0
		var cnt: int :: 0
		for i(0, n)
			if(a[i] - prev >= mid)
				do cnt :+ 1
				do prev :: a[i]
			end if
		end for
		
		var isOk: bool :: cnt >= k + 1
		if(isOk)
			do ok :: mid
		else
			do ng :: mid
		end if
	end while
	
	var ans: int :: ok
	do cui@print("\{ans}\n")
end func
AtCoder problem 001

C++ solution:




Rust solution: fn main() {
    proconio::input! {
        n: isize,
        l: isize,
        k: isize,
        mut a: [isize; n]
    }
    a.sort();
    let mut ok = 0;
    let mut ng = l + 1;
    while (ng - ok).abs() > 1 {
        let mid = (ok + ng) / 2;
        if is_ok(&a, l, k, mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    println!("{}", ok);
}

fn is_ok(a: &Vec<isize>, l: isize, k: isize, mid: isize) -> bool {
    let mut x = 0;
    let mut y = 0;
    for i in a {
        if i - x >= mid && l - i >= mid {
            x = *i;
            y += 1;
        }
    }
    y >= k
}


Kuin solution: func main()
	var n: int :: cui@inputInt()
	var l: int :: cui@inputInt()
	var k: int :: cui@inputInt()
	var a: []int :: #[n + 1]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	do a[n] :: l
	
	var ok: int :: 0
	var ng: int :: l + 1
	while((ok - ng).abs() > 1)
		var mid: int :: (ok + ng) / 2
		var prev: int :: 0
		var cnt: int :: 0
		for i(0, n)
			if(a[i] - prev >= mid)
				do cnt :+ 1
				do prev :: a[i]
			end if
		end for
		
		var isOk: bool :: cnt >= k + 1
		if(isOk)
			do ok :: mid
		else
			do ng :: mid
		end if
	end while
	
	var ans: int :: ok
	do cui@print("\{ans}\n")
end func
AtCoder problem 002

C++ solution:
// this problem was so fun :)

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

#define fs(n) fixed << setprecision(n);
#define mp(a, b) make_pair(a, b);
using ll = long long;
using ld = long double;

int n;
int l = 0, r = 0;
vector<string> anslist;

/*
  if X is correct brackets string then:
  (define length of X = L)
  * L is even number
  * from 1st to nth chars,
  *   the count of "(" is greater than or equal to the count of ")" (1 ~ n ~ L - 1)
  * the total count of "(" is equal to the total count of ")" is equal to L / 2
*/
void dfs(string s){
  if(s.length() == n){
    // should consist l = r = n / 2
    anslist.emplace_back(s);
    return;
  }else{
    if(r < n / 2){
      r++;
      dfs(s + "(");
      r--;
    }
    if(l < n / 2 && r > l){
      l++;
      dfs(s + ")");
      l--;
    }
  }
}

int main(){
  // amount of "(" is equal to amount of ")" is equal to n / 2
  cin >> n;
  if(n % 2) cout << endl;
  else{
    // generate from 2nd char by dfs
    r = 1;
    dfs("(");
  }
  // sort by lexical order ('(' < ')')
  sort(anslist.begin(), anslist.end());
  for(auto itr = anslist.begin(); itr != anslist.end(); itr++){
    cout << *itr << endl;
  }
}

Crystal solution: n = read_line.to_i
['(', ')'].each_repeated_permutation(n, true) { |s|
  a = s.reduce([0]) { |acc, c|
    acc << acc[-1] + (c == '(' ? 1 : -1)
  }
  if a[-1] == 0 && a.all? { |i| i >= 0 }
    puts s.join
  end
}
AtCoder problem 002

C++ solution:
// this problem was so fun :)

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

#define fs(n) fixed << setprecision(n);
#define mp(a, b) make_pair(a, b);
using ll = long long;
using ld = long double;

int n;
int l = 0, r = 0;
vector<string> anslist;

/*
  if X is correct brackets string then:
  (define length of X = L)
  * L is even number
  * from 1st to nth chars,
  *   the count of "(" is greater than or equal to the count of ")" (1 ~ n ~ L - 1)
  * the total count of "(" is equal to the total count of ")" is equal to L / 2
*/
void dfs(string s){
  if(s.length() == n){
    // should consist l = r = n / 2
    anslist.emplace_back(s);
    return;
  }else{
    if(r < n / 2){
      r++;
      dfs(s + "(");
      r--;
    }
    if(l < n / 2 && r > l){
      l++;
      dfs(s + ")");
      l--;
    }
  }
}

int main(){
  // amount of "(" is equal to amount of ")" is equal to n / 2
  cin >> n;
  if(n % 2) cout << endl;
  else{
    // generate from 2nd char by dfs
    r = 1;
    dfs("(");
  }
  // sort by lexical order ('(' < ')')
  sort(anslist.begin(), anslist.end());
  for(auto itr = anslist.begin(); itr != anslist.end(); itr++){
    cout << *itr << endl;
  }
}


Python solution: from itertools import product
n = int(input())
ans = []

if n % 2 == 0: # 奇数のときは正しいカッコ列を生成できない
    for i in product([0, 1], repeat = n): # 0 -> （ , 1 -> ）
        if i.count(1) == n // 2: # 0, 1の個数が同じ時に判定を始める
            zero_count = 0
            Bool = True
            word = ""
            for j in range(len(i)):
                if i[j] == 0:
                    zero_count += 1
                elif i[j] == 1:
                    if zero_count >= 1: # 0の個数が１個以上ないと正しい文字列にならない
                        zero_count -= 1
                    else:
                        Bool = False
                word += str(i[j])
            if Bool:
                x = word.replace("0", "(")
                y = x.replace("1", ")")
                ans.append(y)
ans.sort()
print(*ans ,sep = "\n")