[luoguP2862] [USACO06JAN]把牛Corral the Cows(二分 + 乱搞)
传送门
可以二分边长
然后另开两个数组,把x从小到大排序,把y从小到大排序
枚举x,可以得到正方形的长
枚举y,看看从这个y开始,往上能够到达多少个点,可以用类似队列来搞
其实发现算法的本质之后,x可以不用从小到大排序
#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 1001
#define max(x, y) ((x) > (y) ? (x) : (y))int c, n, ans;
int x[N], y[N], a[N], b[N];inline int read()
{int x = 0, f = 1;char ch = getchar();for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';return x * f;
}inline bool cmp1(int a, int b)
{return x[a] < x[b];
}inline bool cmp2(int a, int b)
{return y[a] < y[b];
}inline bool check(int len)
{int i, j, k, l, r, sum;for(i = 1; i <= n; i++){k = 1;sum = 0;l = x[a[i]];r = l + len - 1;for(j = 1; j <= n; j++){while(y[b[k]] - y[b[j]] + 1 <= len && k <= n){if(l <= x[b[k]] && x[b[k]] <= r) sum++;k++;}if(sum >= c) return 1;if(l <= x[b[j]] && x[b[j]] <= r) sum--;}}return 0;
}int main()
{int i, l = 1, r, mid;c = read();n = read();for(i = 1; i <= n; i++){x[i] = read();y[i] = read();r = max(r, x[i]);r = max(r, y[i]);a[i] = b[i] = i;}std::sort(a + 1, a + n + 1, cmp1);std::sort(b + 1, b + n + 1, cmp2);while(l <= r){mid = (l + r) >> 1;if(check(mid)) ans = mid, r = mid - 1;else l = mid + 1; }printf("%d\n", ans);return 0;
}
转载于:https://www.cnblogs.com/zhenghaotian/p/7506098.html
[luoguP2862] [USACO06JAN]把牛Corral the Cows(二分 + 乱搞)相关推荐
- 洛谷——P2862 [USACO06JAN]把牛Corral the Cows
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 洛谷P2862 [USACO06JAN]把牛Corral the Cows
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 洛谷 P2862 [USACO06JAN]把牛Corral the Cows
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 洛谷[USACO06JAN]把牛Corral the Cows
题目描述 约翰打算建一个围栏来圈养他的奶牛.作为最挑剔的兽类,奶牛们要求这个围栏必须是正方 形的,而且围栏里至少要有C< 500)个草场,来供应她们的午餐. 约翰的土地上共有C<=N< ...
- 【USACO06JAN POJ3179】Corral the Cows
POJ 洛谷 分析 离散化+前缀和+二分 这题和激光炸弹很像,但由于坐标范围较大,需要用到二分. 代码 #include <cstdio> #include <cstring> ...
- 【bzoj5085】最大(二分+乱搞)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5085 这道题我们可以先二分答案,然后转化为判定是否有四角权值>=mid的矩形. ...
- bzoj1992鬼谷子的钱袋(二分乱搞 二进制)
1192: [HNOI2006]鬼谷子的钱袋 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3223 Solved: 2333 Descripti ...
- Corral the Cows(二分、前缀和、离散化)
题面:Corral the Cows[牛客] 题目大意 有 nnn 个单位的三叶草,每单位三叶草占据一个 1×11×11×1 的土地,每块土地的位置由其左下角的下标确定,并且下标 xxx 和 yyy ...
最新文章
- cordova报错:Error: Failed to find ‘ANDROID_HOME‘ environment variable. Try setting setting it manually
- 删除所有的distribution chain再保存的后台执行逻辑
- 基于.NET Framework 4.0的解决方案部署
- 苏州飘“彩云” 五年规模破百亿元
- P4248-[AHOI2013]差异【SAM or SA】
- 用仿ActionScript的语法来编写html5——第三篇,鼠标事件与游戏人物移动
- Android自定义View的构造函数
- Django在Window下的部署
- UFO提示写日志文件ufoerr.log 错误
- 【计算方法】数值积分
- MFC按方向键旋转立方体线框
- 使用DBUtils报错connot create bean 错误解决的办法
- Linux基础知识小结(一)
- EasyExcel的导入excel文件
- 打印机服务器属性添加哪个文件,如何设置打印机服务器属性如何找到打印机服务器属性...
- 安卓开发— —仿微信界面(二)
- srand()和rand()函数详解
- 无线覆盖服务器,港口码头无线覆盖系统方案的构成及功能实现
- 三层交换机和二层交换机之间的配置
- 微软的杀毒软件在 MacOS 上推出