论水题与难题的差距:在于一个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. UTC时间、GMT时间、本地时间、Unix时间戳

    引用: https://blog.csdn.net/u012102306/article/details/51538574 https://blog.csdn.net/foxir/article/de ...

  2. PLA-1

    PLA(Principal Component Analysis)主成分分析: 思路1:坐标旋转 1.数据分布以及投影: 2.特征值以及特征向量 思路2: 未完待续...

  3. 用junit对java代码进行测试,需要注意

    1.用@Test注解的方法必须没有返回值,返回值类型无:void 2.用@Test注解的方法必须没有参数.

  4. python之路--基础数据类型的补充与深浅copy

    一 . join的用法 lst =['吴彦祖','谢霆锋','刘德华'] s = '_'.join(lst) print(s) # 吴彦祖_谢霆锋_刘德华 # join() "*" ...

  5. python学习笔记(5-1)-基本数据类型-字符串类型及操作

    五.字符串处理函数  len(x):字符串x的长度.如len("12345")结果为5  str(x):任意类型x所对应的字符串形式. >>> str(123) ...

  6. 使Python中的turtle模块画图两只小羊

    turtle.circle(radius, extent=None, steps=None) 描述: 以给定半径画圆 参数: radius(半径); 半径为正(负),表示圆心在画笔的左边(右边)画圆 ...

  7. npm 设置代理

    设置代理 npm config set proxy http://username:password@server:portnpm config set https-proxy http://user ...

  8. Java多线程之Join方法阻塞线程

    package org.study2.javabase.ThreadsDemo.status; /** * @Auther:GongXingRui * @Date:2018/9/19 * @Descr ...

  9. ABP 番外篇-菜单

    public class LearningMpaAbpNavigationProvider : NavigationProvider { public override void SetNavigat ...

  10. C#后台绑定select