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
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.”
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
- 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. - 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