Page 2 of 4

Re: 11462 - Ages Sort

Posted: Thu Sep 17, 2009 7:28 am
by ahmed
I am getting wrong answer for the code below.Can anyone please help me to identify the problem with my code?

Code: Select all

#include<stdio.h>

int age[2000000];

void counting(int num)
{
	int j;
	int c[101]={0};
	for(j=0;j<num;j++)
		c[age[j]]++;
	for(j=1;j<100;j++)
	{
		while(c[j])
		{
			printf("%d ",j);
			c[j]--;
		}
	}
	printf("\b\n");
}

int main(void)
{
	int n,i;
	scanf("%d",&n);
	while(n)
	{
		for(i=0;i<n;i++)
		{
			scanf("%d",(age+i));
		}
		counting(n);
		scanf("%d",&n);
	}
	return 0;
}

Re: 11462 - Ages Sort

Posted: Fri Sep 18, 2009 9:10 am
by joshua.bender
I use the merge sort function in java but still got TLE. Please help me.

Code: Select all

import java.util.*;
import java.io.*;

public class Main {

    public static void main(String args[]) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String tc = reader.readLine();
            int nc = Integer.parseInt(tc);
            if (nc == 0) {
                break;
            }
            int arry[] = new int[nc];
            String age = reader.readLine();
            StringTokenizer tokenizer = new StringTokenizer(age);
            for (int i = 0; i < nc; i++) {
                arry[i] = Integer.parseInt(tokenizer.nextToken());
            }
            Arrays.sort(arry);
             for (int i = 0; i < nc; i++) {
                System.out.print(arry[i]+" ");
            }
            System.out.println();

        }
    }
}

Re: 11462 - Ages Sort

Posted: Tue Nov 03, 2009 4:07 pm
by sms.islam
"For each case, print a line with n space separated integers"
whats that means? How many space ?

Re: 11462 - Ages Sort

Posted: Tue Nov 03, 2009 5:21 pm
by helloneo
sms.islam wrote:"For each case, print a line with n space separated integers"
whats that means? How many space ?
I think that means..

print n numbers and each number is separated by only one space

Re: 11462 - Ages Sort

Posted: Sun Dec 20, 2009 7:35 am
by masum.shafayat
What is real output format for this problem???????????????????????any body can help me?

Re: 11462 - Ages Sort

Posted: Fri Jul 02, 2010 11:14 pm
by receme
Thanks Sohel. I got ac now. :P

Re: 11462 - Ages Sort

Posted: Sat Jul 03, 2010 10:15 am
by sohel
printf("\b\n"); --> this won't work.

Re: 11462 - Ages Sort

Posted: Mon Aug 30, 2010 9:55 am
by noor_aub
Why I am getting Runtime error Why

Code: Select all

#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
void merge(int a[2000001], long low, long high, long mid);
void mergesort(int a[2000001], long low, long high)
{
    long mid;
    if(low<high)
    {
        mid=(low+high)/2;
        mergesort(a,low,mid);
        mergesort(a,mid+1,high);
        merge(a,low,high,mid);
    }
    //return(0);
}

void merge(int a[2000001],long low,long high,long mid)
{
    int i, j, k, c[50];
    i=low;
    j=mid+1;
    k=low;
    while((i<=mid)&&(j<=high))
    {
        if(a[i]<a[j])
        {
            c[k]=a[i];
            k++;
            i++;
        }
        else
        {
            c[k]=a[j];
            k++;
            j++;
        }
    }
    while(i<=mid)
    {
        c[k]=a[i];
        k++;
        i++;
    }
    while(j<=high)
    {
        c[k]=a[j];
        k++;
        j++;
    }
    for(i=low;i<k;i++)
    {
        a[i]=c[i];
    }
}
int main()
{
    freopen("in.txt","r",stdin);
    long cas;
    //cout<<"Size = "<<v.size()<<endl;
    while(1)
    {
        long i;
        int v[2000000];
        scanf("%ld",&cas);
        if(cas==0)
            break;
        for( i=0;i<cas;i++)
        {
            scanf("%d",&v[i]);
        }

        long mid= (long ) (0+cas-1)/2;
         mergesort(v,0,cas-1);
        for(i=0;i<cas;i++)
            printf("%d ",v[i]);
        printf("\n");
    }
    return 0;
}


Re: 11462 - Ages Sort

Posted: Tue Sep 07, 2010 11:37 pm
by gtcoder
Can anyone please tell me about the fast I/O? I know fread() fwrite() is what it is. But can u give me an example of it being done?

Re: 11462 - Ages Sort

Posted: Sun May 01, 2011 6:36 pm
by iriz7482
I've the same question as gtcoder (post above) :oops:

Re: 11462 - Ages Sort

