Educational Codeforces Round 129 (Rated for Div. 2) 1681C. Double Sort Solution

 


Problem Link:  https://codeforces.com/contest/1681/problem/c

Solve in C++:  

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long  x,t,i,j,e,n;
    cin>>t;
    while(t--)
    {
        e=0;
        vector<pair<int,int>>r;
        cin>>n;
        long long a[n+9],b[n+9];
        for(i=1; i<=n; i++)cin>>a[i];
        for(i=1; i<=n; i++)cin>>b[i];
        if(is_sorted(a+1,a+n+1)&&(is_sorted(b+1,b+n+1)))
            cout<<"0"<<endl;
        else
        {
            for(i=1; i<=n; i++)
            {
                for( j=1; j<n; j++)
                {
                    if(a[j]>a[j+1] || b[j]>b[j+1])
                    {
                        swap(a[j],a[j+1]);
                        swap(b[j],b[j+1]);
                        r.push_back({j,j+1});
                    }
                }
            }
            e=0;
            for(i=1; i<n; i++)
            {
                if((a[i]>a[i+1])||(b[i]>b[i+1]))
                {
                    e=1;
                    break;
                }
            }
            if(e==1)
                cout<<"-1"<<endl;
            else
            {
                cout<<r.size()<<endl;
                for(i=0; i<r.size(); i++)
                cout<<r[i].first<<" "<<r[i].second<<endl;
            }
        }
    }
}

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.