Billboard(线段树,单点更新)
2015-10-12 22:39
429 查看
A - Billboard
Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
2795
Appoint description:
bupt_admin_13 (2015-07-28)System Crawler (2015-10-11)
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
思路:
其实就是求1*W的板要从上到下最多的放到板。一开始根本没思路,看起来与之前做的题都不一样,最后又有了看题解的冲动。但是又想了想还是再想多一个钟吧,没想到竟然被我想到了,之后还1A。这题与求最值有点不同在,大多数题目是叫你求区间的最值,但是这题却是求点的。
AC代码:
Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
2795
Appoint description:
bupt_admin_13 (2015-07-28)System Crawler (2015-10-11)
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
思路:
其实就是求1*W的板要从上到下最多的放到板。一开始根本没思路,看起来与之前做的题都不一样,最后又有了看题解的冲动。但是又想了想还是再想多一个钟吧,没想到竟然被我想到了,之后还1A。这题与求最值有点不同在,大多数题目是叫你求区间的最值,但是这题却是求点的。
AC代码:
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> using namespace std; #define T 2000005 #define inf 0xffffffff struct node { int v; int l,r,mid; }tree[T]; int val; void PushUp(int rt) { tree[rt].v = max(tree[rt<<1].v,tree[rt<<1|1].v); } void Build(int rt,int L,int R) { tree[rt].l = L,tree[rt].r = R; tree[rt].mid = (L+R)>>1; if(L==R){ tree[rt].v=val; return; } Build(rt<<1,L,tree[rt].mid); Build(rt<<1|1,tree[rt].mid+1,R); PushUp(rt); } void Update(int rt,int pos) { if(tree[rt].l==tree[rt].r){ tree[rt].v -= pos; return; } if(pos<=tree[rt<<1].v){ Update(rt<<1,pos); } else { Update(rt<<1|1,pos); } PushUp(rt); } int query(int rt,int pos) { if(tree[rt].v<pos){ return 0; } if(tree[rt].v>=pos&&tree[rt].l==tree[rt].r){ return tree[rt].l; } if(tree[rt<<1].v>=pos){ query(rt<<1,pos); } else { query(rt<<1|1,pos); } } int main() { /*freopen("input.txt","r",stdin);*/ int n,m,i,j,k,t; while(~scanf("%d%d%d",&n,&val,&k)) { if(n>T-5) Build(1,1,k); else Build(1,1,n); for(i=1;i<=k;++i){ scanf("%d",&j); if(j>val) printf("-1\n"); else { t=query(1,j); if(!t){ printf("-1\n"); } else { printf("%d\n",t); Update(1,j); } } } } return 0; }
相关文章推荐
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- 数据结构之Treap详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)
- java数据结构和算法学习之汉诺塔示例
- Java数据结构及算法实例:三角数字
- Java数据结构之简单链表的定义与实现方法示例
- 数据结构之AVL树详解
- qqwry.dat的数据结构图文解释第1/2页
- JavaScript中数据结构与算法(五):经典KMP算法