Xiongnu's Land (线性扫描,二分)
2017-03-24 17:10
274 查看
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
题意:沙漠中有许多块矩形水源,水源不相交,问能否找到一根中轴线,使得轴线左边的水源面积大于等于右边的水源面积。在满足两个面积之差最小的情况下,使得轴线靠近右端点
两种做法,一是线描。一种是二分。
线描做法:
二分做法:记得所有算和的地方都要开longlong
另一种就是
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
题意:沙漠中有许多块矩形水源,水源不相交,问能否找到一根中轴线,使得轴线左边的水源面积大于等于右边的水源面积。在满足两个面积之差最小的情况下,使得轴线靠近右端点
两种做法,一是线描。一种是二分。
线描做法:
#include <cstdio> #include <cstring> #include <iostream> using namespace std; //long long land[10010][2]; long long green[1000005]; int main(){ ios::sync_with_stdio(0); long long t,n,r,x,y,w,h,re,total; cin>>t; while(t--){ total=0; memset(green,0,sizeof(green)); cin>>r>>n; for(int i=1;i<=n;i++){ cin>>x>>y>>w>>h; total+=w*h; for(int j=x;j<x+w;j++){ green[j]+=h; } } re=0; int i; for(i=0;re*2<total;i++) re+=green[i]; while(green[i]==0&&i<r) i++; cout<<i<<endl; } return 0; }
二分做法:记得所有算和的地方都要开longlong
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define debu using namespace std; const int maxn=1e4+50; typedef long long LL; struct Node { int l,r,h; }; int t,R,n; Node a[maxn]; LL cal(int x) { LL sum=0; for(int i=0; i<n; i++) if(a[i].l<x) sum+=(LL)(min(a[i].r,x)-a[i].l)*a[i].h; return sum; } int main() { #ifdef debug freopen("in.in","r",stdin); #endif // debug scanf("%d",&t); while(t--) { LL sum=0; scanf("%d",&R); scanf("%d",&n); for(int i=0; i<n; i++) { int x,y,w,h; scanf("%d%d%d%d",&x,&y,&w,&h); a[i].l=x,a[i].r=x+w,a[i].h=h; sum+=(LL)w*h; } int l=0,r=R,mid; while(l<r) { mid=(l+r)/2; LL area=cal(mid); if(2*area<sum) l=mid+1; else r=mid; } LL tmp=cal(r);//这种二分答案是右边界,因为右边界是一直满足的。 while(cal(r)==tmp&&r<=R) r++; printf("%d\n",r-1); } return 0; }
另一种就是
#include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <iostream> using namespace std; int n; const int maxn=1e4+50; struct node { int l,r; int h; }s[maxn]; typedef long long LL; LL cal(int x) { LL ans=0; for(int i=0;i<n;i++) { if(s[i].l<x) ans+=(LL)(min(s[i].r,x)-s[i].l)*s[i].h; } return ans; } int main() { int t; scanf("%d",&t); while(t--) { int R; scanf("%d",&R); scanf("%d",&n); LL sum=0; for(int i=0;i<n;i++) { int x,y,w,h; scanf("%d%d%d%d",&x,&y,&w,&h); s[i].l=x;s[i].r=x+w; s[i].h=h; sum+=(LL)w*h; } int l=0;int r=R; while(l<=r) { int mid=(l+r)>>1; LL area=cal(mid); if(area*2<sum) { l=mid+1; } else r=mid-1;// 最终l==r两个都符合然后r<l 弹出 所以答案是l } LL tmp=cal(l); while(cal(l)==tmp&&l<=R) l++; printf("%d\n",l-1 ); } }
相关文章推荐
- 【hihocoder1249 2015北京赛区A】【打标记线性扫描法】 Xiongnu's Land 土地划分使得左边比右边大且尽可能接近
- UVALive 7261 Xiongnu's Land(二分)
- UVALive 7261 A - Xiongnu's Land(二分)
- UVALive 7261 Xiongnu's Land 扫描线
- hihoCoder 1249 Xiongnu's Land(二分)
- 【扫描线】uvalive 7261 Xiongnu's Land
- 2015 Asia BeiJing Regional Contest A. Xiongnu's Land(二分)
- 2015北京区域赛 A.Xiongnu's Land(二分或者标记法)
- 【CF676C】Vasya and String(二分查找,线性扫描尺取法)
- 【NOIP2017练习】怎样更有力气(二分答案,线性扫描)
- 【CF732D】Exams(线性扫描,贪心,二分)
- HDU 5124 hihocoder 1249 Xiongnu's Land (2015北京区域赛A)
- 二分 hihoCoder1249 Xiongnu's Land
- 2015ACM-ICPC 北京赛区 Problem A. Xiongnu's Land
- hihocoder 1249 Xiongnu's Land
- hihocoder 2015ACM-ICPC 北京赛区 Problem A. Xiongnu's Land
- 【CF652C】Foe Pairs(线性扫描)
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs+二分+线段树延迟操作、树形化线性
- 【HDOJ5714】拍照(线性扫描)
- ZOJ2965 Accurately Say "CocaCola"! 线性扫描