论水题与难题的差距:在于一个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. Junit概述

    Junit ->  java unit.也就是说Junit是xunit家族中的一员. unit   <- unit test case,即单元测试用例. Junit  = java uni ...

  2. 手机端图像编辑上传-cropper

    编辑头像,实现相册,照像功能,并能缩放裁剪功能,可自定义UI,引用'cropper.js', 'exif.js' /*初始化裁剪插件*/ var screenWidth = $(window).wid ...

  3. python爬虫之PyQuery的基本使用

    PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...

  4. mysql 如何查看sql语句执行时间和效率

    查看执行时间 1 show profiles; 2 show variables;查看profiling 是否是on状态: 3 如果是off,则 set profiling = 1: 4 执行自己的s ...

  5. linux中的set -e 与set -o pipefail

    1.set -e "Exit immediately if a simple command exits with a non-zero status." 在“set -e”之后出 ...

  6. 三、zookeeper安装

    一.简介 二.下载解压: #wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar ...

  7. 基于create-react-app的再配置

    前面的话 使用Facebook官方推出的create-react-app脚手架,我们基本可以零配置搭建基于webpack的React开发环境.但是,如果需要个性化定制,则还需要基于create-rea ...

  8. QXcbConnection: Could not connect to display

    import matplotlib; matplotlib.use('agg') 注意:要添加到所有matplotlib前面,否则不起作用

  9. DOSD用scratch的方式训练通用目标检测,性能很高

    推荐一篇今年ICCV上基于DenseNet的general object detection的工作.这是目前已知的第一篇在完全脱离ImageNet pre-train模型的情况下使用deep mode ...

  10. qt 在窗口上画框

    在窗口w上面画个黄色的框:在窗口上添加一个label,然后在label上画框 QLabel label(&w); label.setScaledContents(true); QPixmap ...