hiho1249 Xiongnu's Land

题目链接:http://hihocoder.com/problemset/problem/1249

题目大意:有一个大正方形里面有好多不重叠的小矩形,怎么找出一条竖线分割这个正方形,使得两边的矩形面积尽量相等并且正方形左边的面积比右边大

思路:做两次二分就好了

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 #define xx first
 6 #define yy second
 7 struct node{
 8     int len,wid;
 9     long long sum;
10     pair<int,int> p1;
11     pair<int,int> p2;
12     void cal(){
13         p2.xx=p1.xx+len;
14         p2.yy=p1.yy-wid;
15         sum=(long long)len*wid;
16     }
17 };
18 bool cmp(const node&a,const node&b){
19     return a.p1.xx<b.p1.xx;
20 }
21 const int maxn=1e4+10;
22 node a[maxn];
23 int n,r;
24 long long check(int x){
25     long long tmp=0;
26     for(int i=1;i<=n;i++){
27         tmp+=((long long )a[i].wid*max(min(a[i].len,x-a[i].p1.xx),0));
28     }
29     return tmp;
30 }
31 int binary_search(int R,long long sum){
32     int l=-1,r=R+1;
33     int mid;
34     while(r-l>1){
35         mid=(l+r)>>1;
36         long long tmp=check(mid);
37         if(tmp>=sum-tmp){
38             r=mid;
39         }
40         else l=mid;
41     }
42     long long ans=check(r);
43     l=-1,r=R+1;
44     while(r-l>1){
45          mid=(l+r)>>1;
46         long long tmp=check(mid);
47         if(tmp>ans) r=mid;
48         else l=mid;
49     }
50     return l;
51 }
52 int main(){
53     int T;
54     //freopen("C:\\Users\\acm\\Desktop\\2015Beijing\\in.txt","r",stdin);
55     scanf("%d",&T);
56     while(T--){
57         scanf("%d",&r);
58         scanf("%d",&n);
59         long long sum=0;
60         for(int i=1;i<=n;i++){
61             int x,y,len,wid;
62             scanf("%d %d %d %d",&x,&y,&len,&wid);
63             sum+=((long long)len*wid);
64             a[i].len=len,a[i].wid=wid,a[i].p1=make_pair(x,y),a[i].cal();
65         }
66         sort(a+1,a+1+n,cmp);
67         printf("%d\n",binary_search(r,sum));
68     }
69     return 0;
70 }

hiho1249 Xiongnu's Land

时间: 2024-10-22 03:05:53

hiho1249 Xiongnu's Land的相关文章

[UVALive7261]A - Xiongnu&#39;s Land (二分)

题目链接:https://vjudge.net/problem/UVALive-7261 题意略 三个步骤: 1.二分满足左边绿洲面积大于等于右边绿洲面积,并且使左边面积尽可能大的分割线位置. 2.判断这个分割线是否包含于任何一个绿洲中,如果包含,那么直接输出结果就行,否则: 3.从此坐标向右扫描,找到下一个绿洲的左边界,此为答案. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 typ

15北京区域赛——A 二分——hihoCoder 1249 Xiongnu&#39;s Land

两次二分,第一次取得最小值,第二次往右二分看是否能到更右边 注意超出部分land部分要去掉 #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; struct edge{ int x, y, w, h; }a[10010]; bool cmp(edge A, edge B) { return A.x < B.x; } int n; ll cal(int x) { ll

[hihocoder 1249 Xiongnu&#39;s Land]线性扫描

2015区域赛北京赛区的三水,当时在赛场上没做出的原因是复杂度分析不正确导致把方法想复杂了.近来复习复杂度分析,觉得不能只是笼统地看渐进复杂度(big-O),更应根据算法的伪码计算真正的以基本操作数为变量的时间复杂度T(n). 题意:在二维坐标系第一象限中,将一块顶点在原点边长为R的正方形土地用直线x=n一分为二,左侧分给Wei,右侧分给Huo. 土地中包含N个绿洲,每个绿洲是一个矩形,其位置和大小用四元组(L,T,W,H)表示,其中(L,T)为其左上方顶点的坐标,W,H为其宽度和高度.绿洲互不

[ An Ac a Day ^_^ ] HihoCoder 1249 Xiongnu&#39;s Land 线性扫描

拿到了icpc北京站的参赛名额 感谢亮哥~ 虽然是地狱之战 但也要全力以赴! 题意: 有一片沙漠 n片绿洲 让你用一条线分成两部分 左≥右 而且分割线要尽量靠右 问线的位置 思路: 网上说可以二分 没太看懂…… 还有一种思路就是线性扫描 将二维的图化成一维的线 然后从头扫一遍 遇到左≥sum/2时试探着向右继续扫 最后输出答案 1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #includ

UVALive 7261 Xiongnu&#39;s Land(二分)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5273 思路:二分位置(无需考虑总坐标,仅考虑横坐标即可),使得2*area >= sum,在满足该条件的情况下,尽量右移使得左侧面积尽量大. #include<cstdio> #include<cstring> #include<

2015北京区域赛 Xiongnu&#39;s Land

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

UVALive-7261 Xiongnu&#39;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 youn

hdu 1507 Uncle Tom&#39;s Inherited Land*(二分)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1853    Accepted Submission(s): 769 Special Judge Problem Description Your old uncle Tom inherited a piece of land fr

HDU 1507 Uncle Tom&#39;s Inherited Land*

Uncle Tom's Inherited Land* Special Judge Problem Description Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle deci