论水题与难题的差距:在于一个upper_bound

那么,这题一看就很显然了:因为答案满足二分性质所以我们二分。

然后我们再建造一个二维前缀和,每次判断的时候怎么办呢?

我先以为是贪心:选择以每个点为角落的正方形。后来瞬间构造反例:


—————

丨    ·     丨

丨        · 丨

丨·         丨

丨     ·    丨

—————


然后考虑枚举:500 * 500, 随便水!

然后狂T不止...

发现在枚举内部我还有枚举,具体来说时间复杂度是:log10000 * 500 * 500 * 500

然后我改用了upper_bound,时间复杂度变为:log10000 * 500 * 500 * log500

然后果然A了!

 /// poj3179
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = ; int a[N], b[N], x[N], y[N], g[N][N], sum[N][N], n, C, tx, ty; inline bool check(int k) {
//printf("check:%d\n", k);
int ans = ;
for(int i = ; i <= tx; i++) {
for(int j = ; j <= ty; j++) {
int ii = upper_bound(x + i, x + tx + , x[i] + k) - x - ;
int jj = upper_bound(y + j, y + ty + , y[j] + k) - y - ;
/// 就是这里!
//printf("%d %d %d %d ", i, j, ii, jj);
ans = max(ans, sum[ii][jj] - sum[ii][j - ] - sum[i - ][jj] + sum[i - ][j - ]);
if(ans >= C) return ;
//printf("ans=%d\n", ans);
//ii = i; jj = j;
//while(ii >= 0 && x[ii] - x[i] )
}
}
//printf("%d\n", ans);
return ans >= C;
} int main() {
int m = -;
scanf("%d%d", &C, &n);
for(int i = ; i <= n; i++) {
scanf("%d%d", &a[i], &b[i]);
x[i] = a[i];
y[i] = b[i];
m = max(m, x[i]);
m = max(m, y[i]);
}
sort(x + , x + n + );
sort(y + , y + n + );
for(int i = ; i <= n; i++) {
if(x[i] != x[i - ]) {
x[++tx] = x[i];
}
if(y[i] != y[i - ]) {
y[++ty] = y[i];
}
}
for(int i = ; i <= n; i++) {
int px = lower_bound(x + , x + tx + , a[i]) - x;
int py = lower_bound(y + , y + ty + , b[i]) - y;
g[px][py]++;
}
for(int i = ; i <= tx; i++) {
for(int j = ; j <= ty; j++) {
sum[i][j] = sum[i - ][j] + sum[i][j - ] - sum[i - ][j - ] + g[i][j];
}
}
int l = , r = m, mid;
while(l < r) {
mid = (l + r) >> ;
if(check(mid)) {
r = mid;
}
else l = mid + ;
}
printf("%d", r + );
return ;
}

AC代码

小细节:1 * 1的方框边长是0,n * n的方框边长是n - 1

所以我最后输出时 + 1

poj3179 Corral the Cows的更多相关文章

  1. $Poj3179\ Corral\ the\ Cows$ 二分+离散化+二维前缀和

    Poj $Description$ 在一个二维平面上,有$N$颗草,每颗草的大小是$1*1$,左下角坐标为$x_i,y_i$.要求一个正方形,正方形的边平行于$x$或$y$轴,正方形里面包含至少$C$ ...

  2. POJ3179 Corral the Cows题解

    我就是个垃圾--一道水题能写这么长时间-- 首先看到题就想到了二维前缀和+二分边长,但地图边长10000,得离散化. 于是这个离散化就把我搞疯了,淦. 这反映出现在基础知识还是不牢固,相当不牢固. 复 ...

  3. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  4. 【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法

    [BZOJ1720][Usaco2006 Jan]Corral the Cows 奶牛围栏 Description Farmer John wishes to build a corral for h ...

  5. POJ 3179 Corral the Cows

    Corral the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1352   Accepted: 565 De ...

  6. 洛谷——P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  7. 洛谷P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  8. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  9. BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏

    http://www.lydsy.com/JudgeOnline/problem.php?id=1720 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1 ...

随机推荐

  1. spring初始化bean时执行某些方法完成特定的初始化操作

    在项目中经常会在容器启动时,完成特定的初始化操作,如资源文件的加载等. 一 实现的方式有三种: 1.使用@PostConstruct注解,该注解作用于void方法上 2.在配置文件中配置init-me ...

  2. 大白跟着“菜鸟”学node——同名事件

    若存在两个同名事件,触发事件时,两个事件监听器的回调函数会被按次序先后调用. 实例来自菜鸟教程: var events=require('events'); var emitter=new event ...

  3. liunx 运维知识二部分

    Windows下的目录和Linux系统下的目录有什么区别? Windows目录下的文件一般都是分区(C盘,D盘...),C盘下面有什么目录,目录下面还有其他目录加上文件. Linux系统目录结构一切都 ...

  4. python读文件指定行的数据

    import linecacheprint linecache.getline('url.txt',2) 读取url.txt文件的第2行内容

  5. Jmeter安装与使用(压测)

    一.介绍 Apache JMeter是100%纯JAVA桌面应用程序,被设计为用于测试客户端/服务端结构的软件(例如web应用程序).它可以用来测试静态和动态资源的性能,例如:静态文件,Java Se ...

  6. jenkins 邮箱设置

    一.先设置管理员邮箱地址 二.设置邮箱

  7. MySQL磁盘写入策略以及数据安全性的相关参数

     转载自:http://blog.itpub.net/22664653/viewspace-1063134/   innodb_flush_log_at_trx_commit和sync_binlog ...

  8. SWT 几个sample网站

    https://www.programcreek.com/java-api-examples/org.eclipse.swt.custom.ScrolledComposite https://o7pl ...

  9. qtp 自动化测试桌面程序-点滴1(录制设置、共用文件)

    1 automation-record and run settings--设置录制程序 2 将function/repository 放于单独于test的文件夹中-方便多个test使用同一个仓库.函 ...

  10. Linux vmstat命令

    vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...