altahsh's blog

By altahsh, history, 16 months ago, In English

in problem D if https://codeforces.me/contest/2121/problem/D

N=50

array A is 50 49 48 47 46 .................1

array B is 100 99 98 97.......................

so we need 50*49*2 operation which is greater than 4900>1709

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
16 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

but $$$n\leq 40$$$ according to the constraints of the problem

»
16 months ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

Read the editorial, Imma CP paste it for ya.

Using bubble sort, we will sort array $$$a$$$: while there exists an index $$$1 \leq i \lt n$$$ such that $$$a_i \gt a_{i+1}$$$, we will swap them. Similarly, we will sort array $$$b$$$.

In this part of the task, we will perform no more than

$$$ n \times \frac{n-1}{2} + n \times \frac{n-1}{2} = n \times (n-1) $$$

operations, since the number of swaps in bubble sort is equal to the number of inversions in the array.

Next, for all $$$1 \leq i \leq n$$$ such that $$$a_i \gt b_i$$$, we will swap $$$a_i$$$ and $$$b_i$$$. After this, we will satisfy both conditions and perform a total of no more than

$$$ n \times (n-1) + n = n^2 $$$

operations.

The solution can be implemented in $$$O(n^2)$$$ time.

Code from the editorial:


#include<bits/stdc++.h> using namespace std; main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; vector<pair<int, int>> ans; for (int i = 0; i < n; i++) { for (int j = 1; j < n; j++) { if (a[j - 1] > a[j]) { swap(a[j - 1], a[j]); ans.push_back({1, j}); } } } for (int i = 0; i < n; i++) { for (int j = 1; j < n; j++) { if (b[j - 1] > b[j]) { swap(b[j - 1], b[j]); ans.push_back({2, j}); } } } for (int i = 0; i < n; i++) { if (a[i] > b[i]) { ans.push_back({3, i + 1}); } } cout << ans.size() << '\n'; for (auto [x, y] : ans) cout << x << " " << y << '\n'; } return 0; }

Hope this helps, Editorial Link : https://codeforces.me/blog/entry/143822