Page 4 of 4

Re: 11321 - Sort! Sort!! and Sort!!!

Posted: Sun Aug 31, 2014 12:44 pm
by incruator
Please.. Someone help..I m getting RTE ..I have tried every alternative.. Can't find what i m doing wrong

:( :(

Code: Select all

 #include<iostream>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;
pair<long long int,long long int>p[10005];
bool cmp(pair<long long int ,long long int>p1,pair<long long int ,long long int>p2)
{
	if(p1.first<p2.first)return true;
	  if(p1.first>p2.first)return false;
	if(p1.first==p2.first)
	{
		if(p1.second%2!=0 && p2.second%2!=0)
		{
			if(p1.second>p2.second)return true;
			else
			return false;
		}
		if(p1.second%2==0 && p2.second%2==0)
		{
			if(p1.second>p2.second)return false;
			else
			return true;
		}
		if(p1.second%2!=0 && p2.second%2==0)
		{
			return true;
		}
		else
		return false;
	}
}
int main()
{
	long int n,m;
	long long int  a;
	while(cin>>n>>m)
	{
	cout<<n<<" "<<m<<endl;
		if(n==0 && m==0)break;
//		for(int i=0;i<100005;i++)p[i].clear();
		for(long  int i=0;i<n;i++)
		{
			cin>>a;
			p[i]=make_pair(a%m,a);
		}
		sort(p,p+n,cmp);
		for(long int i=0;i<n;i++)
		{
			cout<<p[i].second<<endl;
		}
	}
	return 0;
}	

Please Help..Thanks
:( :(

Re: 11321 - Sort! Sort!! and Sort!!!

Posted: Mon Sep 01, 2014 1:17 pm
by incruator
Please someone reply.. I have posted my code.. It runs for all test cases in thread.. Giving RTE...
:evil: :evil: :evil: :evil: :evil:

Re: 11321 - Sort! Sort!! and Sort!!!

Posted: Wed Sep 03, 2014 9:01 pm
by brianfry713
Change your cmp function. Here is one that works:

Code: Select all

bool cmp(pair<long long int, long long int> p1, pair<long long int, long long int> p2) {
  if(p1.first < p2.first)
    return true;
  if(p1.first > p2.first)
    return false;
  if(abs(p1.second) % 2 != abs(p2.second % 2))
    return (abs(p1.second) % 2 > abs(p2.second) % 2);
  if(p1.second % 2 == 0)
    return (p1.second < p2.second);
  return (p1.second > p2.second);
}

Re: 11321 - Sort! Sort!! and Sort!!!

Posted: Tue Nov 18, 2014 5:09 pm
by gfv
I've been trying to solve this one for a while now, and it only gives me WA, even though it passed all test cases I could find for it.
I don't see what is missing for that to fit in Ac. Can anyone help?

Code: Select all

AC, just missing "\n" when printing the "0 0" line ¬¬

Re: 11321 - Sort! Sort!! and Sort!!!

Posted: Fri Feb 27, 2015 8:12 pm
by Hartha al-oufi
I got AC after 11 RTE .. my java code got all these RTE cuz my implementation of comparable was not perfect , in compareTo you should return 0 when numbers are equals ..don't ignore this part .

Re: 11321 - Sort! Sort!! and Sort!!!

Posted: Sun May 01, 2016 11:54 am
by saad
i got Time limit can anyone help me with my code ?

Code: Select all

 #include<iostream>
using namespace std;

int main()
{
    int a,b;
    while(cin >> a >> b)
    {
        if(a==0&&b==0)
        {
            cout << "0 0" << endl;
            break;
        }
        int ar[a];
        for(int i=0;i<a;i++)
        {
            cin >> ar[i];
        }
        for(int i=0;i<a;i++)
        {
            for(int j=i+1;j<a;j++)
            {
                if(ar[i]%b > ar[j]%b)
                {
                    int temp = ar[i];
                    ar[i] = ar[j];
                    ar[j] = temp;
                }
                if(ar[i]%b==ar[j]%b)
                {
                    if(ar[i]%2==0 && ar[j]%2!=0)
                    {
                        int temp = ar[i];
                        ar[i] = ar[j];
                        ar[j] = temp;


                    }
                    if(ar[i]%2==0&&ar[j]%2==0)
                    {
                        if(ar[i] > ar[j])
                        {
                            int temp = ar[i];
                            ar[i] = ar[j];
                            ar[j] = temp;
                        }
                    }
                    else if(ar[i]%2!=0&&ar[j]%2!=0)
                    {
                        if(ar[i] < ar[j])
                        {
                            int temp = ar[i];
                            ar[i] = ar[j];
                            ar[j] = temp;
                        }
                    }

                }
            }
        }
        cout << a << " " << b << endl;
        for(int i=0;i<a;i++)
        {
            cout << ar[i] << endl ;
        }
    }
    return 0;
}