hdu 2795

来源:互联网 发布:python opencv clone 编辑:程序博客网 时间:2024/06/04 01:21

       题意:给一个w*h的矩形公布栏,有n个1*wi的海报要往上面贴,贴的时候要尽量在高的地方贴,相同高度的时候尽量往左贴,问最多能贴多少张海报(注意不能重叠)。

       分析:使用线段树。用最低层的结点记录每一行的剩余最大宽度,父亲结点记录孩子结点的最大宽度,这样,每次都先考虑左孩子,再考虑右孩子,就能算出结果了。其实这题只要想明白最底层叶子结点从左往右的顺序代表着消息栏从高到底的顺序,此题便是水题了,想不明白,就是“难题”,嗯,好吧,其实还是自己太水了。。。

      代码如下:

#include <cstdio>#include <stack>#include <set>#include <iostream>#include <string>#include <vector>#include <queue>#include <list>#include <functional>#include <cstring>#include <algorithm>#include <cctype>#include <string>#include <map>#include <iomanip>#include <cmath>#define LL long long#define ULL unsigned long long#define SZ(x) (int)x.size()#define Lowbit(x) ((x) & (-x))#define MP(a, b) make_pair(a, b)#define MS(arr, num) memset(arr, num, sizeof(arr))#define PB push_back#define F first#define S second#define ROP freopen("input.txt", "r", stdin);#define MID(a, b) (a + ((b - a) >> 1))#define LC rt << 1, l, mid#define RC rt << 1|1, mid + 1, r#define LRT rt << 1#define RRT rt << 1|1#define BitCount(x) __builtin_popcount(x)#define BitCountll(x) __builtin_popcountll(x)#define LeftPos(x) 32 - __builtin_clz(x) - 1#define LeftPosll(x) 64 - __builtin_clzll(x) - 1const double PI = acos(-1.0);const int INF = 0x3f3f3f3f;using namespace std;const double eps = 1e-5;const int MAXN = 300 + 10;const int MOD = 1000007;const double M=1e-8;const int N=1e6+10;typedef pair<int, int> pii;typedef pair<int, string> pis;const int d[4][2]={{0,1},{0,-1},{-1,0},{1,0}};int n,r,w,st[N];void updata(int k,int a){    k+=n-1;    st[k]+=a;    while(k>0) {        k>>=1;        st[k]=max(st[k<<1],st[k<<1|1]);    }}int query(int a,int rt){    int t=0;    if (rt>=n) {        if (rt-n+1>w) return 0;        updata(rt-n+1,-a);        return rt-n+1;    }    else  {        if (st[rt<<1]>=a) {  t=query(a,rt<<1);   }        else if (st[rt<<1|1]>=a) {  t=query(a,rt<<1|1);    }    }    return t;}int main(){    int i,j,h;    while(~scanf("%d%d%d",&w,&h,&n))    {        MS(st,0);        r=n,n=1;        int ans=0;        while(n<r) n*=2;        if (n==1) n=2;        for (i=1;i<=r;i++) updata(i,h);   // build tree        for (i=0;i<r;i++) {            int a;            scanf("%d",&a);            int x=query(a,1);            if (x) cout<<x<<endl;            else puts("-1");        }        // for (j=1;j<2*n;j++) cout<<st[j]<< " "; cout<<endl;    }}


0 0
原创粉丝点击