题意:有n个1*wi的传单要放在一个h*w的宣传板上,在尽量往上放的基础上尽量往左放,问每个传单放在第几行,若无法放上,则输出-1。
分析:二分,用线段树维护,若前半区间所有行的最大值大于等于wi,那么继续在前半区间搜索,否则搜索后半区间。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 200000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int input[MAXN << 2];
int _max[MAXN << 2];
int h, w, n;
void update(int id, int L, int R, int v){
_max[id] = v;
if(L == R) return;
int mid = L + (R - L) / 2;
update(id << 1, L, mid, v);
update(id << 1 | 1, mid + 1, R, v);
}
int solve(int id, int L, int R, int v){
if(L == R){
_max[id] -= v;
return R;
}
int mid = L + (R - L) / 2;
int ans = 0;
if(_max[id << 1] >= v){
ans = solve(id << 1, L, mid, v);
}
else{
ans = solve(id << 1 | 1, mid + 1, R, v);
}
_max[id] = max(_max[id << 1], _max[id << 1 | 1]);
return ans;
}
int main(){
while(scanf("%d%d%d", &h, &w, &n) == 3){
memset(input, 0, sizeof input);
memset(_max, 0, sizeof _max);
for(int i = 1; i <= n; ++i){
scanf("%d", &input[i]);
}
h = min(h, n);
update(1, 1, h, w);
for(int i = 1; i <= n; ++i){
if(input[i] > w){
printf("-1\n");
continue;
}
if(input[i] > _max[1]){
printf("-1\n");
continue;
}
printf("%d\n", solve(1, 1, h, input[i]));
}
}
return 0;
}