|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
以下两个代码有什么不一样吗?为什么结果差那么多?
#include <stdio.h>
#pragma warning(disable:4996)
struct Rectangle {
long long l, t, w, h;
} a[100005];
long long calc(long long k, long long n)
{
int area_left = 0, area_right = 0;
for (int i = 0; i <= n; i++) {
if (k > a.l + a.w) { //整个矩形都在直线左边
area_left += a.w * a.h;
}
else if (k <= a.l) { //整个矩形都在直线右边
area_right += a.w * a.h;
}
else { //直线穿过矩形
area_left += (k - a.l) * a.h;
area_right += (a.w + a.l - k) * a.h;
}
}
return area_left - area_right;
}
int main(void)
{
long long r, n, i;
long long left = 0, right, mid;
scanf("%lld%lld", &r, &n);
for (i = 0; i < n; i++) {
scanf("%lld%lld%lld%lld", &a.l, &a.t, &a.w, &a.h);
}
right = r;
while (left < right) {
mid = (right + left) / 2;
if (calc(mid,n) >= 0) {
right = mid;
}
else {
left = mid +1;
}
}
long long d = calc(left,n);
while (left < right && calc(left + 1,n) == d) left++;
printf("%lld", left);
return 0;
}
#include <stdio.h>
#pragma warning(disable:4996)
struct Rectangle {
long long l, t, w, h;
} a[100005];
long long calc(long long k, long long n)
{
int area_left = 0, area_right = 0;
for (int i = 0; i <= n; i++) {
if (k > a.l + a.w) { //整个矩形都在直线左边
area_left += a.w * a.h;
}
else if (k <= a.l) { //整个矩形都在直线右边
area_right += a.w * a.h;
}
else { //直线穿过矩形
area_left += (k - a.l) * a.h;
area_right += (a.w + a.l - k) * a.h;
}
}
return area_left - area_right;
}
int main(void)
{
long long r, n, i;
long long left = 0, right, mid;
scanf("%lld%lld", &r, &n);
for (i = 0; i < n; i++) {
scanf("%lld%lld%lld%lld", &a.l, &a.t, &a.w, &a.h);
}
right = r;
while (left < right) {
mid = (right + left) / 2;
if (calc(mid,n) >= 0) {
right = mid;
}
else {
left = mid +1;
}
}
long long d = calc(left,n);
while (left < right && calc(left + 1,n) == d) left++;
printf("%lld", left);
return 0;
} |
|