티스토리 뷰
728x90
문제
https://www.acmicpc.net/problem/1946
시험: 1차 서류심사와 2차 면접시험
근데 최고 인재만을 뽑고싶어서 서류심사 성적과 면접시험 성적 중 적어도 하나가 다른 지원자보다 떨어지지 않는 자만 선발한다는 원칙을 세웠다.(너무해...)
합격자 최대 인원수 출력하는 문제.
입력
첫째 줄에는 테스트 케이스의 개수 T(1 ≤ T ≤ 20)가 주어진다.
각 테스트 케이스의 첫째 줄에 지원자의 숫자 N(1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개 줄에는 각각의 지원자의 서류심사 성적, 면접 성적의 순위가 공백을 사이에 두고 한 줄에 주어진다. 두 성적 순위는 모두 1위부터 N위까지 동석차 없이 결정된다고 가정한다.
출력
각 테스트 케이스에 대해서 진영 주식회사가 선발할 수 있는 신입사원의 최대 인원수를 한 줄에 하나씩 출력한다.
예제 입력1
2
5
3 2
1 4
4 1
2 3
5 5
7
3 6
7 3
4 2
1 4
5 7
2 5
6 1
예제 출력 1 복사
4
3
접근방법
조건: 서류 면접 두 점수가 어떤 사람보다 모두 큰게(낮은게) 하나라도 있으면 떨어짐 -> 서류점수로 sort(오름차순)하면, 면점점수만 보면 됨.
그리고 면접점수는 정렬된 array 보다 큰 값이 있으면 결과적으로 조건에 만족하는 값이 되기 때문에 합격수 += 1를 해주면 된다.
나름대로 서치하는 횟수를 줄일려고 이렇게 짯는데, 시간초과가 났다 ㅠ _ㅜ..
그래서 입력을 파일 입력되는 코드를 사용해서 해결했다.
코드
일반 입력(시간초과)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for _ in 0 ..< Int(readLine()!)! { | |
var 합격자수 = 1 | |
var list: [[String]] = [] | |
let n = Int(readLine()!)! | |
for _ in 0 ..< n { list.append(readLine()!.split(separator: " ").map{String($0)}) } | |
list.sort(by: {$0[0]<$1[0]}) | |
var min = list[0][1] | |
for i in 1 ..< n { | |
if min > list[i][1] { | |
합격자수 += 1 | |
min = list[i][1] | |
} | |
} | |
print(합격자수) | |
} |
파일입력(성공)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// 라이노님의 FileIO | |
final class FileIO { | |
private var buffer:[UInt8] | |
private var index: Int | |
init(fileHandle: FileHandle = FileHandle.standardInput) { | |
buffer = Array(fileHandle.readDataToEndOfFile())+[UInt8(0)] // 인덱스 범위 넘어가는 것 방지 | |
index = 0 | |
} | |
@inline(__always) private func read() -> UInt8 { | |
defer { index += 1 } | |
return buffer.withUnsafeBufferPointer { $0[index] } | |
} | |
@inline(__always) func readInt() -> Int { | |
var sum = 0 | |
var now = read() | |
var isPositive = true | |
while now == 10 | |
|| now == 32 { now = read() } // 공백과 줄바꿈 무시 | |
if now == 45{ isPositive.toggle(); now = read() } // 음수 처리 | |
while now >= 48, now <= 57 { | |
sum = sum * 10 + Int(now-48) | |
now = read() | |
} | |
return sum * (isPositive ? 1:-1) | |
} | |
@inline(__always) func readString() -> String { | |
var str = "" | |
var now = read() | |
while now == 10 | |
|| now == 32 { now = read() } // 공백과 줄바꿈 무시 | |
while now != 10 | |
&& now != 32 && now != 0 { | |
str += String(bytes: [now], encoding: .ascii)! | |
now = read() | |
} | |
return str | |
} | |
} | |
let file = FileIO() | |
let testCase = file.readInt() | |
for _ in 0 ..< testCase { | |
var 합격자수 = 1 | |
let n = file.readInt() | |
var list: [[Int]] = Array(repeating: [], count: n) | |
for i in 0 ..< n { | |
list[i].append(file.readInt()) | |
list[i].append(file.readInt()) | |
} | |
list.sort(by: {$0[0]<$1[0]}) | |
var min = list[0][1] | |
for i in 1 ..< n { | |
if min > list[i][1] { | |
합격자수 += 1 | |
min = list[i][1] | |
} | |
} | |
print(합격자수) | |
} |
728x90
'개발 블로그 > 알고리즘' 카테고리의 다른 글
Swift 파일입출력 코드 (0) | 2020.11.06 |
---|---|
[Swift] 프로그래머스 - 추석 트래픽(카카오 블라인드 2018) (0) | 2020.11.05 |
[Swift] 프로그래머스 - n진수 게임 (0) | 2020.11.03 |
[Swift] 프로그래머스 - 파일명 정렬 (0) | 2020.11.03 |
[Swift] 프로그래머스 - 압축 (0) | 2020.11.02 |
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 카카오블라인드2018
- TransitionStyle
- 카카오 블라인드 2018
- presentStyle
- UIModalPresentationStyle
- Level 3
- 위젯
- VIPER 패턴
- 자기PR
- BaseViewController
- 알고리즘
- 프로그래머스 오픈채팅방
- today extension
- ReactorKit
- 1차 뉴스 클러스터링
- 백준 1946
- RxDataSource
- RxSwift
- 프로그래머스 캐시
- 아키택처
- 괄호연산
- Widget
- ios
- 카카오 블라인드2018
- Stack
- Swift
- BaseTableViewController
- 프로그래머스 추석트래픽
- 백준 신입사원
- Github Search
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함