UVALive-7261 Xiongnu's Land

题目链接

https://vjudge.net/problem/UVALive-7261

题面

Description

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=?

题解

解法一

先用两遍前缀和预处理出每个点左边的绿洲面积总和,二分的时候如果左边的面积*2小于等于总面积,那么就向有找,否则向左找,一旦找到比当前差异值小的划分线就更新答案,如果差异值不变答案取最大值,最后线性扫一遍,知道遇到有绿洲为止

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define N 1000050
using namespace std;
typedef long long ll;
int r, n;
ll pos[N];
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &r);
        scanf("%d", &n);
        memset(pos, 0, sizeof(pos));
        for (int i = 1; i <= n; i++) {
            int l, t, w, h;
            scanf("%d%d%d%d", &l, &t, &w, &h);
            pos[l + 1] += h;
            pos[l + w + 1] -= h;
        }
        for (int i = 1; i <= r; i++) {
            pos[i] += pos[i - 1];
        }
        for (int i = 1; i <= r; i++) {
            pos[i] += pos[i - 1];
        }
        int li = 0, ri = r;
        int mid;
        ll nowmin = 0x7ffffffffffffff;
        int ans = 0;
        while (li <= ri) {
            mid = (li + ri) >> 1;
            if (pos[mid] * 2 < pos[r]) {
                li = mid + 1;
            }
            else {
                ri = mid - 1;
                if (2 * pos[mid] - pos[r] < nowmin) {
                    nowmin = 2 * pos[mid] - pos[r];
                    ans = mid;
                }
                else if (2 * pos[mid] - pos[r] == nowmin) {
                    ans = max(ans, mid);
                }
            }
        }
        while (1) {
            if (pos[ans + 1] == pos[ans]) ans++;
            else break;
        }
        cout << ans << endl;
    }
    return 0;
}

解法二

先把总面积算出来,从左往右找到第一个左边面积大于等于一半的划分线停止循环,再线性扫一遍,遇到绿洲就停,输出最后的划分线即可

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define N 1000050
using namespace std;
typedef long long ll;
int r, n;
ll pos[N];
int main() {
    int ti;
    scanf("%d", &ti);
    while (ti--) {
        scanf("%d", &r);
        scanf("%d", &n);
        memset(pos, 0, sizeof(pos));
        ll sum = 0;
        for (int i = 1; i <= n; i++) {
            int l, t, w, h;
            scanf("%d%d%d%d", &l, &t, &w, &h);
            sum += (ll)w * (ll)h;
            pos[l] += (ll)h;
            pos[l + w] -= (ll)h;
        }
        for (int i = 1; i <= r; i++) {
            pos[i] += pos[i - 1];
        }
        ll nowsize = 0;
        int i;
        for (i = 0; nowsize * 2 < sum; i++) {
            nowsize += pos[i];
        }
        while (pos[i] == 0 && i < r) i++;
        cout << i << endl;
    }
    return 0;
}

UVALive-7261 Xiongnu's Land

原文地址:https://www.cnblogs.com/artoriax/p/10381202.html

时间: 2024-11-10 11:29:49

UVALive-7261 Xiongnu's Land的相关文章

UVALive 7261 Xiongnu&#39;s Land(二分)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5273 思路:二分位置(无需考虑总坐标,仅考虑横坐标即可),使得2*area >= sum,在满足该条件的情况下,尽量右移使得左侧面积尽量大. #include<cstdio> #include<cstring> #include<

[UVALive7261]A - Xiongnu&#39;s Land (二分)

题目链接:https://vjudge.net/problem/UVALive-7261 题意略 三个步骤: 1.二分满足左边绿洲面积大于等于右边绿洲面积,并且使左边面积尽可能大的分割线位置. 2.判断这个分割线是否包含于任何一个绿洲中,如果包含,那么直接输出结果就行,否则: 3.从此坐标向右扫描,找到下一个绿洲的左边界,此为答案. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 typ

15北京区域赛——A 二分——hihoCoder 1249 Xiongnu&#39;s Land

两次二分,第一次取得最小值,第二次往右二分看是否能到更右边 注意超出部分land部分要去掉 #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; struct edge{ int x, y, w, h; }a[10010]; bool cmp(edge A, edge B) { return A.x < B.x; } int n; ll cal(int x) { ll

hiho1249 Xiongnu&#39;s Land

题目链接:http://hihocoder.com/problemset/problem/1249 题目大意:有一个大正方形里面有好多不重叠的小矩形,怎么找出一条竖线分割这个正方形,使得两边的矩形面积尽量相等并且正方形左边的面积比右边大 思路:做两次二分就好了 1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 #define xx firs

[hihocoder 1249 Xiongnu&#39;s Land]线性扫描

2015区域赛北京赛区的三水,当时在赛场上没做出的原因是复杂度分析不正确导致把方法想复杂了.近来复习复杂度分析,觉得不能只是笼统地看渐进复杂度(big-O),更应根据算法的伪码计算真正的以基本操作数为变量的时间复杂度T(n). 题意:在二维坐标系第一象限中,将一块顶点在原点边长为R的正方形土地用直线x=n一分为二,左侧分给Wei,右侧分给Huo. 土地中包含N个绿洲,每个绿洲是一个矩形,其位置和大小用四元组(L,T,W,H)表示,其中(L,T)为其左上方顶点的坐标,W,H为其宽度和高度.绿洲互不

[ An Ac a Day ^_^ ] HihoCoder 1249 Xiongnu&#39;s Land 线性扫描

拿到了icpc北京站的参赛名额 感谢亮哥~ 虽然是地狱之战 但也要全力以赴! 题意: 有一片沙漠 n片绿洲 让你用一条线分成两部分 左≥右 而且分割线要尽量靠右 问线的位置 思路: 网上说可以二分 没太看懂…… 还有一种思路就是线性扫描 将二维的图化成一维的线 然后从头扫一遍 遇到左≥sum/2时试探着向右继续扫 最后输出答案 1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #includ

2015北京区域赛 Xiongnu&#39;s Land

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

UVALive 3211 Now or later

Now or later Time Limit: 9000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 321164-bit integer IO format: %lld      Java class name: Main As you must have experienced, instead of landing immediately, an aircraft someti

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca