我是靠谱客的博主 称心香氛,最近开发中收集的这篇文章主要介绍线段树裸题系列 Billboard,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

hdu2795

Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12470    Accepted Submission(s): 5456


Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.

On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.

Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.

When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.

If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).

Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
 

Input
There are multiple cases (no more than 40 cases).

The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.

Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
 

Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.
 

Sample Input
  
  
3 5 5 2 4 3 3 3
 

Sample Output
  
  
1 2 1 3 -1
 

第二遍开刷线段树了,这次要将难度搞的大一些。裸题试试手!

以前的代码:

#include<cstdio>
int t[800010];
int query(int id,int l,int r,int x){
    if(l==r) {t[id]-=x;return l;}
    int mid=l+r>>1,ret;
    ret=t[id<<1]>=x?query(id<<1,l,mid,x):query(id<<1|1,mid+1,r,x);
    t[id]=t[id<<1]>t[id<<1|1]?t[id<<1]:t[id<<1|1];
    return ret;
}
int main()
{
    int h,w,n,a,s;
    while(~scanf("%d%d%d",&h,&w,&n)){
        s=h>n?n:h;
        for(int i=1;i<800010;i++) t[i]=w;
        while(n--){
            scanf("%d",&a);
            if(a>t[1]) puts("-1");
            else printf("%dn",query(1,1,s,a));
        }
    }
    return 0;
}

现在的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define Maxn 200010
#define ls l,m,id<<1
#define rs m+1,r,id<<1|1
using namespace std;

int h,w;
int tr[Maxn<<2];
void pushup(int id){
    tr[id]=max(tr[id<<1],tr[id<<1|1]);
}
void build(int l,int r,int id){
    tr[id]=w;
    if(l==r) return;
    int m=l+r>>1;
    build(ls);
    build(rs);
}
int query(int l,int r,int id,int x){
    if(l==r){
        tr[id]-=x;
        return l;
    }
    int m=l+r>>1;
    int res;
    if(tr[id<<1]>=x) res=query(ls,x);
    else res=query(rs,x);
    pushup(id);
    return res;
}
int main()
{
    int s,x;
    while(cin>>h>>w>>s){
        if(h>s) h=s;
        build(1,h,1);
        while(s--){
            scanf("%d",&x);
            if(tr[1]<x) puts("-1");
            else printf("%dn",query(1,h,1,x));
        }
    }
    return 0;
}

总之,要有自己的线段树风格,推荐notonlysuccess的代码风格!

poj2828

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 14745 Accepted: 7342

Description

Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…

The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.

It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!

People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.

Input

There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.

There no blank lines between test cases. Proceed to the end of input.

Output

For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.

Sample Input

4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492

Sample Output

77 33 69 51
31492 20523 3890 19243

Hint

The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.


这题起初想到倒序做,然后用一个并查集维护每个位置对应的下一个可插入位置,但后来发现这样做是不对的,比如第二组数据:在插入1 19243的时候,我们会插入到位置1处,实际上由于他前面有0 20523,那么1 19243会后移一位,插入到位置2处。虽然并查集的做法是错误的,但想法已接近正解。实际上我们是利用线段树来记录空位,在倒序做的时候,比如我们插入1 19243的时候我们必须保证这之前有1个空位,因为是倒着做,反过来就是原先那个空位有一个人,然后插入到那个人后面。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ls l,m,id<<1
#define rs m+1,r,id<<1|1
#define Maxn 200010
using namespace std;

int tr[Maxn<<2];
int ans[Maxn],a[Maxn],b[Maxn];
void pushup(int id){
    tr[id]=tr[id<<1]+tr[id<<1|1];
}
void build(int l,int r,int id){
    tr[id]=1;
    if(l==r) return;
    int m=l+r>>1;
    build(ls);
    build(rs);
    pushup(id);
}
int query(int l,int r,int id,int a){
    if(l==r) {tr[id]=0;return l;}
    int m=l+r>>1;
    int res;
    if(a<tr[id<<1]) res=query(ls,a);
    else res=query(rs,a-tr[id<<1]);
    pushup(id);
    return res;
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        build(0,n-1,1);
        for(int i=0;i<n;i++)
            scanf("%d%d",a+i,b+i);
        for(int i=n-1;i>=0;i--)
            ans[query(0,n-1,1,a[i])]=b[i];
        for(int i=0;i<n;i++)
            printf(i==0?"%d":" %d",ans[i]);
        puts("");
    }
    return 0;
}

最后

以上就是称心香氛为你收集整理的线段树裸题系列 Billboard的全部内容,希望文章能够帮你解决线段树裸题系列 Billboard所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部