곽로그

[백준 7568] 덩치 본문

알고리즘/백준

[백준 7568] 덩치

일도이동 2020. 3. 2. 23:41
반응형

문제

 

 

7568번: 덩치

우리는 사람의 덩치를 키와 몸무게, 이 두 개의 값으로 표현하여 그 등수를 매겨보려고 한다. 어떤 사람의 몸무게가 x kg이고 키가 y cm라면 이 사람의 덩치는 (x,y)로 표시된다. 두 사람 A 와 B의 덩치가 각각 (x,y), (p,q)라고 할 때 x>p 그리고 y>q 이라면 우리는 A의 덩치가 B의 덩치보다 "더 크다"고 말한다. 예를 들어 어떤 A, B 두 사람의 덩치가 각각 (56,177), (45,165) 라고 한다면 A의 덩치가 B보다 큰

www.acmicpc.net

풀이

 

코드

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

class Person implements Comparable<Person>{
	int weight;
	int tall;
	
	public Person (int weight, int tall) {
		this.weight = weight;
		this.tall = tall;
	}

	@Override
	public int compareTo(Person person) {
		if(this.weight>person.weight & this.tall >person.tall) {
			return 1;
		}
		else if(this.weight<person.weight & this.tall <person.tall) {
			return -1;
		}
		else {
			return 0;
		}
		
	}
	
}
public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int N = Integer.parseInt(br.readLine());
		ArrayList<Person> people = new ArrayList<Person>();
		int[]  count = new int[N];
		
		for(int i=0; i<N ; i++) {
			String line = br.readLine();
			StringTokenizer st = new StringTokenizer(line);
			int weight = Integer.parseInt(st.nextToken());
			int tall = Integer.parseInt(st.nextToken());
			
			people.add(new Person(weight,tall));
		}
		
		//덩치비교
		for(int i = 0; i<people.size();i++) {
			Person p = people.get(i);
			for(int j=i+1;j<people.size();j++) {
				if(p.compareTo(people.get(j))==-1) {
					//i번째 사람을 기준으로 했을때 j번째 사람이 i번째 사람보다 덩치가 큰경우
					count[i]++;
				}
				else if(p.compareTo(people.get(j))==1) {
					count[j]++;
				}
			}
		}
		
		//등수
		for(int i = 0; i<count.length;i++) {
			System.out.print((count[i]+1)+" ");
		}
		
		
		
		
	}
}

 

반응형

'알고리즘 > 백준' 카테고리의 다른 글

[백준 1018 자바] 체스판 다시 칠하기  (0) 2020.03.07
[백준 2231 자바] 분해합  (0) 2020.03.03
[백준 1157] 단어공부 - 다시  (0) 2020.03.01
[백준 2675] 문자열 반복  (0) 2020.03.01
[백준 11729] 하노이의 탑  (0) 2020.02.26
Comments