11462 - Age Sort

All about problems in Volume 114. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11462 - Ages Sort

Post by brianfry713 » Wed May 23, 2012 11:16 pm

C/C++ is faster than JAVA.
Check input and AC output for thousands of problems on uDebug!

SyFyKid
New poster
Posts: 26
Joined: Tue May 08, 2012 9:47 am

Re: 11462 - Ages Sort

Post by SyFyKid » Thu May 24, 2012 9:33 am

I am not talking about C/C++, I am talking about JAVA and better I/O methods.
brianfry713 wrote:C/C++ is faster than JAVA.

jocker1
New poster
Posts: 5
Joined: Sat May 19, 2012 11:10 pm

Re: 11462 - Ages Sort

Post by jocker1 » Thu May 24, 2012 10:17 am

How to output this code....... cause i got PE... plz could any one help me...

Code: Select all

#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int main(){
    vector<int> mvector;
    vector<int>::iterator it;
    unsigned int ncases;
    int numbers;
    while(cin>>ncases){
                       if(ncases==0) break;
                       while(ncases-- && cin>>numbers){
                                      if(numbers<1) break;
                                      mvector.push_back(numbers);
                                      }
                       sort(mvector.begin(),mvector.end());               
                       for(it=mvector.begin();it!=mvector.end();it++){
                       if(it==mvector.end()) cout<<*it<<endl;
                       cout<<*it<<" ";
                       }
mvector.erase(mvector.begin(),mvector.end());
                       
                       }
    return 0;
    }


SyFyKid
New poster
Posts: 26
Joined: Tue May 08, 2012 9:47 am

Re: 11462 - Ages Sort

Post by SyFyKid » Thu May 31, 2012 1:24 pm

you dont need sort, erase and all that stuff... :D
jocker1 wrote:How to output this code....... cause i got PE... plz could any one help me...

Code: Select all

#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int main(){
    vector<int> mvector;
    vector<int>::iterator it;
    unsigned int ncases;
    int numbers;
    while(cin>>ncases){
                       if(ncases==0) break;
                       while(ncases-- && cin>>numbers){
                                      if(numbers<1) break;
                                      mvector.push_back(numbers);
                                      }
                       sort(mvector.begin(),mvector.end());               
                       for(it=mvector.begin();it!=mvector.end();it++){
                       if(it==mvector.end()) cout<<*it<<endl;
                       cout<<*it<<" ";
                       }
mvector.erase(mvector.begin(),mvector.end());
                       
                       }
    return 0;
    }


sith
Learning poster
Posts: 72
Joined: Sat May 19, 2012 7:46 pm

Re: 11462 - Ages Sort

Post by sith » Thu Jun 14, 2012 10:47 pm

Hello
I've got WA,
Why?????

Here is my code

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

class Main {


    public static void main(String[] args) {


        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));


        String line;
        try {
            while ((line = reader.readLine()) != null) {
                if (line.trim().equals("0")) {
                    break;
                }

                int numbersCount = Integer.parseInt(line);
                PriorityQueue<Integer> numbers = new PriorityQueue<Integer>(numbersCount);

                StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
                while (tokenizer.hasMoreTokens()) {
                    numbers.add(Integer.parseInt(tokenizer.nextToken()));
                }

                StringBuilder builder = new StringBuilder();
                for (Iterator<Integer> iterator = numbers.iterator(); iterator.hasNext(); ) {
                    Integer number = iterator.next();
                    builder.append(number);
                    if (iterator.hasNext()) {
                        builder.append(" ");
                    }
                }
                System.out.println(builder.toString());
            }
        } catch (IOException e) {

        }
    }
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11462 - Ages Sort

Post by brianfry713 » Fri Jun 15, 2012 9:37 pm

Use poll(), not an iterator.
http://docs.oracle.com/javase/1.5.0/doc ... Queue.html
The iterator does not return the elements in any particular order.
poll() Retrieves and removes the head of this queue, or null if this queue is empty.
Check input and AC output for thousands of problems on uDebug!

raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

Re: 11462 - Ages Sort

Post by raj » Mon Feb 18, 2013 7:44 pm

why its "TIME LIMIT EXCEEDED""??
can anyone help me please??? :(

import java.io.*;
import java.util.*;
public class Main{
public static void main(String [] args)throws IOException{
final BufferedReader k = new BufferedReader(new InputStreamReader(System.in));
PrintWriter z = new PrintWriter(System.out);
int t;
while((t = Integer.valueOf(k.readLine())) != 0)
{
int [] y = new int [t];
StringTokenizer s = new StringTokenizer(k.readLine());
int c = 0;
while(s.hasMoreTokens())
{
y[c++] = Integer.valueOf(s.nextToken());
}
Arrays.sort(y);
for(c = 0;c<y.length;c++)
{
z.print(y[c]+" ");
z.flush();
}
z.println();
z.flush();
}
}
}

raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

Re: Age Sort

Post by raj » Tue Feb 19, 2013 12:32 am

Deleted
Last edited by raj on Tue Mar 05, 2013 11:52 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Age Sort

Post by brianfry713 » Wed Feb 20, 2013 11:52 pm

This doesn't match the sample I/O for problem 11462.

Try using BufferedWriter and don't flush the output until the end.
Check input and AC output for thousands of problems on uDebug!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11462 - Ages Sort

Post by brianfry713 » Wed Feb 20, 2013 11:54 pm

Don't double post.
Check input and AC output for thousands of problems on uDebug!

raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

Re: Age Sort

Post by raj » Fri Feb 22, 2013 10:41 pm

thanks sir......... :) its now accepted.. by using flush() at the end only...
but sir can u please tell me why i should BufferedWriter instead of using PrintWriter...??

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Age Sort

Post by brianfry713 » Mon Feb 25, 2013 10:44 pm

I think BufferedWriter is faster.
Check input and AC output for thousands of problems on uDebug!

shaho
New poster
Posts: 1
Joined: Sat Dec 28, 2013 1:26 pm

Re: 11462 - Ages Sort

Post by shaho » Wed Jan 01, 2014 1:02 pm

Hi
How I can solve this problem using Java?
I used CountingSort , and used System.out.println(); only once.
But got TL :(

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11462 - Ages Sort

Post by brianfry713 » Wed Jan 15, 2014 12:24 am

Try using BufferedReader and BufferedWriter.
Check input and AC output for thousands of problems on uDebug!

ssavi
New poster
Posts: 28
Joined: Thu Nov 20, 2014 9:57 pm

Re: 11462 - Age Sort

Post by ssavi » Sun Dec 07, 2014 6:30 pm

Hi why i am getting RTE . Its Counting Sort algorithm . please give a suggestion .

Code: Select all

 Got AC
I know I am a Failure Guy . :(

Post Reply

Return to “Volume 114 (11400-11499)”