robert-min / dev-blog

김민준 - project 정리한 repo입니다.
0 stars 0 forks source link

230817 - [Java-Python] 정렬 #73

Open robert-min opened 1 year ago

robert-min commented 1 year ago

BOJ.10825 국영수 링크

파이썬 풀이

def sort_score():
    return sorted(scores.items(), key=lambda x: (-x[1][0], x[1][1], -x[1][2], x[0]))

if __name__ == "__main__":
    # input
    N = int(input())
    scores = dict()
    for _ in range(N):
        player, k, e, m = input().split()
        scores[player] = list(map(int, [k, e, m]))

    # sort_score
    sorted_score = sort_score()

    # output
    for s in sorted_score:
        print(s[0])

자바풀이


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

public class Main {
    static FastReader scan = new FastReader();
    static StringBuilder sb = new StringBuilder();

    static class Elem implements Comparable<Elem> {
        public String name;
        public int korean, english, math;
        @Override
        public int compareTo(Elem other) {
            if (korean != other.korean) return other.korean - korean;
            if (english != other.english) return english - other.english;
            if (math != other.math) return other.math - math;
            return name.compareTo(other.name);
        }
    }

    static int N;
    static Elem[] a;
    static void input() {
        N = scan.nextInt();
        a = new Elem[N];
        // sort
        for (int i=0; i < N; i++) {
            a[i] = new Elem();
            a[i].name = scan.next();
            a[i].korean = scan.nextInt();
            a[i].english = scan.nextInt();
            a[i].math = scan.nextInt();
        }
    }

    static void sortPrint() {
        Arrays.sort(a);
        for (int i = 0; i < N; i++) {
            sb.append(a[i].name).append("\n");
        }
        System.out.println(sb.toString());
    }

    public static void main(String[] args) {
        // input
        input();
        // sort and out
        sortPrint();
    }
}
robert-min commented 1 year ago

BOJ.1015 수열 정렬 링크

파이썬 풀이

def make_P():
    for i in A:
        idx = B.index(i)
        answer.append(idx)
        B[idx] = 0

if __name__ == "__main__":
    # input
    N = int(input())
    A = list(map(int, input().split()))

    # make B
    B = sorted(A)

    # make P
    answer = []
    make_P()

    # print
    for a in answer:
        print(a, end=" ")

자바 풀이


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

public class Main {
    static FastReader scan = new FastReader();
    static StringBuilder sb = new StringBuilder();

    static class Elem implements Comparable<Elem> {
        public int num, idx;

        @Override
        public int compareTo(Elem o) {
            // 오름차순
            return num - o.num;
        }
    }

    static int N;
    static int[] P;
    static Elem[] B;

    static void input() {
        N = scan.nextInt();
        B = new Elem[N];
        P = new int[N];
        for (int i=0; i < N; i++) {
            B[i] = new Elem();
            B[i].num = scan.nextInt();
            B[i].idx = i;
        }

    }
    static void pro() {
        Arrays.sort(B); // B

        // insert P
        for (int i = 0; i < N; i++) {
            P[B[i].idx] = i;
        }

        for (int i = 0; i < N; i++) {
            sb.append(P[i]).append(" ");
        }
        System.out.println(sb.toString());

    }

    public static void main(String[] args) {
        // input
        input();
        // sort and print
        pro();
    }
}
robert-min commented 1 year ago

BOJ.11652 카드 링크

파이썬 풀이

import sys
input = sys.stdin.readline

if __name__ == "__main__":
    # input
    N = int(input())
    nums = []
    for _ in range(N):
        nums.append(int(input()))

    # Counting
    from collections import Counter
    num_count = Counter(nums).most_common()
    num_count = sorted(num_count, key= lambda x: (-x[1], x[0]))
    print(num_count[0][0])

자바 풀이


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

public class Main {
    static FastReader scan = new FastReader();
    static StringBuilder sb = new StringBuilder();

    static int N;
    static long[] a;

    static void input() {
        N = scan.nextInt();
        a = new long[N + 1];
        for (int i = 1; i <= N; i++) {
            a[i] = scan.nextLong();
        }
    }

    static void pro() {
        // Sort 정렬하기
        Arrays.sort(a, 1, N+1);

        // mode: 최빈값, modeCnt: 최빈값의 등장 횟수, curCnt: 현재 값(a[1])의 등장 횟수
        long mode = a[1];
        int modeCnt = 1, curCnt = 1;

        // 2번 원소부터 차례대로 보면서, 같은 숫자가 이어서 나오고 있는 지, 새로운 숫자가 나왔는 지를 판단하여
        // curCnt를 갱신해주고, 최빈값을 갱신하는 작업.
        for (int i = 2; i <= N; i++) {
            if (a[i] == a[i-1]) {
                curCnt++;
            } else {
                curCnt = 1;
            }

            if (curCnt > modeCnt) {
                modeCnt = curCnt;
                mode = a[i];
            }

        }
        System.out.println(mode);
    }

    public static void main(String[] args) {
        input();
        pro();
    }
}
robert-min commented 1 year ago

BOJ.15970 화살표

import sys
input = sys.stdin.readline

def toLeft(color, i):
    if i == 0:
        return 100000
    return a[color][i] - a[color][i-1]

def toRight(color, i):
    if i + 1 == len(a[color]):
        return 100000
    return a[color][i+1] - a[color][i]

if __name__ == "__main__":
    N = int(input())
    a = [[] for _ in range(N+1)]
    for i in range(N):
        loc, color = map(int ,input().split())
        a[color].append(loc)

    ans = 0
    for color in range(1, N+1):
        a[color].sort()
        for i in range(len(a[color])):
            ans += min(toLeft(color, i), toRight(color, i))

    print(ans)
import java.io.*;
import java.util.*;

public class Main {
    static FastReader scan = new FastReader();
    static StringBuilder sb = new StringBuilder();

    static int N;
    static ArrayList<Integer>[] a;

    static void input() {
        N = scan.nextInt();
        a = new ArrayList[N + 1];
        for (int color = 1; color <= N; color++) {
            a[color] = new ArrayList<Integer>();
        }
        for (int i = 1; i <= N; i++) {
            int coord, color;
            coord = scan.nextInt();
            color = scan.nextInt();
            a[color].add(coord);
        }
    }

    static int toLeft(int color, int idx) {
        if (idx == 0)  // 왼쪽에 더 이상 점이 없는 상태
            return Integer.MAX_VALUE;
        return a[color].get(idx) - a[color].get(idx - 1);
    }

    static int toRight(int color, int idx) {
        if (idx + 1 == a[color].size())  // 오른쪽에 더 이상 점이 없는 상태
            return Integer.MAX_VALUE;
        return a[color].get(idx + 1) - a[color].get(idx);
    }

    static void pro() {
        for (int color = 1; color <= N; color++)
            Collections.sort(a[color]);

        int ans = 0;
        for (int color = 1; color <= N; color++) {
            for (int i = 0; i < a[color].size(); i++) {
                int left_distance = toLeft(color, i);
                int right_distance = toRight(color, i);
                ans += Math.min(left_distance, right_distance);
            }
        }
        System.out.println(ans);
    }

    public static void main(String[] args) {
        input();
        pro();
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(new File(s)));
        }

        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;
        }
    }
}