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

Moderator: Board moderators

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:

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

I think you should rewrite the cmp function because you can write it simply. Otherwise I have to go through the whole function, but I am lazy . If you cant find the any simple cmp function then PM me.
Ami ekhono shopno dekhi...
HomePage

lazyboy
New poster
Posts: 17
Joined: Tue Jul 08, 2008 3:19 am

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

Last edited by lazyboy on Mon Feb 23, 2009 8:44 pm, edited 2 times in total.

sreejond
New poster
Posts: 32
Joined: Fri May 23, 2008 6:16 pm
Contact:

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

i m very much bored with this problem.
lots of time i get WA.
i pass all inputs available here.
CAN ANY ONE HELP ME????

here is my code:

Code: Select all

#include<stdio.h>
#include<stdlib.h>

long long temp,n,m,i;

struct T
{
long long a, b;
};

int sort(const void *aa, const void *bb)
{
T *p = (T *)aa;
T *q = (T *)bb;

if( p->b == q->b)
{
if(p->a%2==0 && q->a%2==0)
{
if(p->a > q->a)
{
temp=p->a;
p->a=q->a;
q->a=temp;
return -1;
}
else
return -1;
}
else if(p->a%2!=0 && q->a%2!=0)
{
if(p->a < q->a)
{
temp=p->a;
p->a=q->a;
q->a=temp;
return -1;
}
else
return -1;
}
else
{
if(p->a%2==0)
{
temp=p->a;
p->a=q->a;
q->a=temp;
return -1;
}
else
return -1;
}
}

else if(p->b > q->b)
return 1;

else
return -1;
}
T s[10001];
int main()
{

while(scanf("%lld%lld",&n,&m)==2)
{
if(n==0 && m==0)
{
printf("%lld %lld\n",n,m);
break;
}

for(i=0;i<n;i++)
{
scanf("%lld",&s[i].a);
s[i].b=s[i].a%m;
}

qsort(s,n,sizeof(T),sort);

printf("%lld %lld\n",n,m);
for(i=0;i<n;i++)
printf("%lld\n",s[i].a);
}
return 0;
}

lazyboy
New poster
Posts: 17
Joined: Tue Jul 08, 2008 3:19 am

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

Please somebody help me with this problem. I got WA for many times. my code passes for all test case given. but this got WA for everytime. Please help me....
code given below:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

long n,m;

long mod( long a, long b)
{
return a%b;
}

long cmp(long *a,long *b)
{
long rm,rm2;
long od,ev;
long *x=(long *)a;
long *y=(long *)b;
rm=*a%m;
rm2=*b%m;
od=abs(mod(*a,2));
ev=abs(mod(*b,2));
if(rm<rm2)
return -1;
else if(rm>rm2)
return 1;
else
{
if(od!=ev)
{
if(od==1)
return -1;
else
return 1;
}
else
{
if(od==1)
return *y-*x;
else
return *x-*y;
}
}
}

int main()
{
long i;
long data[10100];
while(1)
{
scanf("%ld %ld",&n,&m);
if(n==0&&m==0)
break;
for(i=0;i<n;i++)
scanf("%ld",&data[i]);
printf("%ld %ld\n",n,m);
qsort(data,n,sizeof(long),(int (*) (const void *,const void *))cmp);
for(i=0;i<n;i++)
printf("%ld\n",data[i]);
}
printf("0 0\n");
return 0;
}

liauys
New poster
Posts: 7
Joined: Thu Jul 02, 2009 6:37 pm

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

Hello friends. Finally got the AC. For those who are getting WA, check if you have used something like this:

int upcomp(const void *a, const void *b){
return (*(int *)a) - (*(int*)b);
}

This will cause overflow can gives wrong output! I have been using this for quite a long time though . Use ">" and "<" to compare instead of this.

seraph
New poster
Posts: 9
Joined: Tue Dec 15, 2009 4:18 pm

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

finally, i got AC...
i just use

Code: Select all

bool cmp(data a, data b)
{
if (a.mod==b.mod)
if (a.stat==b.stat)
if (a.stat==1)
return a.angka > b.angka;
else
return a.angka < b.angka;
return a.stat < b.stat;
return a.mod < b.mod;
}
and

