847E - Packmen - CodeForces Solution


binary search dp *1800

Please click on ads to support us..

C++ Code:

// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;

int N;
string line;

bool ok(int mid) {
	int p = -1, a = -1;
	int pUsed = -1;
	for (int i = 0; i < N; i++) {
		if (line[i] == '*') {
			if (a == -1) {
				if (p != -1) {
					bool cover;
					if (pUsed > 0) {
						int pToUse = i - p;
						int minUse = min(pUsed, pToUse);
						int maxUse = max(pUsed, pToUse);
						if (2*minUse+maxUse <= mid) cover = true;
						else cover = false;
					} else {
						cover = (i-p <= mid);
					}
					if (!cover) {
						p = -1;
						pUsed = -1;
						a = i;
					}
				} else {
					a = i;
				}
			}
		} else if (line[i] == 'P') {
			if (p == -1) {
				p = i;
				if (a != -1) {
					if (i - a > mid) return false;
					pUsed = (i-a);
					if (mid-pUsed <= 1) {
						p = -1;
						pUsed = -1;
					}
					a = -1;
				} else {
					pUsed = -1;
				}
			} else {
				p = i;
				pUsed = -1;
			}
		}
		//cout << mid << ", " << i << ": " << p << "/" << a << "/" << pUsed << endl;
	}
	if (a > 0) return false;
	return true;
}

int main() {
	cin >> N >> line;
	int lo = 0, hi = N*2;
	while (lo < hi) {
		int mid = lo + (hi-lo)/2;
		if (ok(mid)) {
			hi = mid;
		} else {
			lo = mid+1;
		}
	}
	cout << lo << endl;
}


Comments

Submit
0 Comments
More Questions

426A - Sereja and Mugs
363A - Soroban
1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys
1725A - Accumulation of Dominoes
1675E - Replace With the Previous Minimize
839A - Arya and Bran
16B - Burglar and Matches
1625B - Elementary Particles
1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation
1437B - Reverse Binary Strings
1362B - Johnny and His Hobbies
1299A - Anu Has a Function
1111A - Superhero Transformation
954A - Diagonal Walking
39F - Pacifist frogs
1451C - String Equality
386A - Second-Price Auction
1690E - Price Maximization
282B - Painting Eggs
440A - Forgotten Episode
233B - Non-square Equation