Advertisement
santiagol26

antenasv1

Oct 22nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.39 KB | None | 0 0
  1. import java.math.*;
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5.  
  6. public class Antenas {
  7.   static class Lista<T> {
  8.      
  9.     static class NodoLista<T> {
  10.       NodoLista anterior;
  11.       NodoLista siguiente;
  12.       T valor;
  13.  
  14.       public NodoLista(T valor) {
  15.  
  16.         this.valor = valor;
  17.         siguiente = anterior = null;
  18.       }
  19.     }
  20.  
  21.     NodoLista<T> frente;
  22.     NodoLista<T> cola;
  23.     int longitud;
  24.  
  25.     public Lista() {
  26.       frente = cola = null;
  27.       longitud = 0;
  28.     }
  29.     public void insertarFrente(T valor) {
  30.      
  31.       NodoLista<T> nodo = new NodoLista<>(valor);
  32.  
  33.       if (longitud == 0) {
  34.         cola = nodo;
  35.       } else {
  36.         frente.anterior = nodo;
  37.         nodo.siguiente = frente;
  38.       }
  39.       frente = nodo;
  40.       longitud++;
  41.     }
  42.     public void insertarCola(T valor) {
  43.      
  44.       if (longitud == 0) {
  45.         insertarFrente(valor);
  46.       } else {
  47.          NodoLista<T> nodo = new NodoLista<>(valor);
  48.          cola.siguiente = nodo;
  49.          nodo.anterior = cola;
  50.          cola = nodo;
  51.          longitud++;
  52.       }
  53.     }
  54.     public void insertar(T valor, int posicion) {
  55.      
  56.       if (posicion < 0 || posicion > longitud) {
  57.         throw new IllegalArgumentException("posicion "+posicion+" fuera de rango [0, "+longitud+"]");
  58.       }
  59.       if (posicion == 0) {
  60.         insertarFrente(valor);
  61.       }
  62.       else if (posicion == longitud) {
  63.         insertarCola(valor);
  64.       }
  65.       else {
  66.         NodoLista<T> ref = frente;
  67.  
  68.         for (int i = 0; i < posicion; i++) {
  69.           ref = ref.siguiente;
  70.         }
  71.         NodoLista<T> nodo = new NodoLista<>(valor);
  72.         nodo.siguiente = ref;
  73.         nodo.anterior = ref.anterior;
  74.         ref.anterior.siguiente = nodo;
  75.         ref.anterior = nodo;
  76.         longitud++;
  77.       }
  78.     }
  79.     public T removerFrente() {
  80.       if (longitud == 0) {
  81.         return null;
  82.       }
  83.       T valor = frente.valor;
  84.       frente = frente.siguiente;
  85.       longitud--;
  86.  
  87.       if (longitud > 0) {
  88.         frente.anterior = null;
  89.       }
  90.       return valor;
  91.     }
  92.     public void borrarcola(){      
  93.         while ( frente != null){
  94.             frente = frente.siguiente;
  95.             longitud--;
  96.         }  
  97.     }
  98.     public T removerCola() {
  99.    
  100.       if (longitud == 0) {
  101.         return null;
  102.       }
  103.       T valor = cola.valor;
  104.       cola = cola.anterior;
  105.       longitud--;
  106.  
  107.       if (longitud > 0) {
  108.         cola.siguiente = null;
  109.       }
  110.       return valor;
  111.     }
  112.     public T remover(int posicion){
  113.  
  114.       if (posicion < 0 || posicion >= longitud) {
  115.         throw new IllegalArgumentException("posicion "+posicion+" fuera de rango [0, "+longitud+")");
  116.       }
  117.  
  118.       if (posicion == 0) {
  119.         return removerFrente();
  120.       }
  121.       if (posicion == longitud-1) {
  122.         return removerCola();
  123.       }
  124.       NodoLista<T> ref = frente;
  125.  
  126.       for (int i = 0; i < posicion; i++) {
  127.         ref = ref.siguiente;
  128.       }
  129.       T valor = ref.valor;
  130.       ref.anterior.siguiente = ref.siguiente;
  131.       ref.siguiente.anterior = ref.anterior;
  132.       longitud--;
  133.       return valor;
  134.     }
  135.     public T valorEn(int posicion) {
  136.      
  137.       if (posicion < 0 || posicion >= longitud) {
  138.         throw new IllegalArgumentException("posicion "+posicion+" fuera de rango [0, "+longitud+")");
  139.       }
  140.       NodoLista<T> ref = frente;
  141.  
  142.       for (int i = 0; i < posicion; i++, ref = ref.siguiente);
  143.       return ref.valor;
  144.     }
  145.     public T obtenerFrente() {
  146.  
  147.       if (longitud == 0) {
  148.         return null;
  149.       }
  150.       return frente.valor;
  151.     }
  152.     public T obtenerCola() {
  153.  
  154.       if (longitud == 0) {
  155.         return null;
  156.       }
  157.       return cola.valor;
  158.     }
  159.     public int indiceDe(T valor) {
  160.    
  161.       NodoLista<T> ref = frente;
  162.  
  163.       for (int i = 0; i < longitud; i++, ref = ref.siguiente) {
  164.         if (ref.valor.equals(valor)) {
  165.           return i;
  166.         }
  167.       }
  168.       return -1;
  169.     }
  170.     public boolean contiene(T valor) {
  171.       return indiceDe(valor) != -1;
  172.     }
  173.     public int longitud() {
  174.       return longitud;
  175.     }
  176.   }
  177.  
  178.   public static void main(String args[]) {
  179.     Scanner teclado = new Scanner(System.in);
  180.     Lista<Integer> lista = new Lista<>();
  181.     //System.out.println("Digite el numero de casos ");
  182.     short casos = teclado.nextShort();
  183.        while(casos>0){
  184.            //System.out.println(" Digite el numero de antenas ");  
  185.            int antenas = teclado.nextInt();
  186.            int rango;
  187.            //for ( int i=0;i<lista.longitud;i++){lista.remover(i);}
  188.            while (antenas>0){
  189.                rango=teclado.nextInt();
  190.                lista.insertarCola(rango);
  191.                antenas--;
  192.            }
  193.            for ( int n=0;n<lista.longitud;n++){
  194.                int count=0;
  195.                if (n==0){
  196.                  System.out.print(count+1+" ");
  197.                  continue;
  198.                }    
  199.                for (int i=n;i>=0;i--){
  200.                    if (i==n){continue;
  201.                        }
  202.                    //System.out.println("valor en i "+i);
  203.                    if(lista.valorEn(i)>lista.valorEn(n) ){
  204.                         break;
  205.                    }
  206.                    if(lista.valorEn(i)<=lista.valorEn(n) ){
  207.                        //System.out.println("valor en"+lista.valorEn(n)+" "+lista.valorEn(i)+"**"+n);
  208.                       count=count+1;
  209.                    }
  210.                }
  211.                //System.out.println("este es el contador "+count);
  212.                System.out.print(count+1+" ");
  213.              }  
  214.            lista.borrarcola();  
  215.         casos--;
  216.         System.out.println();
  217.        }  
  218.        //System.out.println(" frente y cola "+lista.obtenerCola()+" "+lista.obtenerFrente());
  219.    teclado.close();  
  220.   }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement