首页 > ACM题库 > HDU-杭电 > HDU 2795-HOJ-Billboard-线段树-[解题报告]c-sharp
2014
02-14

HDU 2795-HOJ-Billboard-线段树-[解题报告]c-sharp

Billboard

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

3 5 5
2
4
3
3
3

样例输出:

1
2
1
3
-1

#include <iostream>
using namespace std;
#define N 200000
#define max(a,b) a>b?a:b

struct LineTree
{
	int l,r;
	int size;
	LineTree *lchild,*rchild;
};

LineTree mem[4*N];
int pos;
int h,w,n;
int f;

LineTree *NewTree()
{
	LineTree *s=&mem[pos++];
	return s;
}

LineTree *CreateTree(int a,int b)
{
	int mid;
	LineTree *s=NewTree();
	s->l=a;
	s->r=b;
	s->size=w;
	if(b-a>1)
	{
		mid=(a+b)/2;
		s->lchild=CreateTree(a,mid);
		s->rchild=CreateTree(mid+1,b);
	}
	else if(b-a==1)
	{
		s->lchild=CreateTree(a,a);
		s->rchild=CreateTree(b,b);
	}
	return s;
}

void search(LineTree *t,int a)
{
	if(a>t->size) return;
	if(t->l==t->r)
	{
		t->size-=a;
		f=t->l;
		return;
	}
	if(t->lchild->size>=a)
		search(t->lchild,a);
	else if(t->rchild->size>=a)
		search(t->rchild,a);
	t->size=max(t->lchild->size,t->rchild->size);
}

int main()
{
	int x;
	LineTree *s;
	while(scanf("%d%d%d",&h,&w,&n)!=EOF)
	{
		pos=0;
		h=h<n?h:n;
		s=CreateTree(1,h);
		while(n--)
		{
			scanf("%d",&x);
			f=-1;
			search(s,x);
			printf("%d/n",f);
		}
	}
	return 0;
}

解题参考:http://blog.csdn.net/xiaotaoqibao/article/details/5750882


  1. 当晚,澎湃新闻从中国气象局相关工作人员处证实了该通知的真实性。该名工作人员表示,这次关于暂停霾预报预警业务的通知只是个内部通知,并没有对外公开发布。

  2. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }