日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

BZOJ1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏

發(fā)布時間:2025/3/15 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BZOJ1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

此題非常一眼

就是直接分別將 x,y 坐標(biāo)離散化

并在離散化之后做二維前綴和

二分一下答案,O(n^2) 的 check 即可

注意在 check 當(dāng)中的二分 x - mid 的過程中,需要去二分 x - mid + 1
因為 STL lower_bound 是找大于等于參數(shù)的第一個數(shù)
如果直接二分 x - mid ,可能會找到想要的行的下一行

而二分 x - mid + 1 就不會出現(xiàn)這種問題
只需要在二分之后將得到的下標(biāo) -1 就行了

復(fù)雜度 O(n^2*log1e4)

網(wǎng)上好像有別的做法,懶得寫了 = = ...


代碼:

#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; }

轉(zhuǎn)載于:https://www.cnblogs.com/xcysblog/p/9737782.html

總結(jié)

以上是生活随笔為你收集整理的BZOJ1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。