2015北京区域赛 Xiongnu'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 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 

1000 

1 1 2 1 
5 1 2 1 
1000 

1 1 2 1 
Sample Output 

2

题意:沙漠中有许多块矩形水源,水源不相交,问能否找到一根中轴线,使得轴线左边的水源面积大于等于右边的水源面积。在满足两个面积之差最小的情况下,使得轴线靠近右端点

用二分法求,否则时间超限

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
struct point{
    int x,y,w,h;
};
point a[10010];
/*bool cmp(point p,point q){
    return p.x<q.x;
}*/
ll cal(int x){
    ll sum = 0;
    for(int i=0;i<n;i++){
        if(a[i].x < x){
            sum += (ll)( min( (a[i].w+a[i].x),x) - a[i].x) * a[i].h;
        }
    }
    return sum;
}
int main(){
    int T,R,w,h;
    ll total;
    while(cin >> T){
        while(T--){
            total = 0;
            cin >> R;
            cin >> n;
            for(int i=0;i<n;i++){
                cin >> a[i].x >> a[i].y >> a[i].w >> a[i].h;
                total += (ll)a[i].h*a[i].w;//注意每次都要取long long 否则答案错误

            }
        //    sort(a,a+n,cmp);
            int l,r,mid;
            ll tmp;
            l = 0,r = R;
            while(l<r){
                mid = (r+l)/2;
                tmp = cal(mid);
                if(2*tmp<total){
                    l = mid + 1;
                }else r = mid;
            }
            tmp = cal(r);
            while(cal(r) == tmp && r<=R){
                r ++;
            }
            cout << r-1 << endl;
        }
    }
    return 0;
}
    

2015北京区域赛 Xiongnu's Land

时间: 2024-11-09 20:33:59

2015北京区域赛 Xiongnu's Land的相关文章

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

2015北京网络赛A题The Cats&#39; Feeding Spots

题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<memory.h> 6 using namespace std; 7 const i

2015北京网络赛 Couple Trees 倍增算法

2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道.  解法来自 qscqesze ,这个其实之前如果了解过倍增的话还是不是很难,不过这题的数据也不是很给力,极限数据理论上是过不了的.  其他解法有树链剖分?并不是很清楚.就这样水过了吧... 1 #include <iostream> 2 #include <cstdio> 3 #include &l

2015北京网络赛 D-The Celebration of Rabbits 动归+FWT

2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x)为满足x≤ai≤m+x且ai的异或和为0 的序列a的个数. 求 ∑Rx=Lf(x)mod1000000007 思路:因为对于每一个第一次分配后的a序列对应唯一的x,所以我们就枚举x然后在求序列的个数.

Heshen&#39;s Account Book HihoCoder - 1871 2018北京区域赛B题(字符串处理)

Heshen was an official of the Qing dynasty. He made a fortune which could be comparable to a whole country's wealth by corruption. So he was known as the most corrupt official in Chinese history. But Emperor Qianlong liked, or even loved him so much

2015长春区域赛赛后总结

比赛时间:2015.10.18 队名:哆啦小黄埋(alone kampai UMR) 成员:ST,QL,WC 解出题目:两道 网络赛的时候并没有出线,老师争取到了女队名额,才有了参加区域赛的机会(谢谢老师么么哒~ 这次的比赛打铁没有什么可怨的,实力不足+比赛策略不当+心里素质差,每一条都是超级致命的(哭 过了的两道水题,L和F,L是经过ST和WC讨论之后1A的,F是我和ST讨论过敲得,ST去看其他题,而我就在一直卡题.由于代码奇葩,队友都看不懂,只能干着急+出数据,所以只能自己调试,WA好多发,

2015亚洲区域赛长春赛区网络预选赛

第一次打网络赛,第一场,总体来说还可以吧,但是我们队三个人状态都并不太好,主要就是 WA 的比较多吧,开场看最后一题是我的习惯了,虽然貌似那题到打了一半可能才有队伍做出来了,我看了感觉像前几天训练赛的时候做的一道题.刻盘开场看 06 ,凯神开场 01.接着两分钟学长发现 07 水题我就跟着看了发题意,求区间最大值,静态.然后数据范围也很小,就直接开敲暴力,四分钟的时候过的,大概 20 名左右吧,那就是我们的最高名次了2333……接着我准备继续研究下 13 ,刻盘告诉我 06 是关于循环的子串的问

HDU5556 Land of Farms(二分图 2015 合肥区域赛)

容易想到将问题转化为求图的独立数问题 ,但求一般图的独立集是一个NPC问题,需要一些转化. 状态压缩,枚举每个上古农场是否选择,然后将剩下的新农场根据i + j奇偶性分为x , y集. 结果为 max(tot  + nx + ny - 二分图匹配数) #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<string> #include&l

北京区域赛总结

好久没写过题解和总结了...Relive!!! 热身赛的时候..三道水题我们队却只过了一道..考虑不足的是边界条件和数据范围...还有一道差分约束我到目前还没搞过...My fault... 正式赛... 开始的时候两个队友分别看到了两道水题..交给队长..A掉了...形势比较好... 然后,我觉得D题是一个比较简单的贪心题目,遂交给队长去写,WA了...看来不是贪心,但是这时候队友已经被我带到坑里了... 再后来,lyw开始写I题,一个几何题,但是情况比较繁琐,就一直在WA了... 这时候题目