U can find the original link here
this question need us to find a line parallel to y axis that divide the given area of (0,0) to (R,R),witch satsifiy the two conditions:one is the part in left have more green land and make the difference between the two part as small as possible ,another is the left one has more always has more land the the one in the right
so we can scan the whole area to find a divide the green land space just in two ,and find if the line we find can expand .
question give us 1e6 parameter of R but only 1e4 greend land , we can search in the last one ,it’s a smart move
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e4+50;
typedef long long LL;
struct Node
{
int l,r,h;
};
int t,R,n;
Node a[maxn];
LL cal(int x)
{
LL sum=0;
for(int i=0; i<n; i++)
if(a[i].l<x)
sum+=(LL)(min(a[i].r,x)-a[i].l)*a[i].h;
return sum;
}
int main()
{
scanf("%d",&t);
while(t--)
{
LL sum=0;
scanf("%d",&R);
scanf("%d",&n);
for(int i=0; i<n; i++)
{
int x,y,w,h;
scanf("%d%d%d%d",&x,&y,&w,&h);
a[i].l=x,a[i].r=x+w,a[i].h=h;
sum+=(LL)w*h;
}
int l=0,r=R,mid;
while(l<r)
{
mid=(l+r)/2;
LL area=cal(mid);
if(2*area<sum) l=mid+1;
else r=mid;
}
LL tmp=cal(r);
while(cal(r)==tmp&&r<=R) r++;
printf("%d\n",r-1);
}
return 0;
}