1169C - Increasing by Modulo - CodeForces Solution


binary search greedy *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n, m; scanf("%d %d", &n, &m);
	vector<int> a(n + 1);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	
	auto check = [&] (int val)
	{
		auto b = a;
		
		for (int i = 1; i <= n; i++)
		{
			int need = (b[i - 1] - b[i] + m) % m;
			if (need <= val) b[i] = b[i - 1];
			else if (b[i] < b[i - 1]) return false;
 		}
 		return true;
	};
	
	int l = 0, r = m;
	while (l < r)
	{
		int mid = l + r >> 1;
		if (check(mid)) r = mid;
		else l = mid + 1;
	}
	printf("%d\n", r);
	
	return 0;
}
	 					  	 			 	 		 	 	  			  	


Comments

Submit
0 Comments
More Questions

844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs
900A - Find Extra One
1093D - Beautiful Graph
748A - Santa Claus and a Place in a Class
1511B - GCD Length
676B - Pyramid of Glasses
597A - Divisibility
1632A - ABC
1619D - New Year's Problem
242B - Big Segment
938A - Word Correction