luogu P2862 [USACO06JAN]Corral the Cows G
题面传送门
这东西有一个更优的复杂度。
显然可以二分,二分以后对每一维尺取后算是O(nwlogw)O(nwlogw)O(nwlogw)的,可以过去。
但是我们发现在第二维尺取时的这一维www很没有必要,换句话说,很没效率。
所以就可以用一棵权值线段树代替掉,对每一个点向后midmidmid个贡献权值。查询时查询最大值即可。
代码实现:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,l,r,mid,f[80039],sum[80039],ans;
struct yyy{int x,y;}s[10039];
inline bool cmp(yyy x,yyy y){return x.x<y.x;}
inline void push(int now){f[now<<1]+=f[now];f[now<<1|1]+=f[now];sum[now<<1]+=f[now];sum[now<<1|1]+=f[now];f[now]=0;
}
inline void get(int x,int y,int z,int l,int r,int now){if(x<=l&&r<=y){f[now]+=z;sum[now]+=z;return;}if(f[now]) push(now);int m=(l+r)>>1;if(x<=m) get(x,y,z,l,m,now<<1);if(y>m) get(x,y,z,m+1,r,now<<1|1);sum[now]=max(sum[now<<1],sum[now<<1|1]);
}
inline int check(int mid){memset(f,0,sizeof(f));memset(sum,0,sizeof(sum));register int i,l=1,j;for(i=1;i<=n;i++){get(s[i].y,min(s[i].y+mid-1,ans),1,1,ans,1);if(s[i+1].x!=s[i].x){while(s[i].x-s[l].x+1>mid)get(s[l].y,min(s[l].y+mid-1,ans),-1,1,ans,1),l++;if(sum[1]>=m) return 1;}}return 0;
}
int main(){register int i;scanf("%d%d",&m,&n);for(i=1;i<=n;i++) scanf("%d%d",&s[i].x,&s[i].y),ans=max(ans,max(s[i].y,s[i].x));sort(s+1,s+n+1,cmp);l=0;r=ans;while(l+1<r){mid=(l+r)>>1;if(check(mid)) r=mid;else l=mid;}printf("%d\n",r);
}
luogu P2862 [USACO06JAN]Corral the Cows G相关推荐
- P2862 [USACO06JAN]Corral the Cows G
知识点:二分,离散化,尺取法 这个题还是有点难度的,首先这里练习的是二分,然后看到数据范围需要用到离散化,然后判定函数里面需要用到尺取法,但是我这个判定函数的复杂度说实话有点高了,看洛谷的题解说可以二 ...
- 洛谷 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 ...
- P2858 [USACO06FEB]Treats for the Cows G/S 题解
emmmmmm,第二篇文章,多多写文章,好好掌握知识! 前言 原本在educoder上刷题,刷到[粉刷匠]一题,使用区间DP来做的.自己之前曾经小部分刷过背包DP的题目,对于区间DP还是知之甚少.在稍 ...
- Corral the Cows(二分、前缀和、离散化)
题面:Corral the Cows[牛客] 题目大意 有 nnn 个单位的三叶草,每单位三叶草占据一个 1×11×11×1 的土地,每块土地的位置由其左下角的下标确定,并且下标 xxx 和 yyy ...
- POJ - 3179 Corral the Cows(二分,离散化,前缀和)
POJ - 3179 Corral the Cows #include<iostream> #include<vector> #include<algorithm> ...
- P2881 [USACO07MAR]Ranking the Cows G
P2881 [USACO07MAR]Ranking the Cows G 题目描述 输出格式 Line 1: A single integer that is the minimum value of ...
最新文章
- 基于轮廓调整的SOTA实例分割方法,速度达32.3fps | CVPR 2020
- 阿里热更新android,阿里最新热更新使用采坑记录
- keras从入门到放弃(十五)图片数据增强
- python源码编译 mingw_在windows上用gcc(mingw32)从命令行编译Cython扩展
- linux进程退出没有log,Linux下应用进程消失原因分析-Go语言中文社区
- 重塑APM标杆,博睿数据战略升级助力企业数字化转型
- mysql删库后恢复_记一次MySQL删库的数据恢复
- 语法分析与中间代码生成
- 慎用PHP $_REQUEST数组
- 矩池云安装gcc4.9和g++4.9简单教程
- Windows PrintNightmare 漏洞和补丁分析
- mysql主从复制读写分离_MySQL主从复制与读写分离的工作原理
- Cocos2d-x3.2 持续动作
- java关键字 final static
- linux下tomcat8安装详解(附图解步骤)
- linux系统屏幕触摸校准
- Django对接微信公众号以实现消息自动回复
- Day21.Python项目案例
- skewx 字体模糊_Pixelmator:从此修图不再叫做 “PS”
- 苹果中国首家直营店选址北京三里屯