data structures dp geometry *2400

Please click on ads to support us..

C++ Code:

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

struct Rect {
	ll x, y, a;
	bool operator<(Rect B) { return x < B.x; }
};

Rect a[1000001];
ll dp[1000001];

double slope(int i, int j) {
	return (double)(dp[i] - dp[j]) / (a[i].x - a[j].x);
}

int main() {
	iostream::sync_with_stdio(false);
	cin.tie(0);
	ll n;
	cin >> n;
	for (int i = 1; i <= n; i++) { cin >> a[i].x >> a[i].y >> a[i].a; }
	sort(a + 1, a + n + 1);

	deque<ll> q;
	q.push_back(0);
	for (int i = 1; i <= n; i++) {
		while (q.size() > 1 && slope(q[0], q[1]) >= a[i].y) q.pop_front();

		ll j = q.front();
		dp[i] =
		    max(dp[i - 1], a[i].x * a[i].y - a[i].a + dp[j] - a[j].x * a[i].y);

		while (q.size() > 1 &&
		       slope(q[q.size() - 2], q.back()) <= slope(q.back(), i))
			q.pop_back();
		q.push_back(i);
	}

	cout << dp[n];
	return 0;
}


Comments

Submit
0 Comments
More Questions

235A - LCM Challenge
1075B - Taxi drivers and Lyft
1562A - The Miracle and the Sleeper
1216A - Prefixes
1490C - Sum of Cubes
868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs
1626B - Minor Reduction
1680A - Minimums and Maximums
1713A - Traveling Salesman Problem
1713B - Optimal Reduction
1710A - Color the Picture
1686B - Odd Subarrays
251A - Points on Line
427C - Checkposts
1159A - A pile of stones
508A - Pasha and Pixels
912A - Tricky Alchemy
1249A - Yet Another Dividing into Teams
1713C - Build Permutation