Xiongnu's Land

本文讲述了西汉名将卫青和霍去病对抗匈奴的英勇事迹,以及他们在战场上取得的辉煌胜利。卫青是汉武帝的亲戚,因击败匈奴而闻名遐迩。霍去病则是卫青的侄子,同样在对抗匈奴的战役中展现了卓越的军事才能。文章还提及了汉武帝为表彰二人功绩,决定将从匈奴手中夺取的土地一分为二,分别赐予卫青和霍去病的故事。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

See the article on my own blog https://dyingdown.github.io/2019/10/20/Xiongnu-s-Land/.

Problem 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

Analysis the Problem

The core of the problem is Dichotomy which is Binary search the best range of the X.

Code

#include<bits/stdc++.h>

using namespace std;

struct point{
	long long fx, fy, bx, by;
	long long s;
}ps[11000];

long long t, n, R, w, h;

long long check(long long mid) {
	long long q = 0, w = 0; 
	for(long long i = 0; i < n; i ++){
		if(mid >=  ps[i].fx and mid <= ps[i].bx) {
			q += (mid - ps[i].fx) * (ps[i].fy - ps[i].by);
			w += (ps[i].bx - mid) * (ps[i].fy - ps[i].by);
		} else if(mid >= ps[i].bx){
			q += ps[i].s;
		} else if(mid <= ps[i].bx) {
			w += ps[i].s;
		}
	}
	return q - w;
}
int main(){
	cin >> t;
	while(t --){
		cin >> R >> n;
		for(long long i = 0; i < n; i ++){
			cin >> ps[i].fx >> ps[i].fy >> w >> h;
			if (ps[i].fx + w > R) {
				ps[i].bx = R;
			} else ps[i].bx = ps[i].fx + w;
			if(ps[i].fy - h < 0) {
				ps[i].by = 0;
			} else {
				ps[i].by = ps[i].fy - h;
			}
			ps[i].s = (ps[i].bx - ps[i].fx) * (ps[i].fy - ps[i].by);
		}
		long long l = 0, r = R + 1, mid = (l + r) / 2;
		while (l <= r) {
			if(check(mid) > 0) {
				r = mid-1;
			} else if (check(mid) == 0) {
				r=mid-1;
			} else {
				l = mid+1;
			} 
			mid = (l + r) / 2;
		} 
		while(check(l) == check(l+1) && l<R)l++;
		cout << l << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值