当然可以帮你写这个题目。以下是完整的C++代码:#include <iostream>#include <algorithm>#include <vector>using namespace std;struct Rectangle { int L, T, R, B; // Left, Top, Right, Bottom};int calculate_area_diff(const vector<Rectangle>& rects, int R, int k) { int left_area = 0, right_area = 0; for (const auto& rect : rects) { int left = max(rect.L, k); // 取左边界和k中较大的那个 int right = min(rect.R, R); // 取右边界和R中较小的那个 if (left < right) { left_area += (right - left) * (rect.B - rect.T); } if (rect.L < k && rect.R > k) { int overlap_height = min(rect.R, R) - max(rect.L, k); right_area += overlap_height * (rect.B - rect.T); } } return left_area - right_area;}int max_found(const vector<Rectangle>& rects, int R) { int left = 0, right = R, result = R; while (left <= right) { int mid = left + (right - left) / 2; int diff = calculate_area_diff(rects, R, mid); if (diff >= 0) { result = mid; left = mid + 1; } else { right = mid - 1; } } return result;}int main() { int R, N; cin >> R >> N; vector<Rectangle> rects(N); for (int i = 0; i < N; ++i) { cin >> rects[i].L >> rects[i].T >> rects[i].R >> rects[i].B; } int result = max_found(rects, R); cout << result << endl; return 0;}
你可以将此代码复制到C++编译器中进行编译和运行。希望对你有所帮助!如果有任何问题,请随时提问。 |