11321 - Sort! Sort!! and Sort!!!

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

Moderator: Board moderators

incruator
New poster
Posts: 5
Joined: Sun Aug 31, 2014 12:32 pm

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

Post 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
:( :(
incruator
New poster
Posts: 5
Joined: Sun Aug 31, 2014 12:32 pm

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

Post 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:
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

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

Post 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);
}
Check input and AC output for thousands of problems on uDebug!
gfv
New poster
Posts: 6
Joined: Wed Jun 26, 2013 3:23 am

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

Post 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 ¬¬
Hartha al-oufi
New poster
Posts: 1
Joined: Fri Feb 27, 2015 7:53 pm

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

Post 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 .
saad
New poster
Posts: 1
Joined: Sun May 01, 2016 11:50 am

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

Post 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;
}
Post Reply

Return to “Volume 113 (11300-11399)”