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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static Manteau[] mants;
public static Temperature[] temps;
public static int nbMants;
public static boolean isMinTemperature(Temperature t){
return t.mes == mants[t.id].min;
}
public static void main(final String[] args){
Scanner s = new Scanner(System.in);
nbMants = s.nextInt();
mants = new Manteau[nbMants];
temps = new Temperature[nbMants * 2];
for(int i = 0; i < nbMants; i++){
int min = s.nextInt(), max = s.nextInt();
mants[i] = new Manteau(min, max);
temps[i * 2] = new Temperature(min, i);
temps[(i * 2) + 1] = new Temperature(max, i);
}
Arrays.sort(temps);
// Pour toutes les Temperature
for(int i = 0; i < nbMants * 2; i++){
Temperature tI = temps[i];
Manteau mI = mants[tI.id];
if(isMinTemperature(tI)){
int j;
Temperature tJ;
Manteau mJ;
// Parcours en arrière
if(i > 0){
j = i - 1;
tJ = temps[j];
mJ = mants[tJ.id];
while(tI.mes == tJ.mes && i > 0){
if(isMinTemperature(tJ) && mJ.max <= mI.max){
mJ.lvl++;
}
j--;
if(j >= 0){
tJ = temps[j];
mJ = mants[tJ.id];
}
}
}
// Et en avant
if(i < nbMants * 2){
j = i + 1;
tJ = temps[j];
mJ = mants[tJ.id];
while(tI.mes <= mI.max && j < nbMants * 2){
if(isMinTemperature(tJ) && mJ.max <= mI.max){
mJ.lvl++;
}
j++;
if(j < nbMants * 2){
tJ = temps[j];
mJ = mants[tJ.id];
}
}
}
}
}
int lvlMax = 0;
for(int i = 0; i < nbMants; i++){
lvlMax = Math.max(lvlMax, mants[i].lvl);
}
System.out.println(lvlMax + 1);
}
}
class Manteau{
public int min, max, lvl = 0;
public Manteau(int min, int max){
this.min = min;
this.max = max;
}
}
class Temperature implements Comparable<Temperature>{
public int mes, id;
public Temperature(int mes, int id){
this.mes = mes;
this.id = id;
}
public int compareTo(Temperature o){
return new Integer(mes).compareTo(new Integer(o.mes));
}
} |
Partager