401C - Team - CodeForces Solution


constructive algorithms greedy implementation *1400

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <queue>
#include <unordered_set>
#include <set>
#include <unordered_map>
//#define __DEV__
#include <algorithm>
#include <string>

using namespace std;


long long solve(vector<int>& arr) {

	sort(arr.begin(), arr.end());
	priority_queue<pair<int, int>> go;

	int prev_num = 0;
	for (int i = arr.size() - 1; i >= 0; i++) {
		int j = i - 1;
		while (j >= 0 && arr[j] == arr[i]) {
			j--;
		}

		go.push(pair<int, int>(i - j, arr[i]));
		i = j - 1;
	}

	int total = 0;
	int current_length = arr.size() - 1;
	while (go.size() > 1) {
		pair<int, int> ne = go.top();
		go.pop();


		pair<int, int> ne_next = go.top();
		go.pop();

		total++;
		ne.first--;
		if (ne.first > 0) {
			go.push(ne);
		}

		ne_next.first--;
		if (ne_next.first > 0) {
			go.push(ne_next);
		}
	}

	return arr.size() - (total * 2);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifdef  __DEV__
	ifstream in("in.txt");
	cin.rdbuf(in.rdbuf());

	ofstream out("out.txt");
	cout.rdbuf(out.rdbuf());
#endif //  __DEV__



	int t;
	//cin >> t;

	int n,  m;
	cin >> n >> m;
	int total = n + m;
	if (n > m + 1 || (n + 1) * 2 < m) {
		cout << -1;
	}
	else {
		bool prev_one = false;
		bool prev_two_one = false;
		for (int i = 0; i < total; i++) {
			if (m >= n && !prev_two_one) {
				cout << 1;
				if (prev_one) {
					prev_two_one = true;
				}
				else {
					prev_one = true;
				}
				m--;
			}
			else {
				cout << 0;
				prev_one = false;
				prev_two_one = false;
				n--;
			}
		}
	}

}


Comments

Submit
0 Comments
More Questions

1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent