Advertisement
santiagol26

antevasv2

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