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

Moderator: Board moderators

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

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

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

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

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

Please someone reply.. I have posted my code.. It runs for all test cases in thread.. Giving RTE...     brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

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

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!!!

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!!!

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

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

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;
}
``````