2015北京现场赛题目:http://media.hihocoder.com/contests/icpcbeijing2015/problems.pdf
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
用double二分,最终结果视情况取整。
判断ans是否穿过某一矩形,若是则输出ans,否则向右找最近矩形左边界(预先排序),找不到就输出R。
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll k,r,n,ssum; 5 const double eps=1e-10; 6 struct st 7 { 8 ll l,t,w,h,s; 9 }a[10005]; 10 void bs() 11 { 12 ll i,in=0; 13 double le=a[1].l,ri=r,mid,sl=0,sr=0; 14 while(ri-le>eps) 15 { 16 mid=(le+ri)/2; 17 in=0; 18 sl=0; 19 for(i=1;i<=n;i++) 20 { 21 if((a[i].l<mid || fabs(a[i].l-mid)<eps) && mid<a[i].l+a[i].w){sl+=(mid-a[i].l)*a[i].h;in=1;} 22 else 23 if((a[i].l+a[i].w<mid) || fabs(a[i].l-a[i].w)<eps)sl+=a[i].s; 24 } 25 sr=ssum-sl; 26 if(fabs(sl-sr)<eps)break; 27 if(sl>sr)ri=mid; 28 else le=mid; 29 } 30 ll ans=r; 31 if(in==1) 32 { 33 if(fabs((ll)mid-mid)<eps)ans=(ll)mid; 34 else ans=(ll)mid+1; 35 for(i=1;i<=n;i++) 36 { 37 if(a[i].l<=ans && ans<a[i].l+a[i].w)break; 38 } 39 if(i<=n) 40 { 41 cout<<ans<<'\n'; 42 } 43 else 44 { 45 for(i=1;i<=n;i++) 46 if(a[i].l>=ans) 47 { 48 ans=a[i].l; 49 break; 50 } 51 if(i>n)ans=r; 52 cout<<ans<<'\n'; 53 } 54 } 55 else 56 { 57 for(i=1;i<=n;i++) 58 if(a[i].l>mid || fabs(a[i].l-mid)<eps) 59 { 60 ans=a[i].l; 61 break; 62 } 63 if(i>n)ans=r; 64 cout<<ans<<'\n'; 65 } 66 } 67 bool cmp(st x,st y) 68 { 69 return x.l < y.l; 70 } 71 int main() 72 { 73 ios_base::sync_with_stdio(false); 74 cin.tie(NULL); 75 cin>>k; 76 ll i; 77 while(k--) 78 { 79 cin>>r>>n; 80 ssum=0; 81 for(i=1;i<=n;i++) 82 { 83 cin>>a[i].l>>a[i].t>>a[i].w>>a[i].h; 84 a[i].s=a[i].w*a[i].h; 85 ssum+=a[i].s; 86 } 87 sort(a+1,a+n+1,cmp); 88 bs(); 89 } 90 return 0; 91 }