67E - Save the City - CodeForces Solution


geometry *2500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	long long x0, y0, x1, y1;
	cin >> x0 >> y0 >> x1 >> y1;
	bool left = x0 > x1;
	long long Y = y0;
	int px = x1, py = y1;
	for (int i = 2; i < n; i++) {
		int x, y;
		cin >> x >> y;
		long long a = ((long long) Y - py) * (x - px);
		long long b = (y - py);
		if (b != 0) {
			if (left){
				long long X = px + (Y-py)*(x-px)/(y-py);
				if (abs(Y - py) < abs(Y - y)){
					if (a % b) {
						if ((a > 0 && b > 0) || (a < 0 && b < 0))
							X += 1;
					}
					x1 = max(x1, X);
				} else {
					if (a % b) {
						if ((a > 0 && b < 0) || (a < 0 && b > 0))
							X -= 1;
					}
					x0 = min(x0, X);
				}
			} else {
				long long X = px + (Y-py)*(x-px)/(y-py);
				if (abs(Y - py) < abs(Y - y)){
					if (a % b) {
						if ((a > 0 && b < 0) || (a < 0 && b > 0))
							X -= 1;
					}
					x1 = min(x1, X);
				} else {
					if (a % b) {
						if ((a > 0 && b > 0) || (a < 0 && b < 0))
							X += 1;
					}
					x0 = max(x0, X);
				}
			}
		} else {
			if (left) {
				if (px > x){
					cout << "0\n";
					return 0;
				}
			} else {
				if (px < x) {
					cout << "0\n";
					return 0;
				}
			}
		}
		px = x;
		py = y;
	}
	if (left){
		if (x0 - x1 >= 0) cout << x0 - x1 + 1 << '\n';
		else cout << "0\n";
	} else {
		if (x1 - x0 >= 0) cout << x1 - x0 + 1 << '\n';
		else cout << "0\n";
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading