HDU 2795 Billboard (線段樹+貪心)
手動博客搬家:本文發表於20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/article/details/77488127
URL: http://acm.hdu.edu.cn/showproblem.php?pid=2795題目大意:有一個h*w的木板 (h, w<=1e9), 現在有n (n<=2e5)張1*xi的海報要貼在木板上,按1~n的順序每次貼海報時會選擇最上的一排的最左邊貼 (海報不能互相覆蓋), 求每張海報會被貼在哪一行。最上方的一行編號為1, 以此類推。
樣例解析:
如下
column 12345 row 1: 11333 row 2: 2222X row 3: 444XXX: 未擺放;
對應行列上的數是擺放在此的海報的編號思路分析:本題實際上是一個貪心的思想。
每次可以從最上一行向下枚舉剩余的空間,找到第一個能夠貼上第i個海報的行,然後輸出,更新即可。
時間復雜度O(n^2), 無法AC.
於是我們可以考慮二分的思想:
用線段樹維護每一行剩余的空間的大小的最大值。
每次查詢時,采用類似於二分答案的方法,每到達線段樹的一個節點,若它的左子樹最大值>=xi, 則左子樹中必存在合法的最大答案,查詢左子樹;
假如左子樹中的行無法裝下第i張海報,但右子樹最大值<=xi, 此時說明右子樹中存在合法的最大答案,於是“退而求其次”,查詢右子樹。
如果兩棵子樹最大值都>xi, 則無法張貼此海報,於是輸出-1.
查詢完畢後,做一個單點修改,將答案所在的行剩余空間減去xi.
其實,最後一種情況不需要考慮。
假如整個[1, h]區間的最大值<xi, 則直接輸出-1, 無需查詢。
但是我們無法開4e9 (4h)如此大的數組,怎麽辦呢?
其實假如h>n, 那麽下面的(h-n)行全部浪費了,不影響結果。
因此,h=min(h,n);
只開8e5 (4
代碼呈現:(Time: 3151 MS; 11256 KB; Code: 1576 B)
#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 2e5; struct Node { int left,right; int maxi; }; struct SegmentTree { Node nd[MAXN*4+8]; void init() { for(int i=1; i<=MAXN*4; i++) { nd[i].left = nd[i].right = nd[i].maxi = 0; } } void build(int lbound,int rbound,int pos,int tot) { nd[pos].left = lbound; nd[pos].right = rbound; if(lbound==rbound) { nd[pos].maxi = tot; return; } int mid = (lbound+rbound)/2; build(lbound,mid,2*pos,tot); build(mid+1,rbound,2*pos+1,tot); nd[pos].maxi = tot; } void modify_subs(int bound,int val,int pos) { int mid = (nd[pos].left+nd[pos].right)/2; if(nd[pos].left==nd[pos].right) { nd[pos].maxi-=val; return; } if(bound<=mid) modify_subs(bound,val,2*pos); else modify_subs(bound,val,2*pos+1); nd[pos].maxi = max(nd[2*pos].maxi,nd[2*pos+1].maxi); } int query(int pos,int val) { int mid = (nd[pos].left+nd[pos].right)/2; if(nd[pos].left==nd[pos].right) return nd[pos].left; int ans; if(val<=nd[2*pos].maxi) return query(2*pos,val); else return query(2*pos+1,val); } }; SegmentTree sgt; int n,m,p; int main() { while(scanf("%d%d%d",&n,&m,&p)!=EOF) { if(n>p) n = p; sgt.init(); sgt.build(1,n,1,m); for(int i=1; i<=p; i++) { int x; scanf("%d",&x); if(sgt.nd[1].maxi < x) { printf("-1\n"); continue; } int ans = sgt.query(1,x); printf("%d\n",ans); if(ans>0) sgt.modify_subs(ans,x,1); } } return 0; }
HDU 2795 Billboard (線段樹+貪心)