hdu 2795-Billboard(线段树)
Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 26278 Accepted Submission(s): 10746
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.
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.
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
题意:
在一个板子上贴海报高为h,宽为w,每个海报大小为1*w,海报尽量放在高并且靠左的地方。如果没有地方放输出-1;
题解:
大体思路为维护每个节点的最大宽度。
ps:高度为1e9 不可能按高度建树,可以先将高度优化为h
#include <stdio.h> #include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define mem(arr, num) memset(arr, 0, sizeof(arr)) #define _for(i, a, b) for (int i = a; i <= b; i++) #define __for(i, a, b) for (int i = a; i >= b; i--) #define IO \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace std; typedef long long ll; const ll inf = 0x3f3f3f3f; const double EPS = 1e-10; const ll mod = 1000000007LL; const int N = 1 << 19; ll dat[N]; int num,h,w; int tree_node; void init(int n) { tree_node = 1; while(tree_node < n) tree_node <<= 1; _for(i, 1, tree_node * 2 -1) dat[i] = 0; } void update(int pos, int date) { int k = tree_node - 1 + pos; dat[k] = date; while(k>1) { k >>= 1; dat[k] = max(dat[k<<1], dat[k<<1|1]); } } int query(int wi,int k) { if(k >= tree_node) { update(k-(tree_node - 1),dat[k]-wi); return k-(tree_node - 1); } else if(wi <= dat[k<<1]) return query(wi, k<<1); else return query(wi, k<<1|1); } void solve() { int x; _for(i, 1, h) update(i, w); _for(i, 1, num) { scanf("%d",&x); if(x > dat[1]) printf("-1\n"); else printf("%d\n",query(x,1)); } } int main() { while(scanf("%d%d%d",&h,&w,&num)!=EOF) { if(h > num) h = num; //优化树的高度 init(h); solve(); } return 0; }
BillboardBillboard
宝剑锋从磨砺出 梅花香自苦寒来
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步