Code: Select all

sort(v.begin(),v.end(),cmp);

arifcsecu
Learning poster
Posts: 64
Joined: Fri Sep 25, 2009 11:29 am
Location: Chittagong,University of chittagong
Contact:

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

I got a couple of Wrong Answer. My code pass all the test found in this thread

Code: Select all

#include<iostream>
#include<cstdio>

#include<algorithm>

using namespace std;

long int m;

bool cmp(long int a,long int b)
{
long int ra,rb,p,q;
if(a<0)
p=(-1)*a;
else
p=a;

if(b<0)
q=(-1)*b;
else
q=b;

ra=a%m;
rb=b%m;

if(ra==rb)
{
if(p%2 ==0 && q%2==0)
return (a<b);
else if(p%2==1 && q%2==1)
return (a>b);
else if(p%2==1 && q%2==0)
return true;
else if(p%2==0 && q%2==1)
return false;
}
else if(ra>rb)
return false;
else
return true;

}

int main()
{

long int i,a,n;
vector<long int> aa;
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);

while(scanf("%ld %ld",&n,&m)==2)
{
pf("%ld %ld\n",n,m);

if(n==0 && m==0) break;

aa.clear();

rep(i,n)
{
sf("%ld",&a);
aa.pb(a);
}

sort(all(aa),cmp);

rep(i,n)
pf("%ld\n",aa[i]);

}
re 0;
}

Try to catch fish rather than asking for some fishes.

Shafaet_du
Experienced poster
Posts: 147
Joined: Mon Jun 07, 2010 11:43 am
Contact:

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

I also got many RTE's before finding the mistake.

if(abs(a)%2==1 && abs(b)%2==1) //correct,ac

if(a%2=1 && b%2==1) //RTE

Hope this helps .

plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

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

And a whole lot of inputs...

Code: Select all

100 1
98996523
702138029
966844567
-866845910
-617887192
1929179531
-722252290
-494624710
-897196254
-1995391956
289115659
-2098754920
1057895599
-1771772912
1563686945
-1709067956
-1989092828
-187112734
-640506788
172581791
-2095032708
-2118368114
1336775175
1572992475
181028349
2045427075
-99497590
-1338421538
-139582950
-136862872
-711944626
-78023290
1810784557
1267627929
-1115375142
-1372923580
239724769
1732904429
-1381513300
-687607086
732631535
1575998877
951812557
174729221
-77593804
84680323
1341213197
672789819
-1889881562
-2031755104
1188173019
1407640365
-787391000
-938140586
-44952868
-61559660
1241429283
1390604087
-1095189300
1880933937
-127271018
-737427462
318463869
953101015
-173798668
1776855163
686247047
-1161043820
183891589
215959877
1811070881
-71724162
-451962434
2095676937
-1715510246
920746403
932056201
-887461238
-159052982
1750513355
89118345
146239983
1933760715
-1063836822
134357537
-657113580
1142933827
-1887590970
1966115327
2048433477
1364262279
2006057525
-1924813090
281098587
1110722377
1515584513
-113384304
924611777
1835408421
1386309227
100 97
-105080908
1941491463
1376001563
1085525865
1049878527
-53113102
-1823740718
2083365005
1134630431
133641727
13815133
33285165
40443265
2073916313
-645517458
1139211615
-297776960
-1442357150
-1702053018
1734336049
82962379
437717815
1134200945
554538007
1359394771
-1516944552
-80457044
-1509070642
2120443963
-348456308
-1605848154
-1688595790
-1936695536
-74873726
-1275000772
1253598053
1720163011
-1393825232
521324423
1068919073
-1534696640
251607215
-2029750836
-82174988
163562585
-948018764
-2102333970
-1688595790
-492477280
1816797361
1697972901
477803175
-1193827918
1583872787
-1131409286
166139501
-1568196548
-1474425438
-1079870966
-313811104
-456829942
1186884561
-882736892
-182388388
89690993
-144593620
364132547
1143792799
1964254221
953959987
853460263
-1019313440
-199281504
149532709
-105224070
17107859
-291907318
784599341
-1207571470
647736469
899415265
-1155889988
-1304635306
-1493036498
-1757313550
829409047
-476299974
-1636198498
1156247893
-1283161006
1720449335
1453452205
-1523386842
-11452960
-587966334
1543787427
605074193
1033414897
-235930976
1256031807
100 7982
109304187
1717013447
-937567938
-1597687920
266352901
223261139
-1142862246
-657256742
-1299051988
1396187405
21402719
-90048898
418677269
135645995
1092397641
-22905920
-2013716692
-1726820044
1715295503
1552520309
-519821222
-1619734868
1715581827
210233397
-1410145700
-1692318002
-1536271422
-1627179292
-922535928
812515931
781306615
775580135
-1121674270
-176805070
663770613
1892673221
-2023308546
-256975790
1730041189
971998399
-466135472
-958612752
-1332551896
-1847935096
-789967916
1545648533
1185739265
2030967713
1329330751
-405005298
-1329259170
-555754884
1655596949
-587393686
-216460944
1527037473
-1614008388
-89046764
-708365576
148673737
1904698829
-956751646
1814649931
937353195
-709940358
2107416221
1764972717
229703429
-808006328
-562769822
-1633907906
-592690680
-565633062
-1173355752
116319125
-1401699142
-2013859854
893831947
129203705
786460447
-17036278
-1631187828
688680801
1609785109
11810865
-1309502814
917024191
2003480609
1136921023
-753318444
-1786661760
1612218863
-884597998
-1835050516
-45668678
1924168861
-1804270686
-1650085212
1293683413
1346223867
0 0

