Xiongnu's Land UVALive - 7261(前缀和)

Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against
the Xiongnu earned him great acclaim. He was a relative of Emperor Wu because he was the younger
half-brother of Empress Wei Zifu (Emperor Wu’s wife) and the husband of Princess Pingyang. He was
also the uncle of Huo Qubing, another notable Han general who participated in the campaigns against
the Xiongnu and exhibited outstanding military talent even as a teenager.
Defeated by Wei Qing and Huo Qubing, the Xiongnu sang: “Losing my Qilian Mountains, made
my cattle unthriving; Losing my Yanzhi Mountains, made my women lacking rouge.”
The text above is digested from Wikipedia. Since Wei and Huo’s distinguished achievements,
Emperor Wu decided to give them some awards — a piece of land taken by them from Xiongnu. This
piece of land was located in a desert, and there were many oases in it. Emperor Wu wanted to draw
a straight south-to-north dividing line to divide the land into two parts, and gave the western part to
Wei Qing while gave the eastern part to Huo Qubing. There are two rules about the land dividing:

  1. The total area of the oases lay in Wei’s land must be larger or equal to the total area of the oases
    lay in Huo’s land, and the difference must be as small as possible.
  2. Emperor Wu wanted Wei’s land to be as large as possible without violating the rule 1.
    To simplify the problem, please consider the piece of land given to Wei and Huo as a square on a
    plane. The coordinate of its left bottom corner was (0, 0) and the coordinate of its right top corner
    was (R, R). Each oasis in this land could also be considered as a rectangle which was parallel to the
    coordinate axes. The equation of the dividing line was like x = n, and n must be an integer. If the
    dividing line split an oasis, then Wei owned the western part and Huo owned the eastern part. Please
    help Emperor Wu to find out how to draw the dividing line.
    Input
    The first line of the input is an integer K meaning that there are K (1 ≤ K ≤ 15) test cases.
    For each test case:
    The first line is an integer R, indicating that the land’s right top corner was at (R, R) (1 ≤ R ≤
    1, 000, 000)
    Then a line containing an integer N follows, indicating that there were N (0 < N ≤ 10000) oases.
    Then N lines follow, each contains four integers L, T, W and H, meaning that there was an
    oasis whose coordinate of the left top corner was (L, T), and its width was W and height was H.
    (0 ≤ L, T ≤ R, 0 < W, H ≤ R). No oasis overlaps.
    Output
    For each test case, print an integer n, meaning that Emperor Wu should draw a dividing line whose
    equation is x = n. Please note that, in order to satisfy the rules, Emperor might let Wei get the whole
    land by drawing a line of x = R if he had to.
    Sample Input
    2
    1000
    2
    1 1 2 1
    5 1 2 1
    1000
    1
    1 1 2 1
    Sample Output
    5
    2

题意:

k组数据,每组数据第一行给定一个R,代表长方形的右上角,左下角为(0,0),之后给出一个n,接下来n行,每行给定(L,T)代表绿洲的左上角,W,H代表宽和高(长方形),然后平行y轴划分土地,左边土地的绿洲面积必须大于等于右边土地的绿洲面积,且让左边土地和右边土地的绿洲面积差异尽可能小,且在满足这个条件的情况下,要求左边土地的总面积尽可能大,问划分线x=?

思路:

看到网上许多大佬都写的二分,那么就写一个前缀和吧;输入的时候求出来每一个横坐标对应的竖线上的面积,然后再求前缀和每个横坐标之前的总面积是多少;预处理之后,遍历前缀和,看那个横坐标对应的左边的面积大于等于右边面积,并且这个横坐标还要尽量的大;坑点就在于这个横坐标必须刚好满足左边大于右边,并且尽量的靠右!!!

#include <iostream>
#include<cstdio>
#include <cstring>

using namespace std;
const int maxx=1e6+7;
typedef long long ll;
struct node
{
    ll a,b,w,h;
}bb[maxx];
ll ff[maxx];
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >>t;
    while(t--)
    {
        memset(ff,0,sizeof ff);
        int R;
        cin >>R;
        int n;
        cin >>n;
        ll sum=0;
        for(int i=0;i<n;i++)
        {
            cin >>bb[i].a>>bb[i].b>>bb[i].w>>bb[i].h;
            sum+=bb[i].w*bb[i].h;
            for(int j=bb[i].a+1;j<=bb[i].a+bb[i].w;j++)
            {
                if(j>R) break;
                ff[j]+=bb[i].h;
            }
        }
        int ans;
        int flag=1;
        ll xx=0;
        for(int i=1;i<=R;i++)
        {
            ff[i]+=ff[i-1];
            if(ff[i]*2>=sum&&flag)
            {
                flag=0;
                xx=ff[i];
                ans=i;
            }
            if(xx!=0&&ff[i]==xx)
            {
                ans=i;
            }
        }
        cout <<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值