poj3179 Corral the Cows
论水题与难题的差距:在于一个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的更多相关文章
- $Poj3179\ Corral\ the\ Cows$ 二分+离散化+二维前缀和
Poj $Description$ 在一个二维平面上,有$N$颗草,每颗草的大小是$1*1$,左下角坐标为$x_i,y_i$.要求一个正方形,正方形的边平行于$x$或$y$轴,正方形里面包含至少$C$ ...
- POJ3179 Corral the Cows题解
我就是个垃圾--一道水题能写这么长时间-- 首先看到题就想到了二维前缀和+二分边长,但地图边长10000,得离散化. 于是这个离散化就把我搞疯了,淦. 这反映出现在基础知识还是不牢固,相当不牢固. 复 ...
- 洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法
[BZOJ1720][Usaco2006 Jan]Corral the Cows 奶牛围栏 Description Farmer John wishes to build a corral for h ...
- POJ 3179 Corral the Cows
Corral the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1352 Accepted: 565 De ...
- 洛谷——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 ...
- 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 ...
随机推荐
- GANs (Generative Adversarial Networks)
#!/usr/bin/python2.7 #coding:utf-8 import tensorflow as tf import numpy as np import matplotlib.pypl ...
- embed标签的flash层级太高问题
因为客户要求,项目得兼容IE的兼容模式 页面到了flash都会遮挡底部悬浮的导航. 改变浮动窗口和embed的层级还是不可以.应该不是层级的关系. 最后百度解决方案:在embed标签内添加了wmode ...
- python学习笔记(11)--文件与数据格式化
文件的概念: 文件是数据的抽象和集合,是存储在辅助存储器上的数据序列,文件是数据存储的一种形式,文件的展现形态,文本文件和二进制文件. 文本文件输出: f.txt文件保存:“我是中国人” >&g ...
- Yii2上传图片插件使用
例子: 1.在表单中: <?php $form = \yii\widgets\ActiveForm::begin([ 'options'=>[ 'class' => 'form-ho ...
- Java多线程之通过标识关闭线程
package org.study2.javabase.ThreadsDemo.status; /** * @Auther:GongXingRui * @Date:2018/9/19 * @Descr ...
- 学习 Spring (十一) 注解之 Spring 对 JSR 支持
Spring入门篇 学习笔记 @Resource Spring 还支持使用 JSR-250 中的 @Resource 注解的变量或 setter 方法 @Resource 有一个 name 属性,并且 ...
- nargin
nargin 编辑 nargin为“number of input arguments”的缩写. 在matlab中定义一个函数时, 在函数体内部, nargin是用来判断输入变量个数的函数.在matl ...
- yum安装时rpmdb损坏解决方法
解决方案如下: # cd /var/lib/rpm # rm -f /var/lib/rpm/__db* # db_verify Packages # rpm --rebuilddb
- eclipse添加tomcat服务器
在网上找资料好辛苦,还不对,自己试了好久,终于成功了 还是一如既往的分享 右键 弄好以后发现如此简单| _ |
- BZOJ2342[Shoi2011]双倍回文——回文自动机
题目描述 输入 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. 输出 输出文件只有一行,即:输入数据中字符串的最长双倍回文子串的长度,如果双倍回文 ...