(SPOJ) 18543 - Vende-se - Solução
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
static int leitor(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') break;
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') break;
}
return resp;
}
void processa() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int qtePredios = leitor(br);
int qteVenda = leitor(br);
int[] vetor = new int[qtePredios];
for (int i = 0; i < qtePredios; i++) {
vetor[i] = leitor(br);
}
Arrays.sort(vetor);
int qteRestante = qtePredios - qteVenda;
int qteLimite = qtePredios - qteRestante + 1;
int menor = 10000001;
int valor = 0;
for (int i = 0; i < qteLimite; i++) {
valor = vetor[(i + qteRestante - 1)] - vetor[i];
if (valor < menor) {
menor = valor;
}
}
System.out.println(menor);
return;
}
}
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
static int leitor(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') break;
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') break;
}
return resp;
}
void processa() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int qtePredios = leitor(br);
int qteVenda = leitor(br);
int[] vetor = new int[qtePredios];
for (int i = 0; i < qtePredios; i++) {
vetor[i] = leitor(br);
}
Arrays.sort(vetor);
int qteRestante = qtePredios - qteVenda;
int qteLimite = qtePredios - qteRestante + 1;
int menor = 10000001;
int valor = 0;
for (int i = 0; i < qteLimite; i++) {
valor = vetor[(i + qteRestante - 1)] - vetor[i];
if (valor < menor) {
menor = valor;
}
}
System.out.println(menor);
return;
}
}
Comments
Post a Comment