1681C - Double Sort - CodeForces Solution


implementation sortings *1200

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	tmp = [i for i in range(n)]
	tmp.sort(key=lambda i: [a[i], b[i]])
	for i in range(n - 1):
		if a[tmp[i]] > a[tmp[i + 1]] or b[tmp[i]] > b[tmp[i + 1]]:
			print("-1")
			break
	else:
		ans = []
		for i in range(n - 1):
			for j in range(n - 1):
				if a[j] > a[j + 1] or b[j] > b[j + 1]:
					a[j], a[j + 1] = a[j + 1], a[j]
					b[j], b[j + 1] = b[j + 1], b[j]
					ans.append([j + 1, j + 2])
		print(len(ans))
		for it in ans:
			print(*it)

C++ Code:

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

// -----------------------------------------------------------------

void test(){
    #ifndef ONLINE_JUDGE 
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
}

// -----------------------------------------------------------------

#define FastIO        cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
#define printv(v)     for(auto x:v){cout<<x<<" ";}cout<<endl;
#define dbg(x)        cout<<#x<<" = "<<x<<endl;
#define SQ(a)         (a)*(a)
#define PB            push_back
#define S             second
#define F             first


// -----------------------------------------------------------------

typedef pair<int,int>     ii;

typedef vector<int>       vi;
typedef vector<ii>        vii;

typedef vector<vi>        mi;
typedef vector<vii>       mii;

// --------------------------------------

typedef long long       ll;

typedef pair<ll, ll>   pll;

typedef vector< ll >    vll;
typedef vector< pll >   vpll;

typedef vector< vll >   mll;
typedef vector< vpll >   mpll;

typedef unsigned long long       ull;
typedef long double     ld;

// --------------------------------------

const ll INF = 9223372036854775807;
const int OO = 2147483647;
const ll MOD = 998244353;
const ld EPS = 1e-9;
const ld PI  = 3.141592653589793238;

const int TOW = 0b10;
const int FIFTEEN = 0xf;

int tc = 1;

// -----------------------------------------------------------------

const int N = 3e5 + 5e3;

ll n;
pair<ii,int> a[105], b[105];
vii ans;

// --------------------------------------



// --------------------------------------

void doTest(){
    cin>>n;
    for(int i = 0 ; i < n ; i ++){
        cin>>a[i].F.F;
        b[i].F.F = a[i].F.F;
    }
    for(int i = 0 ; i < n ; i ++){
        cin>>a[i].F.S;
        a[i].S = i+1;
        b[i].F.S = a[i].F.S;
    }
    sort(a, a+n);
    for(int i = 1 ; i < n ; i ++){
        if(a[i].F.S < a[i-1].F.S){
            cout<<-1<<endl;
            return;
        }
    }
    ans.clear();
    for(int i = 0 ; i < n-1 ; i ++){
        for(int j = 1 ; j < n-i ; j ++){
            if(b[j-1].F > b[j].F){
                ans.push_back({j, j+1});
                ii tmp = b[j].F;
                b[j].F = b[j-1].F;
                b[j-1].F = tmp;
            }
        }
    }
    cout<<ans.size()<<endl;
    for(int i = 0 ; i < ans.size() ; i ++){
        cout<<ans[i].F<<" "<<ans[i].S<<endl;
    }

}


// --------------------------------------

int main(){
    test();
    FastIO
    cout<<fixed<<setprecision(15);



    // scanf("%d",&tc);
    cin>>tc;
    while(tc--) doTest();
}

// -----------------------------------------------------------------


Comments

Submit
0 Comments
More Questions

461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List