设为首页 加入收藏

TOP

HDU2795 billboard[转化为线段树。]
2014-11-23 21:27:53 来源: 作者: 【 】 浏览:5
Tags:HDU2795 billboard 化为 线段

题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子
思路:每次找到最大值的位子,然后减去L
线段树功能:query:区间求最大值的位子(直接把update的操作在query里做了)

技巧挺好,一开始自己思路建个超大二维数组,显然内存不够。

然后。 线段树的话其实就是深搜,if(l==r) 返回的肯定是最左边的结点,哈~

判断的时候直接用Max[1]与p比较就能判断是否输出-1,赞一个!!

#include    
#include    
#include    
#include    
#include    
using namespace std;  
  
#define lson rt<<1,l,m   
#define rson rt<<1|1,m+1,r   
  
int h,w,n;  
  
const int maxn=200005;// 是200000还是20000要搞清楚   
int Max[maxn<<2];     //开maxn的多少倍结合最底层的结点数加以注意   
  
void Pushup(int rt)  
{  
    Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);  
}  
void build(int rt,int l,int r)  
{  
    Max[rt]=w;  
    if(l==r) return;  
    int m=(l+r)>>1;  
    build(lson);  
    build(rson);  
}  
int query(int p,int rt,int l,int r)//技巧5 . update放在query里面了   
{  
    if(l==r)  
    {  
        Max[rt]-=p;  
        return l;// 2. 返回行数   
    }  
  
    int m=(l+r)>>1;  
    int ret;  
    ret=(Max[rt<<1]>=p) query(p,lson):query(p,rson);// 技巧1.这地方深搜返回最左边的结点即符合的。   
    Pushup(rt);// 技巧4 . 自己写这个地方有可能会忘   
    return ret;  
}  
  
int main()  
{  
    while(scanf("%d%d%d",&h,&w,&n)!=EOF)  
    {  
        if(h>n) {h=n;}  
        build(1,1,h);  
  
        int p;  
        for(int i=1;i<=n;i++)  
        {  
            scanf("%d",&p);  
            if(Max[1]
#include 
#include 
#include 
#include 
using namespace std;

#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r

int h,w,n;

const int maxn=200005;// 是200000还是20000要搞清楚
int Max[maxn<<2];     //开maxn的多少倍结合最底层的结点数加以注意

void Pushup(int rt)
{
    Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
void build(int rt,int l,int r)
{
    Max[rt]=w;
    if(l==r) return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
int query(int p,int rt,int l,int r)//技巧5 . update放在query里面了
{
    if(l==r)
    {
        Max[rt]-=p;
        return l;// 2. 返回行数
    }

    int m=(l+r)>>1;
    int ret;
    ret=(Max[rt<<1]>=p) query(p,lson):query(p,rson);// 技巧1.这地方深搜返回最左边的结点即符合的。
    Pushup(rt);// 技巧4 . 自己写这个地方有可能会忘
    return ret;
}

int main()
{
    while(scanf("%d%d%d",&h,&w,&n)!=EOF)
    {
        if(h>n) {h=n;}
        build(1,1,h);

        int p;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p);
            if(Max[1] 
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇URAL 1029 下一篇红黑树源码实现

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)
·关于 MySQL 数据库学 (2025-12-26 23:20:16)
·SOLVED: Ubuntu 24.0 (2025-12-26 22:51:53)
·Linux 常用命令最全 (2025-12-26 22:51:50)