小白求助
题目描述平面上有一个大矩形,其左下角坐标(0,0),右上角坐标(R,R)。大矩形内部包含一些小矩形,小矩形都平行于坐标轴且互不重叠。所有矩形的顶点都是整点。要求画一根平行于y轴的直线x=k(k是整数) ,使得这些小矩形落在直线左边的面积必须大于等于落在右边的面积,且两边面积之差最小。并且,要使得大矩形在直线左边的的面积尽可能大。注意:若直线穿过一个小矩形,将会把它切成两个部分,分属左右两侧。
输入
第一行是整数R,表示大矩形的右上角坐标是(R,R) (1 <= R <= 1,000,000)。
接下来的一行是整数N,表示一共有N个小矩形(0 < N <= 10000)。
再接下来有N 行。每行有4个整数,L,T, W 和 H, 表示有一个小矩形的左上角坐标是(L,T),宽度是W,高度是H (0<=L,T <= R, 0 < W,H <= R). 小矩形不会有位于大矩形之外的部分。
输出
输出整数n,表示答案应该是直线 x=n。 如果必要的话,x=R也可以是答案。
样例输入 复制
1000
2
1 1 2 1
5 1 2 1
样例输出 复制
5
#include <stdio.h>
#pragma warning (disable:4996)
#define LEFT 0
#define WIDTH 2
#define HEIGHT 3
int area_diff(int left,int right,int mid,int rectangle[],int N)
{
int area_left = 0, area_right = 0;
int i;
for (i = 0; i < N; i++) {
if (rectangle + rectangle <= mid) {
area_left += rectangle * rectangle;
}
else if (rectangle > mid) {
area_right += rectangle * rectangle;
}
else {
area_left += (mid - rectangle) * rectangle;
area_right += (rectangle + rectangle - mid) * rectangle;
}
}
return area_left - area_right;
}
int max_found(int rectangle[],int N ,int R)
{
int left = 0, right = R;
int mid,result=0;
while (left <= right) {
mid = (left + right) / 2;
int diff = area_diff(left,right,mid, rectangle,N), result;
if (diff >= 0) {
result = mid;
left = mid;
}
else {
right = mid;
}
}
return result;
}
int main(void)
{
int R, N,i,j;
int rectangle;
scanf("%d%d", &R, &N);
for (i = 0; i < N; i++) {
for (j = 0; j < 4; j++) {
scanf("%d", &rectangle);
}
}
i = max_found(rectangle, N,R);
printf("%d", i);
return 0;
}请问一下这个C语言程序错在哪里呀 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEFT 0
#define TOP 1
#define WIDTH 2
#define HEIGHT 3
int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
int main() {
int R, N, i, j;
int rectangle;
int x, len = 0;
scanf("%d%d", &R, &N);
for (i = 0; i < N; i++) {
for (j = 0; j < 4; j++) {
scanf("%d", &rectangle[ i]);
if (j == LEFT || j == LEFT + WIDTH) {
x = rectangle[ i];
x = rectangle[ i] + rectangle[ i];
}
}
}
qsort(x, len, sizeof(int), compare);
int result = 0, max_area = 0;
for (i = 0; i < len - 1; i++) {
int mid = (x[ i] + x) / 2;
int area_left = 0, area_right = 0;
for (j = 0; j < N; j++) {
if (rectangle < mid && rectangle + rectangle > mid) {
int h = rectangle;
int w1 = mid - rectangle;
int w2 = rectangle + rectangle - mid;
area_left += (w1 < w2 ? w1 : w2) * h;
} else if (rectangle + rectangle <= mid) {
area_left += rectangle * rectangle;
} else {
area_right += rectangle * rectangle;
}
}
if (area_left >= area_right && max_area < area_left) {
max_area = area_left;
result = mid;
}
}
printf("%d\n", result);
return 0;
}
输入代码不对呀{:10_266:}
页:
[1]