584E codeforces - Anton and Ira



584E codeforces - Anton and Ira








Solution in C++



#include <bits/stdc++.h>
using namespace std;

//Hello World

typedef long long int lli;
typedef long double ld;
#define pii pair<int,int>
#define pllilli pair<lli,lli>
#define piiti pair< pair<int,int>,int>
#define ipii pair<int,pair<int,int> >
#define mod 1000000007
#define lasB(b) (b&(-b))
#define N 2003

int n , a[N] , b[N] , d[N] , sol;
vector<pii> all;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie();
    scanf("%d" ,&n);
    for(int i=0;i<n;i++)scanf("%d" , &a[i]);
    for(int i=0;i<n;i++){scanf("%d" , &b[i]);d[b[i]]=i;}
    while(1)
    {
        int cur =-1;
        for(int i=0;i<n;i++)
        {
            if(d[a[i]]<i)
            {
                cur = i;
                break;
            }
        }
        if(cur==-1)break;
        for(int i=cur-1;i>=0;i--)
        {
            if(d[a[i]]>i)
            {
                swap(a[i] , a[cur]);
                all.push_back({i+1,cur+1});
                sol+=cur-i;
                cur=i;
                if(cur == d[a[cur]])
                    break;
            }
        }
    }

    printf("%d\n%d\n" , sol , all.size());
    for(auto u : all)
        printf("%d %d\n" , u.first , u.second);
    return 0;
}
Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق