반응형
https://www.acmicpc.net/problem/2751
2751번: 수 정렬하기 2
첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 수가 주어진다. 이 수는 절댓값이 1,000,000보다 작거나 같은 정수이다. 수는 중복되지 않는다.
www.acmicpc.net
N의 최대 크기가 1000,000이기 때문에 시간복잡도를 줄이는 게 중요하다고 생각함.
Collections의 sort 메소드를 사용했는데, 그래도 시간이 많이 나온 것 같다.
내 코드
메모리 : 129832 KB
시간 : 1416 ms
코드길이 : 670 B
[내 코드]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
ArrayList<Integer> arrlist = new ArrayList<>();
for(int n = 0; n < N; n++) {
int num = Integer.parseInt(br.readLine());
arrlist.add(num);
}
Collections.sort(arrlist);
for(int i : arrlist) {
sb.append(i).append('\n');
}
System.out.print(sb);
}
}
1등 코드
메모리 : 50312 KB
시간 : 216 ms
코드길이 : 2077 B
[상위권 코드]
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException {
Reader sc = new Reader();
int N = sc.nextInt();
int size = 2000001;
int [] arr = new int[size];
for (int i = 0; i < N; i++){
int idx = sc.nextInt();
arr [idx + 1000000] ++;
}
StringBuilder str = new StringBuilder("");
for (int i=0; i < size; i++){
if (arr[i] != 0){
str.append(i - 1000000);
str.append("\n");
}
}
System.out.println(str.toString());
}
}
반응형
'백준 > 정렬' 카테고리의 다른 글
[백준] 1092 : 배 (JAVA) (0) | 2024.09.13 |
---|---|
[백준] 10867 : 중복 빼고 정렬하기 (0) | 2023.04.07 |