지구정복

[구현] 백준 - 아시아 정보올림피아드(2535) 본문

데이터 엔지니어링 정복/Algorithm

[구현] 백준 - 아시아 정보올림피아드(2535)

eeaarrtthh 2022. 7. 27. 21:52
728x90
반응형
SMALL

-문제

https://www.acmicpc.net/problem/2535

 

2535번: 아시아 정보올림피아드

첫 번째 줄에는 대회참가 학생 수를 나타내는 N이 주어진다. 단, 3 ≤ N ≤ 100이다. 두 번째 줄부터 N개의 줄에는 각 줄마다 한 학생의 소속 국가 번호, 학생 번호, 그리고 성적이 하나의 빈칸을 사

www.acmicpc.net

 

-자바 풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

class contest {
    int nation;
    int stuNum;
    int score;

    public contest( int nation, int stuNum, int score ) {
        this.nation = nation;
        this.stuNum = stuNum;
        this.score = score;
    }
}

public class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt( br.readLine() );

        contest[] cts = new contest[N];

        StringTokenizer st;
        for( int i=0; i<N; i++ ) {
            st = new StringTokenizer( br.readLine() );
            
            int tmp_nation = Integer.parseInt( st.nextToken() );
            int tmp_stuNum = Integer.parseInt( st.nextToken() );
            int tmp_score = Integer.parseInt( st.nextToken() );

            cts[i] = new contest( tmp_nation, tmp_stuNum, tmp_score );
        }

        Arrays.sort( cts, new Comparator<contest>() {
            @Override
            public int compare( contest o1, contest o2 ) {
                return o2.score-o1.score;
            }
        });

        int[] visit = new int[N+1];
        int cnt_answer = 0;

        for( int i=0; i<N; i++ ) {
            if( cnt_answer >= 3 ) break;
            
            if( visit[cts[i].nation] < 2 ) {
                System.out.println( cts[i].nation+" "+cts[i].stuNum );

                cnt_answer++;
                visit[cts[i].nation]++;
            }
        }

    }
}

 

-파이썬 풀이

import sys
input = sys.stdin.readline

N = int(input())

stu = []

for _ in range(N):
    stu.append( list( map(int, input().split()) ) )

stu = sorted( stu, key = lambda x: x[2], reverse=True )

cnt = [0] * (N+1)
answer = 0
i = 0

while answer < 3:
    if cnt[stu[i][0]] < 2:
        cnt[stu[i][0]] += 1
        print( stu[i][0], stu[i][1] )
        answer += 1
    i += 1
728x90
반응형
LIST
Comments