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.
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
#7693286 zhuyinghui's solution for [Problem D]
Status | Accepted |
---|---|
Time | 3354ms |
Memory | 7740kB |
Length | 1124 |
Lang | G++ |
Submitted | 2016-12-08 19:13:23 |
Shared |
Select Code
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 200010
using namespace std;
struct node
{
int l,r;
int val;
}tree[maxn<<2];
int ans;
void build(int rt,int p,int q,int w)
{
int mid=(p+q)>>1;
tree[rt].l=p;
tree[rt].r=q;
if(p==q)
{
tree[rt].val=w;
return ;
}
build(rt<<1,p,mid,w);
build(rt<<1|1,mid+1,q,w);
tree[rt].val=max(tree[rt<<1].val,tree[rt<<1|1].val);
}
void updata(int rt,int m)
{
if(tree[rt].l==tree[rt].r)
{
ans=tree[rt].l;
tree[rt].val-=m;
return ;
}
if(tree[rt<<1].val>=m) //注意此处的>=还是等于
{
updata(rt<<1,m);
}
else
updata(rt<<1|1,m);
tree[rt].val=max(tree[rt<<1].val,tree[rt<<1|1].val);
}
int main()
{
int h,n,m,w;
while(~scanf("%d %d %d ",&h,&w,&n))
{
if(h>n)//当大于n时无意义
h=n;
build(1,1,h,w);
while(n--)
{
scanf("%d",&m);
if(tree[1].val>=m)//注意符号选择
{
updata(1,m);
}
else
ans=-1;
printf("%d\n",ans);
}
}
return 0;
}