Code: Select all

100 1
2095676937
2048433477
2045427075
2006057525
1966115327
1933760715
1929179531
1880933937
1835408421
1811070881
1810784557
1776855163
1750513355
1732904429
1575998877
1572992475
1563686945
1515584513
1407640365
1390604087
1386309227
1364262279
1341213197
1336775175
1267627929
1241429283
1188173019
1142933827
1110722377
1057895599
966844567
953101015
951812557
932056201
924611777
920746403
732631535
702138029
686247047
672789819
318463869
289115659
281098587
239724769
215959877
183891589
181028349
174729221
172581791
146239983
134357537
98996523
89118345
84680323
-2118368114
-2098754920
-2095032708
-2031755104
-1995391956
-1989092828
-1924813090
-1889881562
-1887590970
-1771772912
-1715510246
-1709067956
-1381513300
-1372923580
-1338421538
-1161043820
-1115375142
-1095189300
-1063836822
-938140586
-897196254
-887461238
-866845910
-787391000
-737427462
-722252290
-711944626
-687607086
-657113580
-640506788
-617887192
-494624710
-451962434
-187112734
-173798668
-159052982
-139582950
-136862872
-127271018
-113384304
-99497590
-78023290
-77593804
-71724162
-61559660
-44952868
100 97
-1523386842
-1283161006
-1019313440
-144593620
-1275000772
-1534696640
-1493036498
-82174988
-291907318
-1636198498
-53113102
-11452960
-1568196548
-1823740718
-182388388
-1516944552
-1155889988
-1605848154
-587966334
-297776960
-1757313550
-1304635306
-199281504
-1207571470
-1079870966
-1936695536
-492477280
-2029750836
-1442357150
-1702053018
-882736892
-105080908
-948018764
-1193827918
-1509070642
-1393825232
-1131409286
-1474425438
-105224070
-645517458
-235930976
-2102333970
-74873726
-348456308
-80457044
-456829942
-1688595790
-1688595790
-313811104
-476299974
784599341
33285165
2120443963
953959987
13815133
1734336049
1720449335
1359394771
1376001563
163562585
1134200945
1068919073
82962379
1697972901
1253598053
605074193
1720163011
1543787427
1085525865
1186884561
899415265
149532709
1143792799
1453452205
166139501
1156247893
853460263
89690993
1583872787
1964254221
1256031807
2073916313
647736469
521324423
1139211615
554538007
17107859
1816797361
829409047
2083365005
133641727
364132547
437717815
251607215
1033414897
40443265
1941491463
477803175
1134630431
1049878527
100 7982
-1309502814
-1536271422
-89046764
-1142862246
-2023308546
-562769822
-405005298
-958612752
-1847935096
-1692318002
-587393686
-1650085212
-1410145700
-22905920
-1299051988
-789967916
-709940358
-956751646
-216460944
-1332551896
-1835050516
-565633062
-808006328
-1401699142
-90048898
-176805070
-1121674270
-45668678
-1619734868
-1804270686
-256975790
-592690680
-708365576
-657256742
-1597687920
-1786661760
-17036278
-466135472
-1631187828
-937567938
-1726820044
-2013716692
-1173355752
-519821222
-2013859854
-753318444
-884597998
-1329259170
-1627179292
-1633907906
-922535928
-555754884
-1614008388
1329330751
2107416221
1293683413
1764972717
148673737
1527037473
775580135
266352901
688680801
786460447
1904698829
1655596949
1715581827
21402719
937353195
663770613
210233397
1715295503
2030967713
1346223867
1924168861
917024191
812515931
781306615
223261139
1136921023
1092397641
1185739265
1892673221
1552520309
418677269
229703429
116319125
1717013447
11810865
1545648533
1814649931
971998399
1612218863
1730041189
2003480609
109304187
129203705
1609785109
893831947
135645995
1396187405
0 0
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

