Dev Skill Divided by Whom ?

//Next Codeforces Round #354 (Div. 2)
#include<bits/stdc++.h>

//#include<cstdio>
//#include<iostream>
//#include<algorithm>
//#include<vector>
//#include<cstring>
//#include<cmath>
//#include<map>

using namespace std;

#define fast ios_base::sync_with_stdio(false)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define high 100005

typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef map<LL , bool>mpllb;
typedef map<LL , LL>mpLLi;
typedef map<int, multiset<int> >mmset;
typedef multiset<int>mset;
typedef pair<int , int>pii;

int ar[high];

pii pr[high];

int main()
{
    int n, k , i , d , t , tc=0 , prlen=0;

    scanf("%d", &t);

    mmset mp;

    mset::iterator it;

    while(t--)
    {
        mp.clear();

        prlen=0;

        bool f=true;

        scanf("%d %d", &n , &k);

        for(i=0; i<n; i++)
        {
            scanf("%d", &ar[i]);

            mp[ar[i]%k].insert(ar[i]);
        }

        sort(ar, ar+n);

        for(i=0; i<n; i++)
        {
            it = mp[ar[i]%k].find(ar[i]);

            if(it == mp[ar[i]%k].end())
            {
                continue;
            }

            d = k - (ar[i] % k);

            if(d == k)
            {
                d = 0;
            }

            if(mp[d].empty())
            {
                f=false;
                break;
            }

            pr[prlen].first = *it;

            mp[ar[i]%k].erase(it);

            pr[prlen].second = *mp[d].begin();

            mp[d].erase(mp[d].begin());

            prlen++;
        }

        printf("Case %d:\n" , ++tc);

        if(!f)
        {
            printf("Not Possible\n");
        }

        else
        {
            for(i=0; i<prlen; i++)
            {
                printf("%d %d\n" , pr[i].first , pr[i].second);
            }
        }
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number