UVALive-7261 Xiongnu's Land

Posted artoriax

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

[UVALive7261]A - Xiongnu's Land (二分)

2015北京区域赛 Xiongnu's Land

hiho1249 Xiongnu's Land

[hihocoder 1249 Xiongnu's Land]线性扫描

[ An Ac a Day ^_^ ] HihoCoder 1249 Xiongnu's Land 线性扫描

python如何将['abcdefg']分割成['a','b','c','d','e','f&#