728x90

import java.io.*;
import java.util.*;


public class Main {
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //선언

    public static void main(String[] args) throws IOException {
        String n = bf.readLine();
        int a = Integer.parseInt(n);

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < a; i++) {
            String s = bf.readLine();
            String[] parts = s.split(" ");

            for (int j = 0; j < a; j++) {
                list.add(Integer.parseInt(parts[j]));
            }

        }

        list.sort(Integer::compareTo);

        System.out.println(list.get(list.size()-a));
    }
}
728x90

'알고리즘' 카테고리의 다른 글

백준 알고리즘 5347  (2) 2024.03.29
백준 알고리즘 2609  (1) 2024.03.29
백준 알고리즘 9934  (0) 2024.03.29
백준 알고리즘 2800  (0) 2024.03.29
백준 알고리즘 10828  (0) 2024.03.29
728x90

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;


class pair {
    int start, end;

    public pair(int start, int end) {
        this.start = start;
        this.end = end;
    }
}

public class Main {

    static char[] input;
    static ArrayList<pair> pairs = new ArrayList<>();
    static TreeSet<String> set = new TreeSet<>();
    static boolean[] visited;
    static boolean isFirst = true;

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        Stack<Integer> stack = new Stack<>();
        input = br.readLine().toCharArray();
        for (int i = 0; i < input.length; i++) {
            char c = input[i];
            if (c == '(') {
                stack.push(i);
            } else if (c == ')') {
                pairs.add(new pair(stack.pop(), i));
            }
        }
        visited = new boolean[input.length];
        Arrays.fill(visited, true);
        dfs(0);


        for (String s : set) {
            sb.append(s).append('\n');
        }

        System.out.println(sb.toString());

        br.close();

    }

    public static void dfs(int depth)
    {
        if(depth == pairs.size())
        {
            if(isFirst)
                isFirst = false;
            else
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < input.length; i++) {
                    if(visited[i])
                        sb.append(input[i]);
                }
                set.add(sb.toString());
            }
            return;
        }

        pair pair = pairs.get(depth);

        visited[pair.start] = true;
        visited[pair.end] = true;
        dfs(depth + 1);

        visited[pair.start] = false;
        visited[pair.end] = false;
        dfs(depth + 1);
    }


}
728x90

'알고리즘' 카테고리의 다른 글

백준 알고리즘 5347  (2) 2024.03.29
백준 알고리즘 2609  (1) 2024.03.29
백준 알고리즘 9934  (0) 2024.03.29
백준 알고리즘 2075  (0) 2024.03.29
백준 알고리즘 10828  (0) 2024.03.29
728x90

import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


import java.io.IOException;
public class Main  {

    static int arr[] = new int[10001];
    static Scanner scanner = new Scanner(System.in);
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //선언

    public static void main(String[] args) throws IOException {

        String n = bf.readLine();
        int a = Integer.parseInt(n);

        int size = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < a; i++)
        {
            String s = bf.readLine(); //String
            // System.out.println(s);
            if(s.contains("push"))
            {
                String[] parts = s.split(" ");
                arr[size++] = Integer.parseInt(parts[1]);
            }
            if(s.equals("top"))
            {
                if(arr[0] == 0)
                    sb.append("-1\n");
                else
                    sb.append(arr[size-1] + "\n");
            }
            if(s.equals("size"))
            {
                sb.append(size+"\n");
            }
            if(s.equals("pop"))
            {
                if(arr[0] == 0)
                    sb.append("-1\n");
                else
                {
                    sb.append(arr[size-1] + "\n");
                    arr[(size--)-1] = 0;

                }
            }
            if(s.equals("empty"))
            {
                int j = arr[0] == 0 ? 1:0;
                sb.append(j + "\n");
            }
        }

        System.out.println(sb);
    }
}

 

 

728x90

'알고리즘' 카테고리의 다른 글

백준 알고리즘 5347  (2) 2024.03.29
백준 알고리즘 2609  (1) 2024.03.29
백준 알고리즘 9934  (0) 2024.03.29
백준 알고리즘 2075  (0) 2024.03.29
백준 알고리즘 2800  (0) 2024.03.29

+ Recent posts