Friday, August 12, 2016

Uva 11321: Sort! Sort!! and Sort!!!

Problem Link:
                    [https://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&page=show_problem&category=&problem=2296&mosmsg=Submission+received+with+ID+17829695]

Catagory: Sort,STL Algorithm

Reading Material: About how to create own comparator in stl sort function
                            [http://www.cplusplus.com/reference/algorithm/sort/]

Strategy: Just create own comparator according to the condition given in problem statement. Just Make sure that  -x%y  is  -o, not o,

Code:

#include<bits/stdc++.h>
#define ll unsigned long long int
using namespace std;
vector<int>o,e,d,nd;
int n,p,q,m;
bool cmp(int x, int y)
{
if(x%m>y%m)
return false;
else if(x%m<y%m)
return true;
if(x%2 && y%2)
return x>y;
else if(x%2==0 && y%2==0)
return x<y;
return x%2;
}
int main()
{
while(cin>>n>>m)
{ d.clear();
if(!n && !m)
break;
for(int i=1; i<=n; i++)
{
cin>>q;
d.push_back(q);
}
cout<<n<<" "<<m<<endl;
sort(d.begin(),d.end(),cmp);
for(int i=0; i<d.size(); i++)
cout<<d[i]<<endl;
}
cout<<n<<" "<<m<<endl;
}

No comments:

Post a Comment