Posted: Sun Nov 06, 2011 11:44 pm
by john_cu_cse
#include<stdio.h>
int main()
{
long long int a[2000005],b,l,c,i,j,k,n;
while(scanf("%lld",&n)==1)
{
if(n==0)
break;
for(i=0;i<n;i++)
{
scanf("%lld",&a);
}
for(j=0;j<n-1;j++)
{
for(k=0;k<n-j-1;k++)
{
if(a[k]>=a[k+1])
{
c=a[k];
a[k]=a[k+1];
a[k+1]=c;
}
}
}
for(l=0;l<n;l++)
{
printf("%lld ",a[l]);
}
}
return 0;
}



//...why i got always run time error? Plz give me a solution...//

Re: 11462 - Ages Sort

Posted: Fri Dec 23, 2011 2:51 pm
by tzupengwang
the following is my AC code
in this situation, even "sort" function in C++ can't work, because of its large data(25MB)
so I use another way because the data "n" is between 1 & 100(1<=n<=100)
wish i could help~

Code: Select all

#include<stdio.h>
int str[100];
void a()
{
 int k;
 for(k=0;k<100;k++)str[k]=0;
}

int main()
{
 long long int time;
 while(scanf("%lld",&time)==1)
     {
      int flag=1;      
      if(time==0)flag=0;
      int tim=time;
      a();    
      while(time--)
          {
           int b;
           scanf("%d",&b);
           str[b-1]++;   
          }
      if(!flag)break;     
      int i;
      int p=1;
      for(i=0;i<100;i++)
         {          
          while(str[i]--){
                          if(p==tim)printf("%d",i+1);
                          else {printf("%d ",i+1);p++;}
                         }
         }      
      puts("");   
     }
 return 0;   
}

Age Sort

Posted: Wed Apr 25, 2012 5:10 pm
by pollob_28
#include <stdio.h>
#include <math.h>
#include <conio.h>

int array [2000000];

void merge(int* input, int p, int r)
{
int mid = (int)floor((p + r) / 2);
int i1 = 0;
int i2 = p;
int i3 = mid + 1;

int temp[r-p+1];

while ( i2 <= mid && i3 <= r )
if ( input[i2] < input[i3] )
temp[i1++] = input[i2++];
else
temp[i1++] = input[i3++];

while ( i2 <= mid )
temp[i1++] = input[i2++];

while ( i3 <= r )
temp[i1++] = input[i3++];

for ( int i = p; i <= r; i++ )
input = temp[i-p];
}

void merge_sort(int* input, int p, int r)
{
if ( p < r )
{
int mid = (int)floor((p + r) / 2);
merge_sort(input, p, mid);
merge_sort(input, mid + 1, r);
merge(input, p, r);
}
}

int main (){

freopen ("age.txt", "r", stdin);
int test;

scanf ("%d", &test);

while (test !=0){

for (int i =1; i <=test; i++)
scanf ("%d", &array );

merge_sort (array, 1, test);

for (int i =1; i<=test; i++)
printf ("%d ", array );

printf ("\n");

scanf ("%d", &test);
}
getch();
return 0;
}
i don't know why i am getting presentation error
thanks in advance

Re: Age Sort

Posted: Thu Apr 26, 2012 2:05 am
by brianfry713
Don't print a space at the end of a line.

Re: 11462 - Ages Sort

Posted: Wed May 23, 2012 9:34 pm
by SyFyKid
Solved this problem in JAVA in 1.848

How to solve it faster?

Code: Select all

import java.awt.event.PaintEvent;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.awt.geom.*;
import java.util.regex.*;
import java.util.StringTokenizer;

public class Main implements Runnable {
    void solve() {
        console(true);//System.getProperty("ONLINE_JUDGE") != null);

        int n, last;
        int[] data = new int[101];

        while((n=RI())!=0){
            last = 0;
            for(int i=1; i<=n; i++) {
                int tmp = RI();
                data[tmp]++;
                last = tmp>last ? tmp : last;
            }

            for(int i=1; i<=100; i++) {
                for(int j=1; j<=data[i]; j++){
                    out.print((i==last && j==data[i]) ? i+"\n" : i+" ");
                }

                data[i] = 0;
            }
        }
    }

    StringTokenizer st;
    PrintWriter out;
    BufferedReader br;
    boolean eof = false;

    public static void main(String[] args) {
        new Thread(new Main()).start();
    }

    String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }

    String RLine() {
        String ret;
        try {
            ret = br.readLine();
        } catch (Exception e) {
            ret = "";
        }
        if (ret == null) {
            eof = true;
            return null;
        }
        return ret;
    }

    int RC() {
        try {
            return br.read();
        } catch (Exception e) {
            return -1;
        }
    }

    String RS() {
        return nextToken();
    }

    int RI() {
        return Integer.parseInt(nextToken());
    }

    long RL() {
        return Long.parseLong(nextToken());
    }

    double RD() {
        return Double.parseDouble(nextToken());
    }

    void console(boolean f) {
        if (f) {
            br = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
        } else {
            try {
                File input = new File("input.txt");
                if (!input.exists()) {
                    input.createNewFile();
                }

                br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
                out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("output.txt")));
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(111);
            }
        }
    }

    @Override
    public void run() {
        solve();
        out.close();
    }
}