[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;
- }
[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< ...
- [NOIP模拟赛][并没有用二分][乱搞AC]
圆圈舞蹈 [问题描述] 熊大妈的奶牛在时针的带领下,围成了一个圆圈跳舞.由于没有严格的教育,奶牛们之间的间隔不一致. 奶牛想知道两只最远的奶牛到底隔了多远.奶牛A到B的距离为A顺时针走和逆时针走,到达 ...
- poj_3179 Corral the Cows (二分+二维前缀和+离散化)
[题目链接] http://poj.org/problem?id=3179 [参考] http://www.cnblogs.com/evenbao/p/9243183.html [算法] 二分答案+判 ...
- $Poj3179\ Corral\ the\ Cows$ 二分+离散化+二维前缀和
Poj $Description$ 在一个二维平面上,有$N$颗草,每颗草的大小是$1*1$,左下角坐标为$x_i,y_i$.要求一个正方形,正方形的边平行于$x$或$y$轴,正方形里面包含至少$C$ ...
- SCU 4437 Carries(二分乱搞)题解
题意:问任意两对ai,aj相加的总进位数为多少.比如5,6,95分为(5,6)(5,95)(6,95),进位数 = 1 + 2 + 2 = 5 思路:显然暴力是会超时的.我们可以知道总进位数等于每一位 ...
随机推荐
- SQL 视图、事务
假设看多个不同的表 select *from student ,score,course,teacher 有重复的 改为select student.Sno,sname,ssex,sbirthd ...
- es的插件 ik分词器的安装和使用
今天折腾了一天,在es 5.5.0 上安装ik.一直通过官方给定的命令没用安装成功,决定通过手工是形式进行安装.https://github.com/medcl/elasticsearch-analy ...
- 2018_oakland_linuxmalware
2018年oakland论文:理解linux恶意软件 论文地址:http://www.s3.eurecom.fr/~yanick/publications/2018_oakland_linuxmalw ...
- CAD控件界面显示与隐藏(网页版)
控件界面工具栏的显示或隐藏,js代码实现如下: 1 2 3 4 5 6 7 8 9 //隐藏/显示工具栏 mxOcx.ShowToolBar("常用工具",isShow ...
- Asp.Net Core 进阶(一) —— 读取appsettings.json
我们以前在Asp.Net MVC中使用 System.Configuration.ConfigurationManager 来读取web.config文件.但是Asp.Net Core MVC已经没有 ...
- 判断一个链表是否为回文结构 【题目】 给定一个链表的头节点head,请判断该链表是否为回 文结构。 例如: 1->2->1,返回true。 1->2->2->1,返回true。 15->6->15,返回true。 1->2->3,返回false。 进阶: 如果链表长度为N,时间复杂度达到O(N),额外空间复杂 度达到O(1)。
方式1:借助栈 空间辅助度是O(N) 方式2: 借助栈 空间复杂度是 O(n/2).只存后半个链表 方式3: 反转后半个链表 最后再反转回来 package my_basic.class_3; im ...
- XDB基于Library的备份及恢复
基于standalone全备份 语句: xdb backup --federation xhive://localhost:1235 --standalone --file E:\xdbData\xD ...
- (39)zabbix snmp自定义OID nginx监控实例
为什么要自定义OID? 前面的文章已经讲过zabbix如何使用snmp监控服务器,但是他有一个很明显的局限性:只能监控定义好的OID项目 假如我们想知道nginx进程是否在运行?在没有zabbix a ...
- Linux基础学习-数据备份工具Rsync
数据备份工具rsync 作为一个系统管理员,数据备份是非常重要的,如果没有做好备份策略,磁盘损坏了,那么你的数据将全部丢失,所以在日常的维护工作中,一定要时刻牢记给数据做备份. rsync不仅可以可以 ...
- python爬虫基础02-urllib库
Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...