BZOJ1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏
生活随笔
收集整理的這篇文章主要介紹了
BZOJ1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
此題非常一眼
就是直接分別將 x,y 坐標離散化
并在離散化之后做二維前綴和
二分一下答案,O(n^2) 的 check 即可
注意在 check 當中的二分 x - mid 的過程中,需要去二分 x - mid + 1
因為 STL lower_bound 是找大于等于參數的第一個數
如果直接二分 x - mid ,可能會找到想要的行的下一行
而二分 x - mid + 1 就不會出現這種問題
只需要在二分之后將得到的下標 -1 就行了
復雜度 O(n^2*log1e4)
網上好像有別的做法,懶得寫了 = = ...
代碼:
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cctype> #include<cstdio> using namespace std;const int MAXN = 505;struct POS{int x, y, ux, uy;POS(int X = 0, int Y = 0) {x = X; y = Y;} }pos[MAXN]; int n, c, sizx, sizy, maxp; int unx[MAXN], uny[MAXN], sum[MAXN][MAXN];inline bool chk(int mid) {for(int i = 1; i <= sizx; ++i) {register int dstx = lower_bound(unx + 1, unx + sizx + 1, unx[i] - mid + 1) - unx;for(int j = 1; j <= sizy; ++j) {register int dsty = lower_bound(uny + 1, uny + sizy + 1, uny[j] - mid + 1) - uny;if(sum[i][j] - sum[i][dsty - 1] - sum[dstx - 1][j] + sum[dstx - 1][dsty - 1] >= c) return true;}}return false; } inline void hfs(int l, int r) {int mid = 0, ans = 0;while(l <= r) {mid = ((l + r) >> 1);if(chk(mid)) {ans = mid;r = mid - 1;} else l = mid + 1;}printf("%d\n", ans);return; }int main() {scanf("%d%d", &c, &n);register int xx, yy;for(int i = 1; i <= n; ++i) {scanf("%d%d", &xx, &yy);pos[i] = POS(xx, yy);unx[i] = xx; uny[i] = yy;maxp = max(maxp, max(xx, yy));}sort(unx + 1, unx + n + 2); sort(uny + 1, uny + n + 2);sizx = unique(unx + 1, unx + n + 2) - unx - 1;sizy = unique(uny + 1, uny + n + 2) - uny - 1;for(int i = 1; i <= n; ++i) {pos[i].ux = lower_bound(unx + 1, unx + sizx + 1, pos[i].x) - unx;pos[i].uy = lower_bound(uny + 1, uny + sizy + 1, pos[i].y) - uny;++sum[pos[i].ux][pos[i].uy];}for(int i = 1; i <= sizx; ++i) {for(int j = 1; j <= sizy; ++j) {sum[i][j] = sum[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];}}hfs(1, maxp);return 0; }轉載于:https://www.cnblogs.com/xcysblog/p/9737782.html
總結
以上是生活随笔為你收集整理的BZOJ1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Coremail邮件系统存在配置信息泄露
- 下一篇: 自动化与网络工程之间的关系