[UVALive7261]A - Xiongnu'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 typedef struct Sq { LL x, y, w, h; }Sq;
 6 const int maxn = 10100;
 7 LL n, k;
 8 Sq sq[maxn];
 9 LL sum;
10 map<LL, int> vis;
11
12 bool ok1(LL val) {
13     LL a = 0;
14     for(int i = 0; i < k; i++) {
15         if(sq[i].x + sq[i].w <= val) a += sq[i].w * sq[i].h;
16         else if(sq[i].x <= val && val <= sq[i].x+sq[i].w) {
17             a += sq[i].h * (val - sq[i].x);
18         }
19     }
20     LL b = sum - a;
21     return a >= b;
22 }
23
24 int main() {
25     // freopen("in", "r", stdin);
26     int T;
27     LL x, y, w, h;
28     scanf("%d", &T);
29     while(T--) {
30         scanf("%lld%lld",&n,&k);
31         sum = 0; vis.clear();
32         vis[n] = 1;
33         for(int i = 0; i < k; i++) {
34             scanf("%lld%lld%lld%lld",&x,&y,&w,&h);
35             sq[i] = Sq{x,y,w,h};
36             vis[x] = 1;
37             sum += w * h;
38         }
39         LL lo = 0, hi = n;
40         LL ret = 0;
41         while(lo <= hi) {
42             LL mid = (lo + hi) >> 1;
43             if(ok1(mid)) {
44                 ret = mid;
45                 hi = mid - 1;
46             }
47             else lo = mid + 1;
48         }
49         bool flag = 0;
50         for(int i = 0; i < k; i++) {
51             if(sq[i].x <= ret && ret < sq[i].x+sq[i].w) {
52                 flag = 1;
53                 break;
54             }
55         }
56         if(flag) {
57             printf("%lld\n", ret);
58             continue;
59         }
60         LL remain = 0;
61         for(LL i = ret; i <= n; i++) {
62             if(vis.find(i) != vis.end()) {
63                 remain = i - ret;
64                 break;
65             }
66         }
67         printf("%lld\n", ret+remain);
68     }
69     return 0;
70 }

[UVALive7261]A - Xiongnu's Land (二分)

时间: 2024-08-06 11:54:51

[UVALive7261]A - Xiongnu's Land (二分)的相关文章

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

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

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<

hiho1249 Xiongnu&#39;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 firs

[ 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

[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为其宽度和高度.绿洲互不

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

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

ZOJ 1516 Uncle Tom&#39;s Inherited Land(二分匹配 最大匹配 匈牙利啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=516 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-uncl