1157F - Maximum Balanced Circle - CodeForces Solution


constructive algorithms dp greedy two pointers *2000

Please click on ads to support us..

C++ Code:

/*
Problem: 1157F
Date: 16-01-2024 05:00 AM
*/


#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

int n;
int a[200000];
multiset<int> s;

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a, a + n);
	int l = 0;
	int maxlen = 1;
	int maxidx = 0;
	for(int i = 1; i < n; i++) {
		if(a[i] > a[i - 1] + 1) {
			if(i - l > maxlen) {
				maxlen = i - l;
				maxidx = l;
			}
			l = i;
		}else if(a[i] == a[i - 1] + 1) {
			int j = 0;
			for(j = 0; i + j < n && a[i + j] == a[i]; j++);
			if(j == 1) {
				if(i + j - l > maxlen) {
					maxlen = i + j - l;
					maxidx = l;
				}
				l = i + j - 1;
			}
			i = i + j - 1;
		}
	}
	if(n - l > maxlen) {
		maxlen = n - l;
		maxidx = l;
	}
	cout << maxlen << endl;

	int mymin = a[maxidx];
	int mymax = a[maxidx + maxlen - 1];
	for(int i = 0; i < maxlen; i++) {
		s.insert(a[maxidx + i]);
	}
	for(int i = mymax; i > mymin; i--) {
		cout << i << " ";
		s.erase(s.find(i));
	}
	while(!s.empty()) {
		cout << *s.begin() << " ";
		s.erase(s.begin());
	}
}


Comments

Submit
0 Comments
More Questions

1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game