회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
2
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
5419 북서풍 질문(java)
5419번 - 북서풍
wkdtjsdn77
2년 전
0
어떤 반례가 있는지 알 수 있을까요?
import java.io.*; import java.util.*; public class Main { //5419 static int[] tree, yy; static int[][] a; static ArrayList<Pos> arr; static HashMap<Integer, Integer> hashMap; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); StringTokenizer st; for (int test=0; test<t; test++) { int n = Integer.parseInt(br.readLine()); arr = new ArrayList<>(); yy = new int[n]; a = new int[n][2]; for (int i=0; i<n; i++) { st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); arr.add(new Pos(x, y)); yy[i] = y; } Arrays.sort(yy); Collections.sort(arr); int ny = zip(n), ans = 0; tree = new int[ny * 4]; for (int i=0; i<n; i++) { ans += sum(0, ny-1, 1, a[i][1], ny-1); update(0, ny-1, 1, a[i][1], 1); } bw.write(ans + "\n"); } bw.flush(); br.close(); bw.close(); } static int zip(int n) { hashMap = new HashMap<>(); int temp = 0; for (int i=0; i<n; i++) if (!hashMap.containsKey(yy[i])) hashMap.put(yy[i], temp++); for (int i=0; i<n; i++) { int x = arr.get(i).x, y = arr.get(i).y; a[i][0] = x; a[i][1] = hashMap.get(y); } return temp; } static int sum(int start, int end, int node, int left ,int right) { if (left > end || right < start) return 0; if (left <= start && end <= right) return tree[node]; int mid = (start + end) / 2; return sum(start, mid, node * 2, left, right) + sum(mid + 1, end, node * 2 + 1, left, right); } static void update(int start, int end, int node, int index, int dif) { if (index < start || index > end) return; tree[node] += dif; if (start == end) return; int mid = (start + end) / 2; update(start, mid, node * 2, index, dif); update(mid + 1, end, node * 2 + 1, index, dif); } } class Pos implements Comparable<Pos>{ int x; int y; public Pos(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pos o) { if (this.x != o.x) return this.x - o.x; return o.y - this.y; } }
댓글을 작성하려면
로그인
해야 합니다.
wkdtjsdn77 2년 전
어떤 반례가 있는지 알 수 있을까요?