ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1100. Final Standings

JAVA WA#11 TIME LIMIT , SOMEONE CAN HELP ME?
Posted by Julius 26 Nov 2015 08:18
here is my code if someone can help me, thanks you and greetings.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class P1100 {

    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        int [] vec=new int[n*2];
        for(int i=0;i<n*2;i++)vec[i]=in.nextInt();
        int [] aux=new int[n];
        int mayor=0,menor=100;
        for(int i=1;i<n*2;i=i+2){
            if(menor>vec[i]){
                menor=vec[i];
            }
            if(mayor<vec[i]){
                mayor=vec[i];
            }
        }
        int sig=-1;
        for(int i=0;i<n;i++){
            for(int j=1;j<n*2;j=j+2){
               if(mayor==vec[j] && vec[j]!=-1 ){
                   System.out.printf(" %d %d",vec[j-1],vec[j]);
                   vec[j]=-1;

               }
               if(sig<mayor && sig < vec[j] && vec[j]!=-1){
                   sig=vec[j];
               }
            }
            mayor=sig;
            sig=-1;

        }
        out.flush();

    }
}
Re: JAVA WA#11 TIME LIMIT , SOMEONE CAN HELP ME?
Posted by ToadMonster 26 Nov 2015 14:01
Your solution contains 2 nested loops with range [0..n) each. So you have n^2 complexity. Of course you have TLE.

You should better load input into array of structures (with fields Id, Score), sort array by score field in decreasing order and print it.

* "Collections.sort" is stable so you can just use it.
Re: JAVA WA#11 TIME LIMIT , SOMEONE CAN HELP ME?
Posted by Julius 26 Nov 2015 23:05
im trying now with a arrayList of a node(int ele,int pos), but i dont know how no order that, can you help me?
Re: JAVA WA#11 TIME LIMIT , SOMEONE CAN HELP ME?
Posted by ToadMonster 27 Nov 2015 17:33