HDU 2795 Billboard

来源:互联网 发布:mac桌面贴便签 编辑:程序博客网 时间:2024/05/18 21:05

题意:

海报按顺序贴在,可以贴的最左上方。

题解:

最多有min(h,n)行可以贴,那么,每行看成一个节点,用线段树去维护。

//HDU 2795 Billboard#include<stdio.h>#include<math.h>#include<algorithm>using namespace std;#define MAX 200009*3struct Node {    int Max;    int L,R;    Node *left;    Node *right;};Node Tree[MAX];int ans,w,cnt;void build(int l,int r,struct Node *root) {    root->Max=w;    root->L=l;    root->R=r;    if(l==r) return;    int mid=(l+r)/2;    cnt++;    root->left=Tree+cnt;    cnt++;    root->right=Tree+cnt;    build(l,mid,root->left);    build(mid+1,r,root->right);}void query(int len,struct Node *root) {    if(root->Max<len) return;    if(root->L==root->R) {        root->Max-=len;        ans=root->L;        return ;    }    if(root->left->Max>=len) {        query(len,root->left);    } else {        query(len,root->right);    }    root->Max=max(root->left->Max,root->right->Max);}int main() {    int n,h;    while(~scanf("%d %d %d",&h,&w,&n)) {        cnt=0;        int tmp=min(h,n);        build(1,tmp,Tree);        for(int i=0; i<n; i++) {            scanf("%d",&tmp);            ans=-1;            query(tmp,Tree);            printf("%d\n",ans);        }    }    return 0;}


0 0
原创粉丝点击