nickforspeed
New poster
Posts: 1
Joined: Mon May 21, 2012 6:12 pm

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

Hello Everybody
I got RuntimeError and i Don't know why... Se the code Below

Code: Select all

Remove AC
Thanks
Last edited by nickforspeed on Tue May 22, 2012 6:23 pm, edited 1 time in total.

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

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

Change line 20 to: if(a>=b)
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: 11321 - Sort! Sort!! and Sort!!!

Input

Code: Select all

4 2
-2000000000
-2000000001
2000000000
2000000001
0 0
Correct output:

Code: Select all

4 2
-2000000001
-2000000000
2000000000
2000000001
0 0
Check input and AC output for thousands of problems on uDebug!

lukai
New poster
Posts: 25
Joined: Wed Dec 05, 2012 8:11 pm

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

I have gotten RTE couple of times.Can't find out why?

Code: Select all

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <cstring>
#include <typeinfo>
#include <exception>
#include <stdexcept>
#include <memory>
#include <fstream>
#include <string>
#include <sstream>
#include <functional>
#include <iterator>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cstdio>
#include <locale>
#include <limits>
#include <utility>

using namespace std;
int n,m;

bool comparision(int a,int b)
{
if((a%m)!=(b%m))
{
return (a%m)<(b%m);
}
if((a%m)==(b%m))
{
if((a%2!=0)&&(b%2==0))
{
if(a%2==-1)
{
return ((a%2<b%2));
}
else
{
return ((a%2)>(b%2));
}
}
else if((a%2==0)&&(b%2!=0))
{
if(b%2==-1)
{
return ((b%2)<(a%2));
}
else
{
return ((b%2)>(a%2));
}
}
else if((a%2==0)&&(b%2==0))
{
return a<b;
}
else if((a%2==1)&&(b%2==1)||(a%2==-1)&&(b%2==-1))
{
return a>b;
}
}
}
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int temp,i;
vector<int>input;
while(cin>>n>>m)
{
if(!m &&!n)
{
cout<<n<<" "<<m<<endl;
return 0;
}
cout<<n<<" "<<m<<endl;
while(n--)
{
cin>>temp;
input.push_back(temp);
}
sort(input.begin(),input.end(),comparision);
for(i=0;i<input.size();i++)
{
cout<<input[i]<<endl;
}
input.clear();
}
return 0;
}

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

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

Doesn't match the sample I/O.
Check input and AC output for thousands of problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

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

brianfry713, Jan, sapnil

Thanks for providing useful test cases for this problem!
Check input and AC output for over 7,500 problems on uDebug!