http://acm.hdu.edu.cn/showproblem.php?pid=2795
BillboardTime Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 32597 Accepted Submission(s): 12984 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.
Input There are multiple cases (no more than 40 cases).
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<bits/stdc++.h>
using namespace std;
const int N = 200005;
int a[N];
struct node
{
int l, r, sum;
}t[N<<2];
void build(int x, int l, int r, int k)
{
t[x].l = l, t[x].r = r, t[x].sum = k;
if(l == r) return ;
int mid = (l + r) >> 1;
build(x<<1, l ,mid, k);
build(x<<1|1, mid+1, r, k);
}
int update(int x, int k)
{
if(t[x].l == t[x].r)
{
t[x].sum -= k;
return t[x].l;
}
int res;
if(k <= t[x<<1].sum)
res = update(x<<1, k);
else
res = update(x<<1|1, k);
t[x].sum = max(t[x<<1].sum, t[x<<1|1].sum);
return res;
}
int main()
{
int h, w, n;
while(cin >> h >> w >> n)
{
h = min(h, n);
build(1, 1, h, w);
while(n--)
{
int ans = -1, k;
scanf("%d", &k);
if(k <= t[1].sum)
ans = update(1, k);
printf("%d\n",ans);
}
}
return 0;
}