1799B - Equalize by Divide - CodeForces Solution


brute force constructive algorithms greedy math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_po?licy.hpp>

using namespace std;
// using namespace __gnu_pbds;

#pragma GCC optimize ("unroll-loops,Ofast,O3")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")

#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define NFS ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
//#define int long long

// template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/*
order_of_key (k) : Number of items strictly smaller than k .
find_by_order(k) : K-th element in a set (counting from zero).
*/

typedef long long ll;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// template <class T> T rand(T l, T r) {
	// return (uniform_int_distribution<T>(l, r))(rng);
// }

#define int long long 
const int N = 5e5 + 2;
const int MOD = 1e9 + 7;


void solve() {
	int n;
	cin >> n;
	int a[n + 1];
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
	} 
	
	bool ok = 1;
	vector<pair<int,int>> res;
	while(ok && sz(res) <= 30 * n) {
		ok = false;
		int pos = min_element(a + 1, a + 1 + n) - a;
		for(int i = 1; i <= n; ++i) {
			if (a[pos] != a[i]) {
				a[i] = (a[i] + a[pos] - 1) / a[pos];
				res.pb({i, pos});
			}
		}
		
		for(int i = 1; i < n; ++i) {
			ok |= (a[i] != a[i+1]);
		}
	}
	
	for(int i = 1; i < n; ++i) {
		if (a[i] != a[i+1]) {
			cout << "-1\n";
			return;
		}
	}
	
	
	cout << sz(res) << '\n';
	for(auto [l, r] : res) {
		cout << l << ' ' << r << '\n';
	} 

}


signed main() {
	NFS;
	int test = 1;
	cin >> test;
	for(int i = 1; i <= test; ++i){
		solve();
	}
}







Comments

Submit
0 Comments
More Questions

97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill