题意:
给你一块h*w的面板, 每次有一个1*wi的公告要放在上面,并且,公告放上了就不会拿下来。问这个公告能放的最高的位置是哪里(也就是求能放下L的最小h)?
思路:
刚开始想了半天,没思路,然后放了一两天之后,再想,还是没思路,于是只好百度题解思路了。
关键就是需要一个数据结构能够快速二分查询,到底能插到哪个地方。
如果我能知道从一个范围到另一个范围的最大值是多少,那么我只要使公告的位置在允许的范围内尽可能的靠前,
那么答案也就出来了。
按照这个需求,树里面存的应该是最大值。
Code:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<bitset>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<list>
//#define TEST
#define LL long long
#define Mt(f, x) memset(f, x, sizeof(f));
#define xep(i, n) for(int i = 0; i < (n); ++i)
#define rep(i, s, e) for(int i = (s); i <= (e); ++i)
#define dep(i, s, e) for(int i = (s); i >= (e); --i)
#ifdef TEST
#define See(a) cout << #a << " = " << a << endl;
#define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl;
#define debug(a, s, e) rep(_i, s, e) { cout << a[_i] << ' ';}cout << endl;
#define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[i_], ss, ee);}
#else
#define See(a)
#define See2(a, b)
#define debug(a, s, e)
#define debug2(a, s, e, ss, ee)
#endif
const int MAX = 2e9;
const int MIN = -2e9;
const double eps = 1e-8;
const double PI = acos(-1.0);
using namespace std;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const int N = 200000 + 5;
int Max[N << 2];
inline int max(int a, int b)
{
return a > b ? a : b;
}
void pushUp(int rt)
{
Max[rt] = max(Max[rt << 1] , Max[rt << 1 | 1]);
}
int query(int q, int l, int r, int rt)
{
if(l == r)
{
if(q <= Max[rt])
{
Max[rt] -= q;
return l;
}
return -1;
}
int m = (l + r) >> 1;
if(q <= Max[rt])
{
int t = query(q, lson);
if(t != -1)
{
pushUp(rt);
return t;
}
else
{
t = query(q, rson);
pushUp(rt);
return t;
}
}
return -1;
}
int main()
{
int n, h, w;
while(~scanf("%d%d%d", &h, &w, &n))
{
int q = min(h, n);
fill(Max, Max + (n << 2), w);
rep(i, 1, n)
{
int a;
scanf("%d", &a);
printf("%d\n", query(a, 1, q, 1));
}
}
return 0;
}